Laravel调度程序未按预期工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel调度程序未按预期工作相关的知识,希望对你有一定的参考价值。
我在laravel中构建了一个应用程序,假设所有国家/地区每4小时使用webhose.io获取新闻。它似乎在某个时候工作正常。但是一段时间后再次停止并重新启动。我不知道如何追踪出了什么问题。
Laravel版本
Laravel Framework 5.5.45
Ubuntu 16.04.5 LTS(GNU / Linux 4.4.0-142-通用x86_64)
CRON工作细节
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1
00 22 * * * /opt/dbbackup/db.sh /dev/null 2>&1
Kernel.php
<?php
namespace AppConsole;
use DB;
use App;
use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
'AppConsoleCommandsGetNewsUpdates',
'AppConsoleCommandsAddBirthdayNotifications',
'AppConsoleCommandsDeleteBirthdayNotifications',
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command(AddBirthdayNotifications::class, ['--force'])->daily();
$schedule->command('AddBirthdayNotifications:addbdaynotifications')->dailyAt('13:00')->withoutOverlapping();
$schedule->command('DeleteBirthdayNotifications:deletebdaynotifications')->daily()->withoutOverlapping();
//->daily() // midnight
if ( App::environment('live') ) {
$schedule->command('GetNewsUpdates:getnews')->cron('0 */4 * * *')->sendOutputTo('/root/logs/laravel_output.log');
}
$schedule->command('DeleteSendEmails:deleteEmailsSend')->weekly()->withoutOverlapping();
$schedule->command('SendEmails:sendEmailToUser')->hourly()->withoutOverlapping();
$schedule->command('ConnectSendEmails:sendEmailToConnectUser')->hourly()->withoutOverlapping();
// $schedule->command('GetNewsUpdates:getnews')->everyMinute();
// $schedule->command('DeleteBirthdayNotifications:deletebdaynotifications')
// ->daily();
/* $schedule->call(function () {
DB::table('recent_users')->delete();
})->daily();*/
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Laravel记录工作最近的日志(哪个工作正常)
1062 BN 1062 BN 1062 BN 1062 BN 1062 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 IO 1061 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BR 988 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BW 1059 BA 1058 BA 1058 BA 1058 BA 1058 BA 1058 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BO 1057 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BT 1056 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BM 1055 BZ 1053 BZ 1053 BZ 1053 BZ 1053 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BE 987 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BY 1052 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BB 1051 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BD 1050 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 BH 1049 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AZ 1047 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AT 986 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AU 985 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AW 1046 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AM 1045 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AG 1044 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AI 1042 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AO 1041 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AD 1040 AS 1039 AS 1039 AS 1039 AS 1039 AS 1039 AS 1039 AS 1039 AS 1039 AS 1039 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 DZ 1038 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AL 1037 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036 AF 1036
从系统日志记录CRON
Feb 25 10:05:01 connect424 CRON[6770]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:05:01 connect424 CRON[6771]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Feb 25 10:05:01 connect424 CRON[6774]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:06:01 connect424 CRON[6804]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:06:01 connect424 CRON[6805]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:07:01 connect424 CRON[6836]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:07:01 connect424 CRON[6837]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:08:01 connect424 CRON[6871]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:08:01 connect424 CRON[6870]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:09:01 connect424 CRON[6919]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:09:01 connect424 CRON[6920]: (root) CMD ( [ -x /usr/lib/php/sessionclean ] && if [ ! -d /run/systemd/system ]; then /usr/lib/php/sessionclean; fi)
Feb 25 10:09:01 connect424 CRON[6922]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:10:01 connect424 CRON[7013]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:10:01 connect424 CRON[7014]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:11:01 connect424 CRON[7041]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:11:01 connect424 CRON[7043]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:12:01 connect424 CRON[7071]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:12:01 connect424 CRON[7073]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:13:01 connect424 CRON[7123]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:13:01 connect424 CRON[7125]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:14:01 connect424 CRON[7174]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:14:01 connect424 CRON[7175]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:15:01 connect424 CRON[7221]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Feb 25 10:15:01 connect424 CRON[7222]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:15:01 connect424 CRON[7225]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:16:01 connect424 CRON[7269]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:16:01 connect424 CRON[7270]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:17:01 connect424 CRON[7310]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:17:01 connect424 CRON[7311]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Feb 25 10:17:01 connect424 CRON[7309]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:18:01 connect424 CRON[7347]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:18:01 connect424 CRON[7348]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:19:01 connect424 CRON[7396]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:19:02 connect424 CRON[7398]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:20:01 connect424 CRON[7426]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:20:01 connect424 CRON[7427]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:21:01 connect424 CRON[7462]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:21:01 connect424 CRON[7464]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:22:01 connect424 CRON[7500]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:22:01 connect424 CRON[7501]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:23:01 connect424 CRON[7532]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:23:01 connect424 CRON[7534]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:24:01 connect424 CRON[7574]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:24:01 connect424 CRON[7575]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:25:01 connect424 CRON[7613]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Feb 25 10:25:01 connect424 CRON[7614]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:25:01 connect424 CRON[7616]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
Feb 25 10:26:01 connect424 CRON[7649]: (root) CMD (php /var/www/html/connect/artisan schedule:run >> /root/logs/sec.log 2>&1)
Feb 25 10:26:01 connect424 CRON[7650]: (developer) CMD (php /var/www/html/connect/artisan schedule:run >> /dev/null 2>&1)
编辑:
随着时间的推移添加了withoutoverlapping()
日志我发现新闻更新任务每4小时运行一次,但是被其他任务引起了反响。
if (App::environment('live')) {
$schedule->command('GetNewsUpdates:getnews')->cron('0 */4 * * *')->withoutOverlapping(10)->sendOutputTo('/root/logs/laravel_output.log');
记录时间
2019-02-27 12:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' GetNewsUpdates:getnews > '/root/logs/laravel_output.log' 2>&1
2019-02-27 12:01:01: No scheduled commands are ready to run.
2019-02-27 12:02:01: No scheduled commands are ready to run.
2019-02-27 12:03:01: No scheduled commands are ready to run.
2019-02-27 12:04:01: No scheduled commands are ready to run.
2019-02-27 13:30:01: No scheduled commands are ready to run.
Running scheduled command: '/usr/bin/php7.2' 'artisan' SendEmails:sendEmailToUser > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
2019-02-27 13:31:01: No scheduled commands are ready to run.
2019-02-27 13:59:01: No scheduled commands are ready to run.
2019-02-27 14:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' SendEmails:sendEmailToUser > '/dev/null' 2>&1
2019-02-27 14:59:01: No scheduled commands are ready to run.
2019-02-27 15:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' SendEmails:sendEmailToUser > '/dev/null' 2>&1
2019-02-27 15:01:01: No scheduled commands are ready to run.
2019-02-27 15:02:01: No scheduled commands are ready to run.
2019-02-27 15:58:01: No scheduled commands are ready to run.
2019-02-27 15:59:01: No scheduled commands are ready to run.
2019-02-27 16:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' GetNewsUpdates:getnews > '/root/logs/laravel_output.log' 2>&1
2019-02-27 16:58:01: No scheduled commands are ready to run.
2019-02-27 16:59:01: No scheduled commands are ready to run.
2019-02-27 17:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
2019-02-27 17:01:01: No scheduled commands are ready to run.
2019-02-27 17:58:01: No scheduled commands are ready to run.
2019-02-27 17:59:01: No scheduled commands are ready to run.
2019-02-27 18:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
2019-02-27 18:01:01: No scheduled commands are ready to run.
2019-02-27 18:58:02: No scheduled commands are ready to run.
2019-02-27 18:59:01: No scheduled commands are ready to run.
2019-02-27 19:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
2019-02-27 19:01:01: No scheduled commands are ready to run.
2019-02-27 19:09:01: No scheduled commands are ready to run.
2019-02-27 19:10:01: No scheduled commands are ready to run.
2019-02-27 19:59:01: No scheduled commands are ready to run.
2019-02-27 20:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' GetNewsUpdates:getnews > '/root/logs/laravel_output.log' 2>&1
2019-02-27 20:01:01: No scheduled commands are ready to run.
2019-02-27 20:59:01: No scheduled commands are ready to run.
2019-02-27 21:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
2019-02-27 21:01:01: No scheduled commands are ready to run.
2019-02-27 21:19:01: No scheduled commands are ready to run.
Running scheduled command: '/usr/bin/php7.2' 'artisan' SendEmails:sendEmailToUser > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
2019-02-27 21:20:01: No scheduled commands are ready to run.
2019-02-27 21:59:01: No scheduled commands are ready to run.
2019-02-27 22:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
2019-02-27 22:01:01: No scheduled commands are ready to run.
2019-02-27 22:59:01: No scheduled commands are ready to run.
2019-02-27 23:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' SendEmails:sendEmailToUser > '/dev/null' 2>&1
2019-02-27 23:01:01: No scheduled commands are ready to run.
2019-02-27 23:59:01: No scheduled commands are ready to run.
2019-02-28 00:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' DeleteBirthdayNotifications:deletebdaynotifications > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.2' 'artisan' GetNewsUpdates:getnews > '/root/logs/laravel_output.log' 2>&1
2019-02-28 01:00:01: Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.2' 'artisan' SendEmails:sendEmailToUser > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.2' 'artisan' ConnectSendEmails:sendEmailToConnectUser > '/dev/null' 2>&1
2019-02-28 01:01:01: No scheduled commands are ready to run.
Laravel Recent Log where you can see inconsistency .
如果您与上面的完整任务进行比较,则正在完成不完整的任务。
ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZW 1226 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 ZM 1225 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 YE 1223 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VG 1219 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218 VN 1218
简单来说当CRON守护进程调用php artisan schedule:每分钟运行命令时,将启动Console Kernel并在AppConsoleKernel::schedule()
方法中定义的作业将注册到调度程序中。
schedule()
方法将IlluminateConsoleSchedulingSchedule
的一个实例作为唯一的参数,这是用于记录您提供的作业的计划管理器,并决定每次CRON守护程序ping它时应该运行什么。
现在转向你的问题。
as @ hoseinz3提到几乎是对的
Laravel如何阻止您的预定作业重叠?
有时,计划的作业比我们最初的预期花费更多的时间来运行,这导致另一个作业实例在第一个作业尚未完成时启动,例如假设我们运行的作业每分钟生成一个报告,之后有时,当数据变得庞大时,报告生成可能需要1分钟以上,因此该工作的另一个实例会在第一个实例仍在进行时启动。
在大多数情况下,这很好,但有时应该防止这种情况,以保证正确的数据或防止高服务器资源消耗。
Laravel将检查Console Scheduling Event :: withoutOverlapping类属性,如果它设置为true,它将尝试为作业创建一个互斥锁,并且只有在可以创建互斥锁时才运行该作业。
有关Mutex的更多信息
因此,当作业第一次启动时,Laravel会创建一个互斥锁,然后每次运行该作业时,都会检查互斥锁是否存在,只检查该作业是否存在。
因此,Laravel创建了一个过滤器回调方法,指示计划管理器在互斥锁仍然存在时忽略该任务,它还会创建一个在完成任务实例后清除互斥锁的后回调。
试试这个片段
<?php
namespace AppConsole;
use App;
use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
'AppConsoleCommandsGetNewsUpdates',
'AppConsoleCommandsAddBirthdayNotifications',
'AppConsoleCommandsDeleteBirthdayNotifications',
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule) {
// ---------- Birthday Notification at Daily at 15:00 --------------------
$schedule->command('AddBirthdayNotifications:addbdaynotifications')
->dailyAt('23:45')
->withoutOverlapping(60);
// ---------- Delete Birthday Notification at Daily at 23:30 -------------
$schedule->command('DeleteBirthdayNotifications:deletebdaynotifications')
->dailyAt('23:30')
->withoutOverlapping(60);
/*
Get News At every 4 Hours Daily
*/
if (App::environment('live')) {
$schedule->command('GetNewsUpdates:getnews')
->cron('0 */4 * * *')
->withoutOverlapping(100)
->pingBefore("") //This will alert if something fails
->thenPing("")
->appendOutputTo('output.log');
}
// ----------- Delete Send Emails at weekly -------------------
$schedule->command('DeleteSendEmails:deleteEmailsSend')
->weekly()
->withoutOverlapping(60);
// ----------- Send Emails at Daily at 6:00 --------------------
$schedule->command('SendEmails:sendEmailToUser')
->dailyAt('6:00')
->withoutOverlapping()
->appendOutputTo('/root/logs/Emails_output.log');
// ------------ Bulk user email scheduling Run Daily at 9:30 According to America time .... -----------------
$schedule->command('ConnectSendEmails:sendEmailToConnectUser')
->timezone('America/New_York')
->dailyAt('9:30')
->withoutOverlapping()
->appendOutputTo('/root/logs/BulkEmails_output.log');
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands() {
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}
}
尝试某种CRON作业监控系统。你可以尝试目击者,这是监测Laravel Application非常好的工具。
您已经使用withoutOverlapping
方法,基于Laravel documentation,这意味着当一个作业需要很长时间才能默认运行Laravel Hold新作业24小时,然后,如果没有作业运行,它会尝试运行该作业,并且您也可以更改这个数字通过withoutOverlapping
论证。例如
$schedule->command('command')->hourly()->withoutOverlapping(5);
这意味着如果有运行命令,Laravel将持续5小时的新命令,之后它将再次尝试运行此命令。
Laravel同步运行cron作业,因此某些任务可以阻止其他任务。
- 您可以尝试将某个事件调度到队列而不是仅运行命令。
- 或者试试run command in background
$schedule->command('analytics:report') ->daily() ->runInBackground();
- 同样简单的解决方法是按照不同的计划配置您的cron作业。与分钟差异一样。
由于cron每分钟启动一次,所有计划任务都将正常启动,它们之间的延迟几分钟而不会相互阻塞。
$schedule->command('analytics:report')->hourlyAt(1); // run every hour at 1 minute
$schedule->command('analytics:report2')->hourlyAt(2); // run every hour at 2 minute
$schedule->command('analytics:report3')->hourlyAt(3); // run every hour at 3 minute
要么
$schedule->command('analytics:report')->dailyAt('13:01');
$schedule->command('analytics:report2')->dailyAt('13:02');
$schedule->command('analytics:report3')->dailyAt('13:03');
以上是关于Laravel调度程序未按预期工作的主要内容,如果未能解决你的问题,请参考以下文章