1
0
mirror of https://github.com/freescout-helpdesk/freescout.git synced 2024-11-25 20:02:30 +01:00
freescout/app/Console/Kernel.php

177 lines
5.6 KiB
PHP
Raw Normal View History

2018-06-22 19:44:21 +02:00
<?php
namespace App\Console;
2018-08-15 08:10:05 +02:00
use Carbon\Carbon;
2018-06-22 19:44:21 +02:00
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
2019-05-01 18:09:03 +02:00
use App\Misc\Mail;
use App\Option;
2018-06-22 19:44:21 +02:00
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
2018-09-24 15:07:07 +02:00
// It is not clear what for this array
//\App\Console\Commands\CreateUser::class,
2018-06-22 19:44:21 +02:00
];
/**
* Define the application's command schedule.
*
2018-07-24 08:34:28 +02:00
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*
2018-06-22 19:44:21 +02:00
* @return void
*/
protected function schedule(Schedule $schedule)
{
// Keep in mind that this function is also called on clearing cache.
2018-08-02 18:17:13 +02:00
// Remove failed jobs
$schedule->command('queue:flush')
->weekly();
2018-08-02 18:17:13 +02:00
// Restart processing queued jobs (just in case)
$schedule->command('queue:restart')
->hourly();
2018-09-24 15:07:07 +02:00
$schedule->command('freescout:fetch-monitor')
->everyMinute()
->withoutOverlapping();
$schedule->command('freescout:update-folder-counters')
->hourly();
2018-11-12 13:40:23 +01:00
$schedule->command('freescout:module-check-licenses')
->daily();
2018-12-10 13:52:28 +01:00
// Logs monitoring.
2018-12-12 15:39:24 +01:00
$alert_logs_period = config('app.alert_logs_period');
if (config('app.alert_logs') && $alert_logs_period) {
2018-12-10 13:52:28 +01:00
$logs_cron = '';
2018-12-12 15:39:24 +01:00
switch ($alert_logs_period) {
2018-12-10 13:52:28 +01:00
case 'hour':
$logs_cron = '0 * * * *';
break;
case 'day':
$logs_cron = '0 0 * * *';
break;
case 'week':
$logs_cron = '0 0 * * 0';
break;
case 'month':
$logs_cron = '0 0 1 * *';
break;
}
if ($logs_cron) {
$schedule->command('freescout:logs-monitor')
->cron($logs_cron)
->withoutOverlapping();
}
}
2018-08-03 20:56:06 +02:00
// Fetch emails from mailboxes
2019-05-01 18:09:03 +02:00
$fetch_command = $schedule->command('freescout:fetch-emails')
2018-12-15 10:38:53 +01:00
->withoutOverlapping()
->sendOutputTo(storage_path().'/logs/fetch-emails.log');
2018-08-03 20:56:06 +02:00
switch (config('app.fetch_schedule')) {
case Mail::FETCH_SCHEDULE_EVERY_FIVE_MINUTES:
2019-05-01 18:09:03 +02:00
$fetch_command->everyFiveMinutes();
break;
case Mail::FETCH_SCHEDULE_EVERY_TEN_MINUTES:
2019-05-01 18:09:03 +02:00
$fetch_command->everyTenMinutes();
break;
case Mail::FETCH_SCHEDULE_EVERY_FIFTEEN_MINUTES:
2019-05-01 18:09:03 +02:00
$fetch_command->everyFifteenMinutes();
break;
case Mail::FETCH_SCHEDULE_EVERY_THIRTY_MINUTES:
2019-05-01 18:09:03 +02:00
$fetch_command->everyThirtyMinutes();
break;
case Mail::FETCH_SCHEDULE_HOURLY:
2019-05-01 18:09:03 +02:00
$fetch_command->Hourly();
break;
default:
$fetch_command->everyMinute();
break;
2019-05-01 18:09:03 +02:00
}
2018-11-23 10:06:32 +01:00
$schedule = \Eventy::filter('schedule', $schedule);
2018-08-02 18:17:13 +02:00
// Command runs as subprocess and sets cache mutex. If schedule:run command is killed
2018-08-02 18:18:32 +02:00
// subprocess does not clear the mutex and it stays in the cache until cache:clear is executed.
2018-08-02 18:17:13 +02:00
// By default, the lock will expire after 24 hours.
2018-11-12 10:12:57 +01:00
2018-08-15 08:10:05 +02:00
if (function_exists('shell_exec')) {
$running_commands = $this->getRunningQueueProcesses();
if (count($running_commands) > 1) {
// Stop all queue:work processes.
2018-08-15 08:10:05 +02:00
// queue:work command is stopped by settings a cache key
\Helper::queueWorkRestart();
// Sometimes processes stuck and just continue running, so we need to kill them.
// Sleep to let processes stop.
sleep(1);
// Check processes again.
$worker_pids = $this->getRunningQueueProcesses();
if (count($worker_pids) > 1) {
// Current process also has to be killed, as otherwise it "stucks"
// $current_pid = getmypid();
// foreach ($worker_pids as $i => $pid) {
// if ($pid == $current_pid) {
// unset($worker_pids[$i]);
// break;
// }
// }
shell_exec('kill '.implode(' | kill ', $worker_pids));
}
2018-08-15 08:10:05 +02:00
}
}
2018-08-02 18:17:13 +02:00
$schedule->command('queue:work', Config('app.queue_work_params'))
2018-08-03 20:56:06 +02:00
->everyMinute()
2018-08-02 18:17:13 +02:00
->withoutOverlapping()
2018-08-02 18:18:32 +02:00
->sendOutputTo(storage_path().'/logs/queue-jobs.log');
2018-06-22 19:44:21 +02:00
}
/**
* Get pids of the queue:work processes.
*
* @return [type] [description]
*/
protected function getRunningQueueProcesses()
{
$pids = [];
try {
$processes = preg_split("/[\r\n]/", shell_exec("ps aux | grep 'queue:work'"));
foreach ($processes as $process) {
preg_match("/^[\S]+\s+([\d]+)\s+/", $process, $m);
if (!preg_match("/(sh \-c|grep )/", $process) && !empty($m[1])) {
$pids[] = $m[1];
}
}
} catch (\Exception $e) {
// Do nothing
}
return $pids;
}
2018-06-22 19:44:21 +02:00
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}