Skip to content

Commit

Permalink
update to laravel 11
Browse files Browse the repository at this point in the history
  • Loading branch information
micha committed May 14, 2024
1 parent 30607a7 commit 9d90963
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,24 @@ $schedule->command('dummy:test blabla -c')->everyMinute()->description('Call dum
The switch "force" lets run your command, ignoring last exitcode. **be careful: this can spam your DB.**
Personally I would use "force" only with "nooutput".

#### Kernel.php
To use the logging, you have to include the Trait `LaravelSchedulerWatcher` into your `app\Console\Kernel.php`
#### routes/console.php
```php
<?php
use macropage\LaravelSchedulerWatcher\LaravelSchedulerWatcher;
<?php

class Kernel extends ConsoleKernel {
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use macropage\LaravelSchedulerWatcher\LaravelSchedulerWatcher;

use LaravelSchedulerWatcher;
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})
->description('Display an inspiring quote [log]')
->hourly();

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

## Logging to File
The last Output-File (the file that captured the output of your job) will be written to `/tmp/<mutex>.scheduler.output.log`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.1 || ^8.2 || ^8.3",
"macropage/php-to-ascii-table" : "dev-master",
"codedungeon/php-cli-colors" : "^1.0",
"macropage/ansi-to-html" : "dev-master"
Expand Down
35 changes: 18 additions & 17 deletions src/LaravelSchedulerWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

namespace macropage\LaravelSchedulerWatcher;

use Artisan;
use Carbon\Carbon;
use DB;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use macropage\LaravelSchedulerWatcher\Models\job_event_outputs;
use macropage\LaravelSchedulerWatcher\Models\job_events;
use macropage\LaravelSchedulerWatcher\Models\jobs;
use Throwable;

trait LaravelSchedulerWatcher
class LaravelSchedulerWatcher
{

private array $measure_times = [];
private static array $measure_times = [];

public function monitor(Schedule $schedule): void
public static function monitor(): void
{
$events = new Collection($schedule->events());
$schedule = app(Schedule::class);
$events = new Collection($schedule->events());

$events->each(function (Event $event) {

$switches = [];

if ($event->description) {
preg_match_all('/.*\[(.*)]$/m', $event->description, $matches, PREG_SET_ORDER );
preg_match_all('/.*\[(.*)]$/m', $event->description, $matches, PREG_SET_ORDER);
if (count($matches)) {
$switches = explode(',', $matches[0][1]);
}
Expand All @@ -36,7 +37,7 @@ public function monitor(Schedule $schedule): void

if (str_contains($event->command, '\'artisan\'')) {
$commandSplittet = explode('\'artisan\'', $event->command);
$customMutexd = md5(trim($commandSplittet[1]));
$customMutexd = md5(trim($commandSplittet[1]));
} else {
$customMutexd = md5($event->command);
}
Expand All @@ -57,21 +58,21 @@ public function monitor(Schedule $schedule): void


$outputLogFile = sys_get_temp_dir() . '/' . $customMutexd . '.scheduler.output.log';
$this->measure_times[$customMutexd]['start'] = 0;
$this->measure_times[$customMutexd]['duration'] = 0;
self::$measure_times[$customMutexd]['start'] = 0;
self::$measure_times[$customMutexd]['duration'] = 0;

$event->before(function () use ($customMutexd) {
$this->measure_times[$customMutexd]['start'] = microtime(true);
$this->measure_times[$customMutexd]['start_date'] = Carbon::now();
self::$measure_times[$customMutexd]['start'] = microtime(true);
self::$measure_times[$customMutexd]['start_date'] = Carbon::now();
});

$event->sendOutputTo($outputLogFile)->after(
/**
* @throws Throwable
*/ function () use ($customMutexd, $outputLogFile, $event, $Description, $switches) {

$this->measure_times[$customMutexd]['duration'] = microtime(true) - $this->measure_times[$customMutexd]['start'];
$this->measure_times[$customMutexd]['end_date'] = Carbon::now();
self::$measure_times[$customMutexd]['duration'] = microtime(true) - self::$measure_times[$customMutexd]['start'];
self::$measure_times[$customMutexd]['end_date'] = Carbon::now();

if (file_exists($outputLogFile) && $logData = file_get_contents($outputLogFile)) {
DB::connection(config('laravel-scheduler-watcher.mysql_connection'))->transaction(function () use ($logData, $customMutexd, $event, $Description, $switches) {
Expand All @@ -88,10 +89,10 @@ public function monitor(Schedule $schedule): void
}
$jobEvent = new job_events([
'jobe_job_id' => $job_id,
'jobe_start' => $this->measure_times[$customMutexd]['start_date'],
'jobe_end' => $this->measure_times[$customMutexd]['end_date'],
'jobe_start' => self::$measure_times[$customMutexd]['start_date'],
'jobe_end' => self::$measure_times[$customMutexd]['end_date'],
'jobe_exitcode' => ($event->exitCode) ?: 0,
'jobe_duration' => $this->measure_times[$customMutexd]['duration'],
'jobe_duration' => self::$measure_times[$customMutexd]['duration'],
]);
$jobEvent->save();
if (!in_array('nooutput', $switches, true)) {
Expand Down

0 comments on commit 9d90963

Please sign in to comment.