22
33namespace macropage \LaravelSchedulerWatcher ;
44
5- use Artisan ;
65use Carbon \Carbon ;
7- use DB ;
86use Illuminate \Console \Scheduling \Event ;
97use Illuminate \Console \Scheduling \Schedule ;
108use Illuminate \Support \Collection ;
9+ use Illuminate \Support \Facades \DB ;
1110use macropage \LaravelSchedulerWatcher \Models \job_event_outputs ;
1211use macropage \LaravelSchedulerWatcher \Models \job_events ;
1312use macropage \LaravelSchedulerWatcher \Models \jobs ;
1413use Throwable ;
1514
16- trait LaravelSchedulerWatcher
15+ class LaravelSchedulerWatcher
1716{
1817
19- private array $ measure_times = [];
18+ private static array $ measure_times = [];
2019
21- public function monitor (Schedule $ schedule ): void
20+ public static function monitor (): void
2221 {
23- $ events = new Collection ($ schedule ->events ());
22+ $ schedule = app (Schedule::class);
23+ $ events = new Collection ($ schedule ->events ());
24+
2425 $ events ->each (function (Event $ event ) {
2526
2627 $ switches = [];
2728
2829 if ($ event ->description ) {
29- preg_match_all ('/.*\[(.*)]$/m ' , $ event ->description , $ matches , PREG_SET_ORDER );
30+ preg_match_all ('/.*\[(.*)]$/m ' , $ event ->description , $ matches , PREG_SET_ORDER );
3031 if (count ($ matches )) {
3132 $ switches = explode (', ' , $ matches [0 ][1 ]);
3233 }
@@ -36,7 +37,7 @@ public function monitor(Schedule $schedule): void
3637
3738 if (str_contains ($ event ->command , '\'artisan \'' )) {
3839 $ commandSplittet = explode ('\'artisan \'' , $ event ->command );
39- $ customMutexd = md5 (trim ($ commandSplittet [1 ]));
40+ $ customMutexd = md5 (trim ($ commandSplittet [1 ]));
4041 } else {
4142 $ customMutexd = md5 ($ event ->command );
4243 }
@@ -57,21 +58,21 @@ public function monitor(Schedule $schedule): void
5758
5859
5960 $ outputLogFile = sys_get_temp_dir () . '/ ' . $ customMutexd . '.scheduler.output.log ' ;
60- $ this -> measure_times [$ customMutexd ]['start ' ] = 0 ;
61- $ this -> measure_times [$ customMutexd ]['duration ' ] = 0 ;
61+ self :: $ measure_times [$ customMutexd ]['start ' ] = 0 ;
62+ self :: $ measure_times [$ customMutexd ]['duration ' ] = 0 ;
6263
6364 $ event ->before (function () use ($ customMutexd ) {
64- $ this -> measure_times [$ customMutexd ]['start ' ] = microtime (true );
65- $ this -> measure_times [$ customMutexd ]['start_date ' ] = Carbon::now ();
65+ self :: $ measure_times [$ customMutexd ]['start ' ] = microtime (true );
66+ self :: $ measure_times [$ customMutexd ]['start_date ' ] = Carbon::now ();
6667 });
6768
6869 $ event ->sendOutputTo ($ outputLogFile )->after (
6970 /**
7071 * @throws Throwable
7172 */ function () use ($ customMutexd , $ outputLogFile , $ event , $ Description , $ switches ) {
7273
73- $ this -> measure_times [$ customMutexd ]['duration ' ] = microtime (true ) - $ this -> measure_times [$ customMutexd ]['start ' ];
74- $ this -> measure_times [$ customMutexd ]['end_date ' ] = Carbon::now ();
74+ self :: $ measure_times [$ customMutexd ]['duration ' ] = microtime (true ) - self :: $ measure_times [$ customMutexd ]['start ' ];
75+ self :: $ measure_times [$ customMutexd ]['end_date ' ] = Carbon::now ();
7576
7677 if (file_exists ($ outputLogFile ) && $ logData = file_get_contents ($ outputLogFile )) {
7778 DB ::connection (config ('laravel-scheduler-watcher.mysql_connection ' ))->transaction (function () use ($ logData , $ customMutexd , $ event , $ Description , $ switches ) {
@@ -88,10 +89,10 @@ public function monitor(Schedule $schedule): void
8889 }
8990 $ jobEvent = new job_events ([
9091 'jobe_job_id ' => $ job_id ,
91- 'jobe_start ' => $ this -> measure_times [$ customMutexd ]['start_date ' ],
92- 'jobe_end ' => $ this -> measure_times [$ customMutexd ]['end_date ' ],
92+ 'jobe_start ' => self :: $ measure_times [$ customMutexd ]['start_date ' ],
93+ 'jobe_end ' => self :: $ measure_times [$ customMutexd ]['end_date ' ],
9394 'jobe_exitcode ' => ($ event ->exitCode ) ?: 0 ,
94- 'jobe_duration ' => $ this -> measure_times [$ customMutexd ]['duration ' ],
95+ 'jobe_duration ' => self :: $ measure_times [$ customMutexd ]['duration ' ],
9596 ]);
9697 $ jobEvent ->save ();
9798 if (!in_array ('nooutput ' , $ switches , true )) {
0 commit comments