Updating a table field with cron job the right way

Viewed 35

I have followed some cron job examples trying to update one specific users table field named P using the following query. I want to set the value of P to 0 when timeout is null and logdate is less than the current time. The logdate is the logdate yesterday and Carbon:: now() is the current date that is today. As for the testing I use everySecond (0.5) but the actual scenario is to run it everyday after working hours

UserPStatus Command

     public function handle()
    {

            DB::table('users')
            ->select('users.name', 'attendances.logdate', 'attendances.timeout')
            ->leftJoin('attendances','users.id','=','attendances.user_id')
            ->whereNull('attendances.timeout')
            ->where('attendances.logdate','<',Carbon::now())
            ->where('users.P',1)
            ->update([
                'P' => '0',              
            ]);
            
    }

Kernel

      class Kernel extends ConsoleKernel
        {

            protected $commands = [
                Commands\UserPStatus::class,
             ];
            /**
             * Define the application's command schedule.
             *
             * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
             * @return void
             */
            protected function schedule(Schedule $schedule)
            {
                    $schedule->command('p:update')->everySeconds(0.5);
            }

            /**
             * Register the commands for the application.
             *
             * @return void
             */
            protected function commands()
            {
                $this->load(__DIR__.'/Commands');

                require base_path('routes/console.php');
            }
        }

With the two command below I did not see any updates to user.P value

  1. Artisan schedule: run 1>> /dev/null 2>&1

  2. PHP artisan schedule: work.

0 Answers
Related