Skip to content

Commit f6377e8

Browse files
TiBeNTiBeN
authored andcommitted
Added remove feature\nWritten CHANGELOG.md
1 parent 5545ba0 commit f6377e8

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1.1.0 (2014-01-11)
2+
------------------
3+
- Cronjob can now be removed from de crontab
4+
5+
1.0.0 (2014-01-11)
6+
------------------
7+
- Done some refactoring and cs fixes
8+
- Written README.md documentation
9+
- Preparing source to be published on Github
10+

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ $crontabJob->hours = '21';
7070
$crontabRepository->persist();
7171
```
7272

73+
### Remove a cron job from the crontab
74+
You can removing a job like this :
75+
```php
76+
$results = $crontabRepository->findJobByRegex('/Logging\ disk\ usage/');
77+
$crontabJob = $results[0];
78+
$crontabRepository->removeJob($crontabJob);
79+
$crontabRepository->persist();
80+
```
81+
Note: Since cron jobs are internally matched by reference, theses must be previously obtained by the repository.
82+
7383
### Work with the crontab of another user than runtime user:
7484
This feature allow you to manage the crontab of another user than the user who launched the runtime. This can be useful when the runtime user is `www-data` but the owner of the crontab you want to edit is your own linux user account.
7585

src/CrontabManager/CrontabRepository.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,28 @@ public function findJobByRegex($regex)
9696
*/
9797
public function addJob(CrontabJob $crontabJob)
9898
{
99+
if (array_search($crontabJob, $this->crontabJobs) !== false) {
100+
$exceptionMessage = 'This job is already in the crontab. Please consider cloning the'
101+
. 'CrontabJob object if you want it to be registered twice.'
102+
;
103+
throw new \LogicException($exceptionMessage);
104+
}
99105
array_push($this->crontabJobs, $crontabJob);
100106
}
101-
107+
108+
/**
109+
* Remove a CrontabJob in the connected crontab
110+
* @param CrontabJob $crontabJob
111+
*/
112+
public function removeJob(CrontabJob $crontabJob)
113+
{
114+
$jobKey = array_search($crontabJob, $this->crontabJobs, true);
115+
if ($jobKey === false) {
116+
throw new \LogicException('This job is not part of this crontab');
117+
}
118+
unset($this->crontabJobs[$jobKey]);
119+
}
120+
102121
/**
103122
* Save all operations to the connected crontab.
104123
*/

tests/CrontabManager/Tests/CrontabRepositoryTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,42 @@ public function testPersist()
174174
$crontabRepository->persist();
175175
}
176176

177+
/**
178+
* Test Remove a job
179+
*/
180+
public function testRemove()
181+
{
182+
/* Create fake crontabAdapter */
183+
$fakeCrontabAdapter = $this->getMock('TiBeN\CrontabManager\CrontabAdapter');
184+
185+
$fakeCrontabAdapter
186+
->expects($this->any())
187+
->method('readCrontab')
188+
->will($this->returnValue(file_get_contents($this->fixturesPath . 'testing_persisted_crontab.txt')))
189+
;
190+
191+
$fakeCrontabAdapter
192+
->expects($this->once())
193+
->method('writeCrontab')
194+
->with($this->equalTo(file_get_contents($this->fixturesPath . 'testing_removed_crontab.txt')))
195+
;
196+
197+
$crontabRepository = new CrontabRepository($fakeCrontabAdapter);
198+
199+
/* Retrieve job */
200+
$crontabJobs = $crontabRepository->findJobByRegex('/launch\ -param\ mycommand/');
201+
202+
/* Apply some local updates on the job to ensure
203+
* it is linked by reference to the repository
204+
*/
205+
$crontabJobs[0]->minutes = '00';
206+
$crontabJobs[0]->hours = '01';
207+
208+
/* Remove job */
209+
$crontabRepository->removeJob($crontabJobs[0]);
210+
$crontabRepository->persist();
211+
}
212+
177213
/**
178214
* Test if pass a wrong regular expression when searching by regex throw an invalid regex exception
179215
* @expectedException InvalidArgumentException
@@ -191,4 +227,45 @@ public function testExceptionInvalidRegexOnFindJobByRegex()
191227
$crontabRepository = new CrontabRepository($fakeCrontabAdapter);
192228
$crontabRepository->findJobByRegex('/$');
193229
}
230+
231+
/**
232+
* Test remove an unknown Job
233+
* @expectedException LogicException
234+
* @expectedExceptionMessage This job is not part of this crontab
235+
*/
236+
public function testRemoveAnUnknownJob()
237+
{
238+
$fakeCrontabAdapter = $this->getMock('TiBeN\CrontabManager\CrontabAdapter');
239+
240+
$fakeCrontabAdapter
241+
->expects($this->any())
242+
->method('readCrontab')
243+
->will($this->returnValue(file_get_contents($this->fixturesPath . 'simple_crontab.txt')))
244+
;
245+
$crontabRepository = new CrontabRepository($fakeCrontabAdapter);
246+
247+
$job = CrontabJob::createFromCrontabLine('30 23 * * * launch -param mycommand');
248+
$crontabRepository->removeJob($job);
249+
}
250+
251+
/**
252+
* Add an already in the repository job
253+
* @expectedException \LogicException
254+
*/
255+
public function testAddAnAlreadyInTheRepositoryJob()
256+
{
257+
$fakeCrontabAdapter = $this->getMock('TiBeN\CrontabManager\CrontabAdapter');
258+
259+
$fakeCrontabAdapter
260+
->expects($this->any())
261+
->method('readCrontab')
262+
->will($this->returnValue(file_get_contents($this->fixturesPath . 'simple_crontab.txt')))
263+
;
264+
265+
$crontabRepository = new CrontabRepository($fakeCrontabAdapter);
266+
267+
/* Modify the existing job */
268+
$crontabJobs = $crontabRepository->findJobByRegex('/launch\ -param\ mycommand/');
269+
$crontabRepository->addJob($crontabJobs[0]);
270+
}
194271
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Edit this file to introduce tasks to be run by cron.
2+
#
3+
# Each task to run has to be defined through a single line
4+
# indicating with different fields when the task will be run
5+
# and what command to run for the task
6+
#
7+
# To define the time you can provide concrete values for
8+
# minute (m), hour (h), day of month (dom), month (mon),
9+
# and day of week (dow) or use '*' in these fields (for 'any').#
10+
# Notice that tasks will be started based on the cron's system
11+
# daemon's notion of time and timezones.
12+
#
13+
# Output of the crontab jobs (including errors) is sent through
14+
# email to the user the crontab file belongs to (unless redirected).
15+
#
16+
# For example, you can run a backup of all your user accounts
17+
# at 5 a.m every week with:
18+
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
19+
#
20+
# For more information see the manual pages of crontab(5) and cron(8)
21+
#
22+
# m h dom mon dow command
23+
24+
30 23 * * * df >> /tmp/df.log #new crontab job

0 commit comments

Comments
 (0)