2
2
3
3
namespace macropage \LaravelSchedulerWatcher ;
4
4
5
- use Artisan ;
6
5
use Carbon \Carbon ;
7
- use DB ;
8
6
use Illuminate \Console \Scheduling \Event ;
9
7
use Illuminate \Console \Scheduling \Schedule ;
10
8
use Illuminate \Support \Collection ;
9
+ use Illuminate \Support \Facades \DB ;
11
10
use macropage \LaravelSchedulerWatcher \Models \job_event_outputs ;
12
11
use macropage \LaravelSchedulerWatcher \Models \job_events ;
13
12
use macropage \LaravelSchedulerWatcher \Models \jobs ;
14
13
use Throwable ;
15
14
16
- trait LaravelSchedulerWatcher
15
+ class LaravelSchedulerWatcher
17
16
{
18
17
19
- private array $ measure_times = [];
18
+ private static array $ measure_times = [];
20
19
21
- public function monitor (Schedule $ schedule ): void
20
+ public static function monitor (): void
22
21
{
23
- $ events = new Collection ($ schedule ->events ());
22
+ $ schedule = app (Schedule::class);
23
+ $ events = new Collection ($ schedule ->events ());
24
+
24
25
$ events ->each (function (Event $ event ) {
25
26
26
27
$ switches = [];
27
28
28
29
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 );
30
31
if (count ($ matches )) {
31
32
$ switches = explode (', ' , $ matches [0 ][1 ]);
32
33
}
@@ -36,7 +37,7 @@ public function monitor(Schedule $schedule): void
36
37
37
38
if (str_contains ($ event ->command , '\'artisan \'' )) {
38
39
$ commandSplittet = explode ('\'artisan \'' , $ event ->command );
39
- $ customMutexd = md5 (trim ($ commandSplittet [1 ]));
40
+ $ customMutexd = md5 (trim ($ commandSplittet [1 ]));
40
41
} else {
41
42
$ customMutexd = md5 ($ event ->command );
42
43
}
@@ -57,21 +58,21 @@ public function monitor(Schedule $schedule): void
57
58
58
59
59
60
$ 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 ;
62
63
63
64
$ 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 ();
66
67
});
67
68
68
69
$ event ->sendOutputTo ($ outputLogFile )->after (
69
70
/**
70
71
* @throws Throwable
71
72
*/ function () use ($ customMutexd , $ outputLogFile , $ event , $ Description , $ switches ) {
72
73
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 ();
75
76
76
77
if (file_exists ($ outputLogFile ) && $ logData = file_get_contents ($ outputLogFile )) {
77
78
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
88
89
}
89
90
$ jobEvent = new job_events ([
90
91
'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 ' ],
93
94
'jobe_exitcode ' => ($ event ->exitCode ) ?: 0 ,
94
- 'jobe_duration ' => $ this -> measure_times [$ customMutexd ]['duration ' ],
95
+ 'jobe_duration ' => self :: $ measure_times [$ customMutexd ]['duration ' ],
95
96
]);
96
97
$ jobEvent ->save ();
97
98
if (!in_array ('nooutput ' , $ switches , true )) {
0 commit comments