1111use GT \Cron \CronException ;
1212use GT \Cron \CrontabNotFoundException ;
1313use GT \Cron \FunctionExecutionException ;
14+ use GT \Cron \JobNotFoundException ;
15+ use GT \Cron \Runner ;
1416use GT \Cron \RunnerFactory ;
1517use 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 }
0 commit comments