Skip to content

Commit 9d90963

Browse files
author
micha
committed
update to laravel 11
1 parent 30607a7 commit 9d90963

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,24 @@ $schedule->command('dummy:test blabla -c')->everyMinute()->description('Call dum
6666
The switch "force" lets run your command, ignoring last exitcode. **be careful: this can spam your DB.**
6767
Personally I would use "force" only with "nooutput".
6868

69-
#### Kernel.php
70-
To use the logging, you have to include the Trait `LaravelSchedulerWatcher` into your `app\Console\Kernel.php`
69+
#### routes/console.php
7170
```php
7271
<?php
73-
use macropage\LaravelSchedulerWatcher\LaravelSchedulerWatcher;
72+
<?php
7473

75-
class Kernel extends ConsoleKernel {
74+
use Illuminate\Foundation\Inspiring;
75+
use Illuminate\Support\Facades\Artisan;
76+
use macropage\LaravelSchedulerWatcher\LaravelSchedulerWatcher;
7677

77-
use LaravelSchedulerWatcher;
78+
Artisan::command('inspire', function () {
79+
$this->comment(Inspiring::quote());
80+
})
81+
->description('Display an inspiring quote [log]')
82+
->hourly();
7883

79-
protected function schedule(Schedule $schedule): void {
80-
$schedule->command('dummy:test blabla -c')->everyMinute()->description('Call dummy test');
81-
$this->monitor($schedule);
82-
}
83-
}
84+
LaravelSchedulerWatcher::monitor();
8485
```
85-
Inside the `schedule` function, call the monitor. This is the place where all the magic happens ;)
86+
Somewhere in your code call the Monitor, this is the place where all the magic happens ;)
8687

8788
## Logging to File
8889
The last Output-File (the file that captured the output of your job) will be written to `/tmp/<mutex>.scheduler.output.log`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.1",
19+
"php": "^8.1 || ^8.2 || ^8.3",
2020
"macropage/php-to-ascii-table" : "dev-master",
2121
"codedungeon/php-cli-colors" : "^1.0",
2222
"macropage/ansi-to-html" : "dev-master"

src/LaravelSchedulerWatcher.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22

33
namespace macropage\LaravelSchedulerWatcher;
44

5-
use Artisan;
65
use Carbon\Carbon;
7-
use DB;
86
use Illuminate\Console\Scheduling\Event;
97
use Illuminate\Console\Scheduling\Schedule;
108
use Illuminate\Support\Collection;
9+
use Illuminate\Support\Facades\DB;
1110
use macropage\LaravelSchedulerWatcher\Models\job_event_outputs;
1211
use macropage\LaravelSchedulerWatcher\Models\job_events;
1312
use macropage\LaravelSchedulerWatcher\Models\jobs;
1413
use 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

Comments
 (0)