-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report Scheduled Task outputs to Appkeep (#23)
* fix memory check * update version * add new event to report cronjob outputs * don't report appkeep:run output * add start at/finish at timestamps * refactor * fixes * update guzzle version * fix guzzle
- Loading branch information
Showing
11 changed files
with
217 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Appkeep\Laravel\Concerns; | ||
|
||
use Appkeep\Laravel\ScheduledTaskOutput; | ||
use Illuminate\Console\Scheduling\Event; | ||
|
||
trait ReportsScheduledTaskOutputs | ||
{ | ||
private $scheduledTaskStartMs; | ||
private $scheduledTaskStartedAt; | ||
|
||
public function scheduledTaskStarted(Event $task) | ||
{ | ||
$this->scheduledTaskStartMs = hrtime(true); | ||
$this->scheduledTaskStartedAt = now(); | ||
} | ||
|
||
/** | ||
* Get the duration of the scheduled task run in milliseconds | ||
*/ | ||
private function getScheduledTaskRunDuration(): int | ||
{ | ||
return round((hrtime(true) - $this->scheduledTaskStartMs) / 1e+6); | ||
} | ||
|
||
public function scheduledTaskFailed(Event $task, $output) | ||
{ | ||
$duration = $this->getScheduledTaskRunDuration(); | ||
$finishedAt = now(); | ||
|
||
$output = ScheduledTaskOutput::fromScheduledTask($task) | ||
->failed() | ||
->setDuration($duration) | ||
->setStartedAt($this->scheduledTaskStartedAt) | ||
->setFinishedAt($finishedAt) | ||
->setOutput($output); | ||
|
||
try { | ||
$this->client()->sendScheduledTaskOutput($output); | ||
} catch (\Exception $e) { | ||
report($e); | ||
} | ||
} | ||
|
||
public function scheduledTaskCompleted(Event $task, $output) | ||
{ | ||
$duration = $this->getScheduledTaskRunDuration(); | ||
$finishedAt = now(); | ||
|
||
$output = ScheduledTaskOutput::fromScheduledTask($task) | ||
->succeeded() | ||
->setDuration($duration) | ||
->setStartedAt($this->scheduledTaskStartedAt) | ||
->setFinishedAt($finishedAt) | ||
->setOutput($output); | ||
|
||
try { | ||
$this->client()->sendScheduledTaskOutput($output); | ||
} catch (\Exception $e) { | ||
report($e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Appkeep\Laravel\Events; | ||
|
||
use Appkeep\Laravel\ScheduledTaskOutput; | ||
|
||
class ScheduledTaskEvent extends AbstractEvent | ||
{ | ||
protected $name = 'scheduled-task'; | ||
|
||
public function __construct(private ScheduledTaskOutput $output) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return array_merge( | ||
parent::toArray(), | ||
[ | ||
'output' => [ | ||
'id' => $this->output->id, | ||
'success' => $this->output->success, | ||
'duration' => $this->output->duration, | ||
'output' => $this->output->output, | ||
'started_at' => $this->output->startedAt, | ||
'finished_at' => $this->output->finishedAt, | ||
], | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Appkeep\Laravel; | ||
|
||
use DateTime; | ||
use Illuminate\Console\Scheduling\Event; | ||
use Appkeep\Laravel\Support\ScheduledTaskId; | ||
|
||
class ScheduledTaskOutput | ||
{ | ||
public bool $success = true; | ||
public ?float $duration = null; | ||
public ?string $output = null; | ||
public ?DateTime $startedAt = null; | ||
public ?DateTime $finishedAt = null; | ||
|
||
public function __construct(public string $id) | ||
{ | ||
} | ||
|
||
public static function fromScheduledTask(Event $task) | ||
{ | ||
return new self( | ||
ScheduledTaskId::get($task) | ||
); | ||
} | ||
|
||
public function succeeded() | ||
{ | ||
$this->success = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function failed() | ||
{ | ||
$this->success = false; | ||
|
||
return $this; | ||
} | ||
|
||
public function setDuration(float $duration) | ||
{ | ||
$this->duration = $duration; | ||
|
||
return $this; | ||
} | ||
|
||
public function setOutput(string $output) | ||
{ | ||
$this->output = $output; | ||
|
||
return $this; | ||
} | ||
|
||
public function setStartedAt(DateTime $startedAt) | ||
{ | ||
$this->startedAt = $startedAt; | ||
|
||
return $this; | ||
} | ||
|
||
public function setFinishedAt(DateTime $finishedAt) | ||
{ | ||
$this->finishedAt = $finishedAt; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters