2019-10-30 21:55:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Contracts\Service;
|
|
|
|
use App\Repositories\KvpRepository;
|
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-12-10 04:26:30 +08:00
|
|
|
use Symfony\Component\Process\PhpExecutableFinder;
|
2019-10-30 21:55:32 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-12-10 04:26:30 +08:00
|
|
|
$finder = new PhpExecutableFinder();
|
|
|
|
$php_path = $finder->find(false);
|
2020-02-19 06:01:00 +08:00
|
|
|
$php_exec = str_replace('-fpm', '', $php_path);
|
|
|
|
|
|
|
|
// If this is the cgi version of the exec, add this arg, otherwise there's
|
|
|
|
// an error with no arguments existing
|
|
|
|
if (str_contains($php_exec, '-cgi')) {
|
|
|
|
$php_exec .= ' -d register_argc_argv=On';
|
|
|
|
}
|
2019-12-10 04:26:30 +08:00
|
|
|
|
2019-10-30 21:55:32 +08:00
|
|
|
$path = [
|
|
|
|
'cd '.base_path(),
|
|
|
|
'&&',
|
2020-02-19 06:01:00 +08:00
|
|
|
$php_exec,
|
2019-10-30 21:55:32 +08:00
|
|
|
'artisan schedule:run',
|
|
|
|
];
|
|
|
|
|
|
|
|
return implode(' ', $path);
|
|
|
|
}
|
|
|
|
|
2019-12-10 04:26:30 +08:00
|
|
|
/**
|
|
|
|
* Show an example cron command that runs every minute
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCronExecString(): string
|
|
|
|
{
|
|
|
|
return implode(' ', [
|
|
|
|
'* * * * *',
|
|
|
|
$this->getCronPath(),
|
|
|
|
'>> /dev/null 2>&1',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-10-30 21:55:32 +08:00
|
|
|
/**
|
|
|
|
* Update the last time the cron was run in the kvp repo
|
|
|
|
*/
|
|
|
|
public function updateLastRunTime()
|
|
|
|
{
|
2019-12-10 11:11:20 +08:00
|
|
|
$dt = new DateTime('now', new DateTimeZone('UTC'));
|
2019-10-30 21:55:32 +08:00
|
|
|
$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);
|
2019-12-10 11:11:20 +08:00
|
|
|
$dt_now = new DateTime('now', new DateTimeZone('UTC'));
|
2019-10-30 21:55:32 +08:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|