Skip to content

Commit

Permalink
Finished readme, facades and listeners.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marlon Basten committed Jan 29, 2023
1 parent 367a941 commit 0073282
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 60 deletions.
50 changes: 22 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/marlonbasten/ohdear-croncheck-laravel/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/marlonbasten/ohdear-croncheck-laravel/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/marlonbasten/ohdear-croncheck-laravel.svg?style=flat-square)](https://packagist.org/packages/marlonbasten/ohdear-croncheck-laravel)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
Monitoring cronjobs with OhDear made easy! Just register your commands in a service provider.

## Support us

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/ohdear-croncheck-laravel.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/ohdear-croncheck-laravel)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
```php
public function boot(): void
{
OhDearCron::register(TestCommand::class, 'id');
}
```

## Installation

Expand All @@ -23,13 +22,6 @@ You can install the package via composer:
composer require marlonbasten/ohdear-croncheck-laravel
```

You can publish and run the migrations with:

```bash
php artisan vendor:publish --tag="ohdear-croncheck-laravel-migrations"
php artisan migrate
```

You can publish the config file with:

```bash
Expand All @@ -40,27 +32,30 @@ This is the contents of the published config file:

```php
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',
];
```

Optionally, you can publish the views using

```bash
php artisan vendor:publish --tag="ohdear-croncheck-laravel-views"
```

## Usage

Register the command you want to monitor with OhDear in any service provider using the Facade.

```php
$ohdearCroncheckLaravel = new Marlonbasten\OhdearCroncheckLaravel();
echo $ohdearCroncheckLaravel->echoPhrase('Hello, Marlonbasten!');
public function boot(): void
{
\Marlonbasten\OhdearCroncheckLaravel\Facades\OhDearCron::register(TestCommand::class, 'id');
}
```

## Testing
The `id` is the last part of your ping URL you get on OhDear.

```bash
composer test
```
![img.png](img.png)

<b>That's it!</b> Now every time the command runs, OhDear will receive a ping.<br />
The package will also send the exit code and the runtime of the command to OhDear.

## Changelog

Expand All @@ -77,7 +72,6 @@ Please review [our security policy](../../security/policy) on how to report secu
## Credits

- [Marlon Basten](https://github.com/marlonbasten)
- [All Contributors](../../contributors)

## License

Expand Down
6 changes: 4 additions & 2 deletions config/ohdear-croncheck-laravel.php
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',
];
Binary file added img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions src/Exception/ProvidedClassIsNotACommandException.php
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);
}
}
16 changes: 16 additions & 0 deletions src/Facades/OhDearCron.php
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;
}
}
16 changes: 0 additions & 16 deletions src/Facades/OhdearCroncheckLaravel.php

This file was deleted.

40 changes: 40 additions & 0 deletions src/Listeners/CheckOhDearCommandListener.php
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());
}
}
}
17 changes: 17 additions & 0 deletions src/Listeners/OhDearCommandStartListener.php
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());
}
}
52 changes: 52 additions & 0 deletions src/OhDearCron.php
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;
}
}
7 changes: 0 additions & 7 deletions src/OhdearCroncheckLaravel.php

This file was deleted.

24 changes: 18 additions & 6 deletions src/OhdearCroncheckLaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,36 @@

namespace Marlonbasten\OhdearCroncheckLaravel;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
use Marlonbasten\OhdearCroncheckLaravel\Commands\OhdearCroncheckLaravelCommand;
use Marlonbasten\OhdearCroncheckLaravel\Listeners\CheckOhDearCommandListener;
use Marlonbasten\OhdearCroncheckLaravel\Listeners\OhDearCommandStartListener;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Marlonbasten\OhdearCroncheckLaravel\Commands\OhdearCroncheckLaravelCommand;

class OhdearCroncheckLaravelServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package
->name('ohdear-croncheck-laravel')
->hasConfigFile()
->hasViews()
->hasMigration('create_ohdear-croncheck-laravel_table')
->hasCommand(OhdearCroncheckLaravelCommand::class);
}

public function packageBooted(): void
{
if (
(config('ohdear-croncheck-laravel.only_in_prod') === true && App::isProduction()) ||
config('ohdear-croncheck-laravel.only_in_prod') === false
) {
Event::listen(CommandStarting::class, [OhDearCommandStartListener::class, 'handle']);
Event::listen(CommandFinished::class, [CheckOhDearCommandListener::class, 'handle']);
}
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Marlonbasten\OhdearCroncheckLaravel\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use Marlonbasten\OhdearCroncheckLaravel\OhdearCroncheckLaravelServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
Expand Down

0 comments on commit 0073282

Please sign in to comment.