-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppkeepProvider.php
102 lines (83 loc) · 2.88 KB
/
AppkeepProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace Appkeep\Laravel;
use Illuminate\Support\ServiceProvider;
use Appkeep\Laravel\Commands\RunCommand;
use Illuminate\Console\Scheduling\Event;
use Appkeep\Laravel\Commands\InitCommand;
use Appkeep\Laravel\Commands\ListCommand;
use Appkeep\Laravel\Commands\LoginCommand;
use Illuminate\Console\Scheduling\Schedule;
use Appkeep\Laravel\Commands\PostDeployCommand;
use Appkeep\Laravel\Concerns\RegistersDefaultChecks;
class AppkeepProvider extends ServiceProvider
{
use RegistersDefaultChecks;
/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/appkeep.php', 'appkeep');
$this->app->singleton('appkeep', function () {
return new AppkeepService();
});
$this->app->bind(HttpClient::class, function () {
return new HttpClient(config('appkeep.key'));
});
if ($this->app->runningInConsole()) {
$this->registerDefaultChecks();
}
}
/**
* Bootstrap the application services.
*/
public function boot()
{
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}
public function bootForConsole()
{
$this->publishes([
__DIR__ . '/../config/appkeep.php' => config_path('appkeep.php'),
], 'config');
$this->publishes([
__DIR__ . '/../config/appkeep.php' => config_path('appkeep.php'),
], 'config');
$this->commands([
RunCommand::class,
ListCommand::class,
InitCommand::class,
LoginCommand::class,
PostDeployCommand::class,
]);
$this->app->booted(function () {
$schedule = $this->app->make(Schedule::class);
$schedule->command('appkeep:run')
->everyMinute()
->runInBackground()
->evenInMaintenanceMode();
$this->watchScheduledTasks($schedule);
});
}
protected function watchScheduledTasks(Schedule $schedule)
{
collect($schedule->events())
->filter(function ($event) {
logger($event->command);
// Don't monitor the Appkeep scheduled task itself.
return $event->command && ! str_contains($event->command, 'appkeep:run');
})
->each(function (Event $event) {
/**
* @var AppkeepService
*/
$appkeep = app('appkeep');
$event->before(fn () => $appkeep->scheduledTaskStarted($event));
$event->onSuccessWithOutput(fn () => $appkeep->scheduledTaskCompleted($event));
$event->onFailureWithOutput(fn () => $appkeep->scheduledTaskFailed($event));
});
}
}