noblemfd
1 week ago
106
5
Laravel

Cron job not updating automatically on live server

Posted 1 week ago by noblemfd

I have this cron job on my Digital Ocean Ubuntu live server:

namespace App\Console\Commands;App/Console/Command

class UpdateCreateEmployeeDepartment extends Command
{

    protected $signature = 'updatecreateemployeedepartments';

    protected $description = 'Update or Create Employee Department';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        // Select all employees that haven't been updated
        $employees = HrEmployee::where('is_nav_updated', 0)->where('company_id', 1)->get();        


        // For each of the employees selected above
        foreach ($employees as $employee) {
            $department = HrDepartment::where('dept_code', $employee->nav_dept_id)->where('company_id', 1)->first();

            // If a department was found, update the employee
            if ($department) {
                $employee->department_id = $department->id;
                $employee->is_nav_updated = 1;
                $employee->save();
            }            

        }        

    } 

}


namespace App\Console;

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

class Kernel extends ConsoleKernel
{

    protected $commands = [

        'App\Console\Commands\UpdateCreateEmployeeDepartment',
    ];

    protected function schedule(Schedule $schedule)
    {
         $schedule->command('updatecreateemployeedepartments')
                  ->dailyAt('01:00');           
    }

    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

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

I observed that it's not running automatically as scheduled until when I manually run:

php artisan updatecreateemployeedepartments

How do I resolve this?

Thanks

Please sign in or create an account to participate in this conversation.