Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel scheduler not working as expected

I build an application in laravel which suppose to fetch news using webhose.io every 4 hours for all country . It seems to work fine for sometime . But stops and restart again after sometime . I don't know how to track what is going wrong .

Laravel Version

Laravel Framework 5.5.45

Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-142-generic x86_64)

CRON Job details

# 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 App\Console;

    use DB;
    use App;
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

    class Kernel extends ConsoleKernel
    {
        /**
        * The Artisan commands provided by your application.
        *
        * @var array
        */
        protected $commands = [
            //
            '\App\Console\Commands\GetNewsUpdates',
            '\App\Console\Commands\AddBirthdayNotifications',
            '\App\Console\Commands\DeleteBirthdayNotifications',
        ];

        /**
        * Define the application's command schedule.
        *
        * @param  \Illuminate\Console\Scheduling\Schedule  $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 Logs for Jobs Recent logs ( Which is working fine )

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 

Log for CRON Job

LOG for CRON from System log

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)

Edit :

Added withoutoverlapping() Logs with time I found news update task is running every 4 hours but getting intreputted by other task .

if (App::environment('live')) {
            $schedule->command('GetNewsUpdates:getnews')->cron('0 */4 * * *')->withoutOverlapping(10)->sendOutputTo('/root/logs/laravel_output.log');

Logs with time

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 .

Incomplete task is being done if you compare with complete task from above.

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
like image 913
twister_void Avatar asked Feb 25 '19 10:02

twister_void


People also ask

How do I know if a cron job is working in laravel?

Using Output In a Script to Show a Running Cron Job You can add a line of code in your existing script to output a result when the script is run. If the result of this command produces an output, then you can use this output to confirm that your cron script is running.

How do I know if laravel Task Scheduler is running?

Well one of the way can test if Schedule is running you can dump anything to log file. Log::info('Schedule Running '. \Carbon\Carbon::now()); After that you can grep the message from Logs.

What is cron job in laravel?

Laravel Cron Job SchedulingLaravel provides an easy way for task scheduling. Laravel's 'Command Scheduler' allows you to easily define the schedule of the commands within Laravel itself. When using the scheduler, only one Cron entry is needed on the server. Your task schedule is defined in the app/Console/Kernel.


3 Answers

In Simple Terms When the CRON daemon calls the php artisan schedule:run command every minute, the Console Kernel will be booted up and the jobs you defined inside your App\Console\Kernel::schedule() method will be registered into the scheduler.

The schedule() method takes an instance of Illuminate\Console\Scheduling\Schedule as the only argument, this is the schedule manager used to record the jobs you give it and decides what should run every time the CRON daemon pings it.

Now moving towards your issue .

as @hoseinz3 Mentioned is almost right

How Laravel prevents your scheduled jobs from overlapping ?

Sometimes a scheduled job takes more time to run than what we initially expected, and this causes another instance of the job to start while the first one is not done yet, for example imagine that we run a job that generates a report every minute, after sometime when the data gets huge the report generation might take more than 1 minute so another instance of that job starts while the first is still ongoing.

In most scenarios this is fine, but sometimes this should be prevented in order to guarantee correct data or prevent a high server resources consumption .

Laravel will check for the Console\Scheduling\Event::withoutOverlapping class property and if it's set to true it'll try to create a mutex for the job, and will only run the job if creating a mutex was possible.

For more on Mutex

So Laravel creates a mutex when the job starts the very first time, and then every time the job runs it checks if the mutex exists and only runs the job if it doesn't.

So Laravel creates a filter-callback method that instructs the Schedule Manager to ignore the task if a mutex still exists, it also creates an after-callback that clears the mutex after an instance of the task is done.

Try this snippet

<?php

namespace App\Console;

use App;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        '\App\Console\Commands\GetNewsUpdates',
        '\App\Console\Commands\AddBirthdayNotifications',
        '\App\Console\Commands\DeleteBirthdayNotifications',
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $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');
    }
}

Try some kind of CRON Job Monitoring system . You can Try Eyewitness which is very good tool to monitor Laravel Application .

like image 120
Java_begins Avatar answered Oct 17 '22 23:10

Java_begins


You have used withoutOverlapping method, based on Laravel documentation that means when one job took longs time to run Laravel Hold new job for 24 hours by default, and after that, it tries to run that job if no job would running, and you could also change this number by passing through withoutOverlapping argument. for example

$schedule->command('command')->hourly()->withoutOverlapping(5);

It means Laravel hold new command for 5 hours if there is running command after that it will try to run this again.

like image 29
hoseinz3 Avatar answered Oct 18 '22 00:10

hoseinz3


Laravel run cron jobs synchronously, so some tasks can block other.

  1. You can try dispatch some event to queue instead of just running command.

  2. Or try run command in background

    $schedule->command('analytics:report')
         ->daily()
         ->runInBackground();
    
  3. Also simple workaround is configure your cron jobs at different schedule. Like with minutes difference.

Since cron start each minute all scheduled tasks will be started normally with few minutes delay between it without blocking each other.

$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

or

$schedule->command('analytics:report')->dailyAt('13:01');
$schedule->command('analytics:report2')->dailyAt('13:02');
$schedule->command('analytics:report3')->dailyAt('13:03');
like image 1
Sanasol Avatar answered Oct 17 '22 22:10

Sanasol