generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished readme, facades and listeners.
- Loading branch information
Marlon Basten
committed
Jan 29, 2023
1 parent
367a941
commit 0073282
Showing
12 changed files
with
187 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
<?php | ||
|
||
// config for Marlonbasten/OhdearCroncheckLaravel | ||
return [ | ||
|
||
// If false, cronjobs will always be sent to OhDear | ||
'only_in_prod' => true, | ||
// The ping URL for OhDear (https://ohdear.app/docs/features/cron-job-monitoring#php) | ||
'ohdear_url' => 'https://ping.ohdear.app', | ||
]; |
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,17 @@ | ||
<?php | ||
|
||
namespace Marlonbasten\OhdearCroncheckLaravel\Exception; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
class ProvidedClassIsNotACommandException extends Exception | ||
{ | ||
public function __construct( | ||
string $message = 'The provided class is not a command.', | ||
int $code = 0, | ||
?Throwable $previous = null | ||
) { | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Marlonbasten\OhdearCroncheckLaravel\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @mixin \Marlonbasten\OhdearCroncheckLaravel\OhDearCron | ||
*/ | ||
class OhDearCron extends Facade | ||
{ | ||
protected static function getFacadeAccessor(): string | ||
{ | ||
return \Marlonbasten\OhdearCroncheckLaravel\OhDearCron::class; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
<?php | ||
|
||
namespace Marlonbasten\OhdearCroncheckLaravel\Listeners; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Console\Events\CommandFinished; | ||
use Illuminate\Http\Client\RequestException; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Facades\Log; | ||
use Marlonbasten\OhdearCroncheckLaravel\Facades\OhDearCron; | ||
|
||
/** | ||
* @interal | ||
*/ | ||
class CheckOhDearCommandListener | ||
{ | ||
public function handle(CommandFinished $event): void | ||
{ | ||
$command = OhDearCron::getCommand($event->command); | ||
if (! $command) { | ||
return; | ||
} | ||
|
||
$runtime = null; | ||
if ($command['started'] instanceof Carbon) { | ||
$runtime = $command['started']->diffInSeconds(now()); | ||
} | ||
|
||
$data = collect([ | ||
'exit_code' => $event->exitCode, | ||
'runtime' => $runtime, | ||
])->reject(fn ($value) => $value === null)->toArray(); | ||
|
||
try { | ||
Http::post(config('ohdear-croncheck-laravel.ohdear_url').'/'.$command['id'], $data)->throw(); | ||
} catch (RequestException $e) { | ||
Log::error('Could not send cronjob request: '.$e->getMessage()); | ||
} | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace Marlonbasten\OhdearCroncheckLaravel\Listeners; | ||
|
||
use Illuminate\Console\Events\CommandStarting; | ||
use Marlonbasten\OhdearCroncheckLaravel\Facades\OhDearCron; | ||
|
||
/** | ||
* @interal | ||
*/ | ||
class OhDearCommandStartListener | ||
{ | ||
public function handle(CommandStarting $event): void | ||
{ | ||
OhDearCron::setStartedTime($event->command, now()); | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace Marlonbasten\OhdearCroncheckLaravel; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
use Marlonbasten\OhdearCroncheckLaravel\Exception\ProvidedClassIsNotACommandException; | ||
|
||
class OhDearCron | ||
{ | ||
protected array $registered = []; | ||
|
||
/** | ||
* @param class-string $className | ||
* @param string $id | ||
* @return void | ||
* | ||
* @throws ProvidedClassIsNotACommandException | ||
*/ | ||
public function register(string $className, string $id): void | ||
{ | ||
$commandClass = new $className; | ||
|
||
if (! $commandClass instanceof Command) { | ||
throw new ProvidedClassIsNotACommandException(); | ||
} | ||
|
||
$this->registered[$commandClass->getName()] = [ | ||
'id' => $id, | ||
'started' => null, | ||
]; | ||
} | ||
|
||
public function getRegisteredCommands(): array | ||
{ | ||
return $this->registered; | ||
} | ||
|
||
public function getCommand(string $commandName): ?array | ||
{ | ||
return $this->registered[$commandName] ?? null; | ||
} | ||
|
||
public function setStartedTime(string $commandName, Carbon $time): void | ||
{ | ||
if (! $this->getCommand($commandName)) { | ||
return; | ||
} | ||
|
||
$this->registered[$commandName]['started'] = $time; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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