Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a message about setting up the cron and if there was an error ru… #425

Merged
merged 1 commit into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@
use App\Console\Cron\Monthly;
use App\Console\Cron\Nightly;
use App\Console\Cron\Weekly;
use App\Services\CronService;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
private $cronSvc;

public function __construct(Application $app, Dispatcher $events)
{
parent::__construct($app, $events);
$this->cronSvc = app(CronService::class);
}

/**
* Define the application's command schedule.
*
Expand All @@ -28,6 +39,9 @@ protected function schedule(Schedule $schedule): void
// When spatie-backups runs
$schedule->command('backup:clean')->daily()->at('01:00');
$schedule->command('backup:run')->daily()->at('02:00');

// Update the last time the cron was run
$this->cronSvc->updateLastRunTime();
}

/**
Expand Down
18 changes: 16 additions & 2 deletions app/Http/Controllers/Admin/MaintenanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@
namespace App\Http\Controllers\Admin;

use App\Contracts\Controller;
use App\Services\CronService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Laracasts\Flash\Flash;

class MaintenanceController extends Controller
{
public function __construct()
private $cronSvc;

public function __construct(CronService $cronSvc)
{
$this->cronSvc = $cronSvc;
}

public function index()
{
return view('admin.maintenance.index');
// Generate the cron path. Replace php-fpm with just php
$cron_path = [
'* * * * *',
$this->cronSvc->getCronPath(),
'>> /dev/null 2>&1',
];

return view('admin.maintenance.index', [
'cron_path' => implode(' ', $cron_path),
'cron_problem_exists' => $this->cronSvc->cronProblemExists(),
]);
}

/**
Expand Down
73 changes: 73 additions & 0 deletions app/Services/CronService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Services;

use App\Contracts\Service;
use App\Repositories\KvpRepository;
use DateTime;
use DateTimeZone;
use Exception;
use Illuminate\Support\Facades\Log;

class CronService extends Service
{
private $kvpRepo;

public function __construct(
KvpRepository $kvpRepo
) {
$this->kvpRepo = $kvpRepo;
}

/**
* Get the path for running a cron job
*
* @return string
*/
public function getCronPath(): string
{
$path = [
'cd '.base_path(),
'&&',
str_replace('-fpm', '', PHP_BINARY),
'artisan schedule:run',
];

return implode(' ', $path);
}

/**
* Update the last time the cron was run in the kvp repo
*/
public function updateLastRunTime()
{
$dt = new DateTime('now', DateTimeZone::UTC);
$this->kvpRepo->save('cron_last_run', $dt->format(DateTime::ISO8601));
}

/**
* True/false if there's a problem with the cron. Now this is mainly
* if the cron hasn't run in the last 5 minutes at least
*
* @return bool
*/
public function cronProblemExists(): bool
{
$last_run = $this->kvpRepo->get('cron_last_run');
if (empty($last_run)) {
return true;
}

try {
$dt = DateTime::createFromFormat(DateTime::ISO8601, $last_run);
$dt_now = new DateTime('now', DateTimeZone::UTC);
} catch (Exception $e) {
Log::error('Error checking for cron problem: '.$e->getMessage());
return true;
}

// More than 5 minutes... there's a problem
$diff = $dt_now->diff($dt);
return $diff->i > 5;
}
}
2 changes: 1 addition & 1 deletion resources/views/admin/maintenance/caches.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<h6><i class="fas fa-clock"></i>
&nbsp;Reset Caches
</h6>
<div class="row">
<div class="row" style="padding-top: 5px">
<div class="col-sm-4 text-center">
{{ Form::open(['route' => 'admin.maintenance.cache']) }}
{{ Form::hidden('type', 'all') }}
Expand Down
24 changes: 24 additions & 0 deletions resources/views/admin/maintenance/cron.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="row">
<div class="col-sm-12">
<div class="form-container">
<h6><i class="fas fa-clock"></i>
&nbsp;Cron
</h6>
<div class="row" style="padding-top: 5px">
<div class="col-sm-12">
<p>A cron must be created that runs every minute calling artisan. Example:</p>
<label style="width: 100%">
<input type="text" value="{{ $cron_path }}" class="form-control" style="width: 100%"/>
</label>

@if($cron_problem_exists)
<div class="alert alert-danger" role="alert">
There was a problem running the cron; make sure it's setup and check logs at
<span class="text-monospace bg-gradient-dark">storage/logs/cron.log</span>
</div>
@endif
</div>
</div>
</div>
</div>
</div>
6 changes: 6 additions & 0 deletions resources/views/admin/maintenance/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
@section('content')
@include('flash::message')

<div class="card border-blue-bottom">
<div class="content">
@include('admin.maintenance.cron')
</div>
</div>

<div class="card border-blue-bottom">
<div class="content">
@include('admin.maintenance.caches')
Expand Down