Skip to content

Commit

Permalink
Add --now flag to run all tasks at start, closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed May 31, 2019
1 parent 1f13f41 commit 349d478
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions bin/cron
Expand Up @@ -6,6 +6,7 @@ use Gt\Cli\Argument\CommandArgumentList;
use Gt\Cron\Command\RunCommand;

$autoloadLocations = [
getcwd() . "/vendor/autoload.php",
__DIR__ . "/../../../autoload.php",
__DIR__ . "/../vendor/autoload.php",
];
Expand Down
11 changes: 11 additions & 0 deletions src/Command/RunCommand.php
Expand Up @@ -41,6 +41,11 @@ public function run(ArgumentValueList $arguments = null):void {

$runner->setRunCallback([$this, "cronRunStep"]);

if($arguments->contains("now")) {
$numRunJobs = $runner->runAll();
$this->output->writeLine("Ran $numRunJobs jobs now.");
}

try {
$runner->run($arguments->contains("watch"));
}
Expand Down Expand Up @@ -144,6 +149,12 @@ public function getOptionalParameterList():array {
null,
"Check the syntax of the crontab file without running anything."
),
new Parameter(
true,
"now",
"n",
"Run all tasks once now. Useful when using --watch for when developing locally."
)
];
}
}
6 changes: 4 additions & 2 deletions src/Job.php
Expand Up @@ -89,10 +89,12 @@ protected function executeFunction():void {
$command = trim($command);
}

if(!is_callable($command)) {
$callable = explode("::", $command);

if(!is_callable($callable)) {
throw new FunctionExecutionException($command);
}
call_user_func_array($command, $args);
call_user_func_array($callable, $args);
}

protected function executeScript():void {
Expand Down
11 changes: 11 additions & 0 deletions src/Queue.php
Expand Up @@ -50,6 +50,17 @@ public function runDueJobs():int {
return $jobsRan;
}

public function runAllJobs():int {
$jobsRan = 0;

foreach($this->jobList as $job) {
$job->run();
$jobsRan++;
}

return $jobsRan;
}

public function reset():void {
foreach($this->jobList as $job) {
$job->resetRunFlag();
Expand Down
4 changes: 4 additions & 0 deletions src/Runner.php
Expand Up @@ -103,4 +103,8 @@ public function run(bool $continue = false):int {

return $jobsRan;
}

public function runAll():int {
return $this->queue->runAllJobs();
}
}

0 comments on commit 349d478

Please sign in to comment.