Skip to content

Commit edbb053

Browse files
committed
feature: implement --now with job name
closes #80
1 parent 70367da commit edbb053

4 files changed

Lines changed: 226 additions & 84 deletions

File tree

src/Cli/RunCommand.php

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use GT\Cron\CronException;
1212
use GT\Cron\CrontabNotFoundException;
1313
use GT\Cron\FunctionExecutionException;
14+
use GT\Cron\JobNotFoundException;
15+
use GT\Cron\Runner;
1416
use GT\Cron\RunnerFactory;
1517
use GT\Cron\ScriptExecutionException;
1618

@@ -50,9 +52,9 @@ public function run(?ArgumentValueList $arguments = null):int {
5052

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

53-
if($arguments->contains("now")) {
54-
$numRunJobs = $runner->runAll();
55-
$this->stream->writeLine("Ran $numRunJobs jobs now.");
55+
$nowStatusCode = $this->runNowJobs($runner, $arguments);
56+
if(!is_null($nowStatusCode)) {
57+
return $nowStatusCode;
5658
}
5759

5860
try {
@@ -76,6 +78,60 @@ public function run(?ArgumentValueList $arguments = null):int {
7678
return 0;
7779
}
7880

81+
private function runNowJobs(
82+
Runner $runner,
83+
ArgumentValueList $arguments
84+
):?int {
85+
if(!$arguments->contains("now")) {
86+
return null;
87+
}
88+
89+
$jobName = $arguments->get("now")->get();
90+
try {
91+
if($jobName) {
92+
$numRunJobs = $this->runNamedJobNow($runner, $jobName);
93+
$this->stream->writeLine(
94+
"Ran $numRunJobs "
95+
. ($numRunJobs === 1 ? "job" : "jobs")
96+
. " now."
97+
);
98+
99+
return $arguments->contains("watch") ? null : 0;
100+
}
101+
102+
$numRunJobs = $runner->runAll();
103+
$this->stream->writeLine("Ran $numRunJobs jobs now.");
104+
}
105+
catch(JobNotFoundException $exception) {
106+
$this->stream->writeLine(
107+
$exception->getMessage(),
108+
Stream::ERROR
109+
);
110+
return 2;
111+
}
112+
113+
return null;
114+
}
115+
116+
private function runNamedJobNow(
117+
Runner $runner,
118+
string $jobName,
119+
):int {
120+
$jobName = trim($jobName);
121+
$numRunJobs = $runner->runMatching(
122+
fn(string $command):bool => $this->displayCommandName($command)
123+
=== $jobName
124+
);
125+
126+
if(!$numRunJobs) {
127+
throw new JobNotFoundException(
128+
"No cron job found matching \"$jobName\" in crontab."
129+
);
130+
}
131+
132+
return $numRunJobs;
133+
}
134+
79135
/**
80136
* @param array<string> $runCommandList
81137
* @SuppressWarnings(PHPMD.ExitExpression)
@@ -140,6 +196,9 @@ protected function displayCommandName(string $command):string {
140196
}
141197

142198
$script = preg_split("/\\s+/", $command, 2)[0];
199+
[$script] = explode("?", $script, 2);
200+
$script = preg_replace("/\\.php$/i", "", $script);
201+
143202
return basename(str_replace("\\", "/", $script));
144203
}
145204

@@ -262,7 +321,7 @@ public function getOptionalParameterList():array {
262321
true,
263322
"now",
264323
"n",
265-
"Run all tasks once now. Useful when using --watch for when developing locally."
324+
"Run all tasks once now, or pass a job name to run only that task. Useful when using --watch for when developing locally." // phpcs:ignore Generic.Files.LineLength.TooLong
266325
)
267326
];
268327
}

src/Queue.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@ public function runDueJobsAndGetCommands():array {
6565
return $commandList;
6666
}
6767

68-
public function runAllJobs():int {
68+
/** @param null|callable(string):bool $matches */
69+
public function runAllJobs(?callable $matches = null):int {
6970
$jobsRan = 0;
7071

7172
foreach($this->jobList as $job) {
73+
if($matches && !$matches($job->getCommand())) {
74+
continue;
75+
}
76+
7277
$job->run();
7378
$jobsRan++;
7479
}

src/Runner.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace GT\Cron;
33

44
use DateTime;
5-
use InvalidArgumentException;
65

76
class Runner {
87
/** @var bool */
@@ -75,4 +74,9 @@ public function run(bool $continue = false):int {
7574
public function runAll():int {
7675
return $this->queue->runAllJobs();
7776
}
77+
78+
/** @param callable(string):bool $matches */
79+
public function runMatching(callable $matches):int {
80+
return $this->queue->runAllJobs($matches);
81+
}
7882
}

0 commit comments

Comments
 (0)