Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Support scheduled background task finished #20

Open
wants to merge 7 commits into
base: 3.x
Choose a base branch
from
Next Next commit
add support for ScheduledBackgroundTaskFinished event added in Larave…
…l 8.42
  • Loading branch information
michaeldyrynda committed Jul 10, 2021
commit ad90f244a0554ed43f24b01474c6667160604b85
20 changes: 20 additions & 0 deletions src/Payload/ScheduledBackgroundTaskFinishedPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Thenpingme\Payload;

use Illuminate\Support\Carbon;

final class ScheduledBackgroundTaskFinishedPayload extends ThenpingmePayload
{
public function toArray(): array
{
return array_merge(parent::toArray(), [
'type' => class_basename($this->event),
'time' => Carbon::now()->toIso8601String(),
'runtime' => $this->event->runtime ?? null,
'exit_code' => $this->event->task->exitCode,
'memory' => memory_get_usage(true),
'task' => TaskPayload::make($this->event->task)->toArray(),
]);
}
}
2 changes: 1 addition & 1 deletion src/Payload/ScheduledTaskFinishedPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function toArray(): array
return array_merge(parent::toArray(), [
'type' => class_basename($this->event),
'time' => Carbon::now()->toIso8601String(),
'runtime' => $this->event->runtime,
'runtime' => $this->event->runtime ?? null,
'exit_code' => $this->event->task->exitCode,
'memory' => memory_get_usage(true),
'task' => TaskPayload::make($this->event->task)->toArray(),
Expand Down
5 changes: 5 additions & 0 deletions src/Payload/ThenpingmePayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Thenpingme\Payload;

use Illuminate\Console\Events\ScheduledBackgroundTaskFinished;
use Illuminate\Console\Events\ScheduledTaskFailed;
use Illuminate\Console\Events\ScheduledTaskFinished;
use Illuminate\Console\Events\ScheduledTaskSkipped;
Expand Down Expand Up @@ -36,6 +37,10 @@ public static function fromEvent($event): ?ThenpingmePayload
return new ScheduledTaskFinishedPayload($event);
}

if ($event instanceof ScheduledBackgroundTaskFinished) {
return new ScheduledBackgroundTaskFinishedPayload($event);
}

if ($event instanceof ScheduledTaskSkipped) {
return new ScheduledTaskSkippedPayload($event);
}
Expand Down
7 changes: 7 additions & 0 deletions src/ScheduledTaskSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Thenpingme;

use Illuminate\Console\Events\ScheduledBackgroundTaskFinished;
use Illuminate\Console\Events\ScheduledTaskFailed;
use Illuminate\Console\Events\ScheduledTaskFinished;
use Illuminate\Console\Events\ScheduledTaskSkipped;
Expand Down Expand Up @@ -49,5 +50,11 @@ public function subscribe(Dispatcher $events): void
static::class.'@handleScheduledTaskEvent'
);
}

if (class_exists(ScheduledBackgroundTaskFinished::class)) {
$events->listen([
ScheduledBackgroundTaskFinished::class,
], static::class.'@handleScheduledTaskEvent');
}
}
}
2 changes: 2 additions & 0 deletions tests/ScheduledTaskListenerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Console\Events\ScheduledBackgroundTaskFinished;
use Illuminate\Console\Events\ScheduledTaskFailed;
use Illuminate\Console\Events\ScheduledTaskFinished;
use Illuminate\Console\Events\ScheduledTaskSkipped;
Expand Down Expand Up @@ -36,4 +37,5 @@
'scheduled task finished' => [ScheduledTaskFinished::class, [1]],
'scheduled task skipped' => [ScheduledTaskSkipped::class, [1]],
'scheduled task failed' => [ScheduledTaskFailed::class, [new Exception('testing')]],
'scheduled backround task finished' => [ScheduledBackgroundTaskFinished::class, []],
]);