4
4
5
5
use Illuminate \Console \Command ;
6
6
use Illuminate \Support \Carbon ;
7
+ use Illuminate \Support \Str ;
8
+ use Symfony \Component \Process \Process ;
7
9
8
10
class ScheduleWorkCommand extends Command
9
11
{
@@ -30,12 +32,38 @@ public function handle()
30
32
{
31
33
$ this ->info ('Schedule worker started successfully. ' );
32
34
35
+ [$ lastExecutionStartedAt , $ keyOfLastExecutionWithOutput , $ executions ] = [null , null , []];
36
+
33
37
while (true ) {
34
- if (Carbon::now ()->second === 0 ) {
35
- $ this ->call ('schedule:run ' );
38
+ usleep (100 * 1000 );
39
+
40
+ if (Carbon::now ()->second === 0 &&
41
+ ! Carbon::now ()->startOfMinute ()->equalTo ($ lastExecutionStartedAt )) {
42
+ $ executions [] = $ execution = new Process ([PHP_BINARY , 'artisan ' , 'schedule:run ' ]);
43
+
44
+ $ execution ->start ();
45
+
46
+ $ lastExecutionStartedAt = Carbon::now ()->startOfMinute ();
36
47
}
37
48
38
- sleep (1 );
49
+ foreach ($ executions as $ key => $ execution ) {
50
+ $ output = trim ($ execution ->getIncrementalOutput ()).
51
+ trim ($ execution ->getIncrementalErrorOutput ());
52
+
53
+ if (! empty ($ output )) {
54
+ if ($ key !== $ keyOfLastExecutionWithOutput ) {
55
+ $ this ->info (PHP_EOL .'Execution # ' .($ key + 1 ).' output: ' );
56
+
57
+ $ keyOfLastExecutionWithOutput = $ key ;
58
+ }
59
+
60
+ $ this ->output ->writeln ($ output );
61
+ }
62
+
63
+ if (! $ execution ->isRunning ()) {
64
+ unset($ executions [$ key ]);
65
+ }
66
+ }
39
67
}
40
68
}
41
69
}
0 commit comments