Skip to content

Commit

Permalink
Comment-only (empty) crontabs do not break (#13)
Browse files Browse the repository at this point in the history
* Update dependencies

* Don't test the language for type safety

* Isolate bug #10

* Typo

* Skip comments earlier
Fix #10

* No need to double check line length

* Update Cli release

* Improve type safety
  • Loading branch information
g105b authored Feb 15, 2021
1 parent 324bb40 commit a2eb615
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 53 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Run scripts or static functions at regular intervals.",

"require": {
"php": ">=7.2",
"php": ">=7.4",
"phpgt/cli": "1.*",
"dragonmantank/cron-expression": "^2.2"
},
Expand Down
76 changes: 45 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function cronRunStep(
int $jobsRan,
?DateTime $wait,
bool $continue
) {
):void {
$message = "";
$now = new DateTime();

Expand Down Expand Up @@ -157,4 +157,4 @@ public function getOptionalParameterList():array {
)
];
}
}
}
12 changes: 6 additions & 6 deletions src/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use DateTime;

class Job {
protected $expression;
protected $command;
protected $hasRun;
protected CronExpression $expression;
protected string $command;
protected bool $hasRun;

public function __construct(CronExpression $factory, string $command) {
$this->expression = $factory;
public function __construct(CronExpression $expression, string $command) {
$this->expression = $expression;
$this->command = $command;
$this->hasRun = false;
}
Expand Down Expand Up @@ -133,4 +133,4 @@ protected function executeScript():void {
proc_close($proc);
}
}
}
}
4 changes: 2 additions & 2 deletions src/JobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Cron\CronExpression;

class JobRepository {
public function create(CronExpression $expression, $command):Job {
public function create(CronExpression $expression, string $command):Job {
return new Job($expression, $command);
}
}
}
4 changes: 2 additions & 2 deletions src/QueueRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use DateTime;

class QueueRepository {
protected $className;
protected string $className;

public function __construct(string $className = Queue::class) {
$this->className = $className;
Expand All @@ -13,4 +13,4 @@ public function __construct(string $className = Queue::class) {
public function createAtTime(DateTime $now):Queue {
return new $this->className($now);
}
}
}
11 changes: 5 additions & 6 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function __construct(
continue;
}

if($line[0] === "#") {
continue;
}

preg_match(
"/(?P<crontab>\S+\s\S+\s\S+\s\S+\s\S+)\s(?P<command>.+)/",
$line,
Expand All @@ -46,11 +50,6 @@ public function __construct(
$crontab = trim($crontab);
$command = trim($command);

if(strlen($crontab) > 0
&& $crontab[0] === "#") {
continue;
}

try {
$job = call_user_func(
[$jobRepository, "create"],
Expand Down Expand Up @@ -107,4 +106,4 @@ public function run(bool $continue = false):int {
public function runAll():int {
return $this->queue->runAllJobs();
}
}
}
3 changes: 1 addition & 2 deletions test/phpunit/JobRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function testCreate() {
$command
);

self::assertInstanceOf(Job::class, $job);
self::assertEquals($command, $job->getCommand());
}

Expand All @@ -25,4 +24,4 @@ protected function mockExpression():CronExpression {
/** @var CronExpression $expression */
return $expression;
}
}
}
16 changes: 15 additions & 1 deletion test/phpunit/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ public function testComments() {
self::assertEquals(2, $runner->getNumJobs());
}

public function testOnlyComments() {
$cronContents = <<<CRON
# List your background tasks here in normal crontab syntax
# and they will be run with `gt run`
# More info: https://www.php.gt/cron
CRON;
$runner = new Runner(
$this->mockJobRepository(0),
$this->mockQueueRepository(0),
$cronContents
);
self::assertEquals(0, $runner->getNumJobs());
}

protected function mockJobRepository(int...$wait):JobRepository {
$isDue = [];
$runDate = [];
Expand Down Expand Up @@ -319,4 +333,4 @@ protected function mockQueueRepository(int...$wait):QueueRepository {
/** @var QueueRepository $repository */
return $repository;
}
}
}

0 comments on commit a2eb615

Please sign in to comment.