调度cron作业laravel

xsuvu9jc  于 2022-12-14  发布在  其他
关注(0)|答案(3)|浏览(163)

我想知道如何安排一个cron作业在每天00:01运行。
我已经在App/Jobs文件夹中创建了JOB

<?php
namespace App\Jobs;
use App\Models\Result;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use DB;

set_time_limit(0);

class UpdateActive extends Job implements SelfHandling
{
    public static function ActiveUpdate()
    {
        Result::update(['draw_id' => 1, 
                        'isactive' => 0
                       ]);
     }

   public static function downGrade()
   {
   try {
        UserRole::update(['permission' => 1,
                         'isactive' => 2    
        ]);
   } catch (QueryException $e ) {
     //handle error
   }
   }

   public static function handle() 
   {
     self::ActiveUpdate();
     self::downGrade();
   }
 }

在App/Console/Kernel.php中,我已经将此链接添加到schedule方法

protected function schedule(Schedule $schedule)
    {
        /*$schedule->command('inspire')
                 ->hourly(); */
        $schedule->call(function () {
            $check_draw = \App\Jobs\UpdateActive::ActiveUpdate();

        })->everyMinute();
    }

请注意,我已将everyMinute用于测试目的

crontab -e中,我添加了
* * * * * php /home/vagrant/Code/projects/artisan schedule:run 1>> /dev/null 2>&1
但我想调度似乎没有运行,因为当我检查results表时,isactive字段没有更改。
我想知道我哪里错了,请。如果有人在L5做过这个。我错过了什么?

ryoqjall

ryoqjall1#

我想知道如何安排一个cron作业在每天00:01运行。
所以你想让它在每天00:01运行?

答案:

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        $check_draw = \App\Jobs\UpdateActive::ActiveUpdate();
    })->dailyAt("00:01");
}

* 旁白:(根据您的意见编辑)*

我会这样做:
命令:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdateActiveCommand extends Command
{
    protected $signature = 'update-active';

    protected $description = 'Update something?';

    public function handle()
    {
        try {
            $this->comment("Update active...");
            $this->updateActive();

            $this->comment("Downgrade...");
            $this->downGrade();

            $this->info("Done!");
        } catch (QueryException $e ) {
            $this->error($e->getMessage());
        }
    }

     private function updateActive()
     {
        Result::update([
            'draw_id'  => 1, 
            'isactive' => 0,
        ]);
     }

     private function downGrade()
     {
        UserRole::update([
            'permission' => 1,
            'isactive'   => 2,
        ]);
     }
}

调度程序:

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    protected $commands = [
        \App\Console\Commands\UpdateActiveCommand::class,
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('update-active')
                 ->dailyAt('00:01')
                 ->sendOutputTo(storage_path('logs/update-active.log'))
                 ->emailOutputTo('baako@baako.com');
    }
}

如果您以这种方式执行,您还可以从命令行使用php artisan update-active运行它并查看输出。

q3qa4bjr

q3qa4bjr2#

namespace App\Console\Commands;

使用应用程序\模型\用户;使用DB;
使用照明\控制台\命令;
class CronJob extends命令{ /**控制台命令的名称和签名。 * @var string */ protected $signature = '命令:名称';

protected $new = 'cronjob';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Command description';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $users = DB::table('users')
                ->where([
                    ['end_date', '>=',date("Y-m-d", strtotime('+ 1 day'))],//some condition
                    ['start_date', '<=',date("Y-m-d", strtotime('+ 1 day'))],//some condition
                ])->get(); 
    foreach ($users as $values ){

       //Do Something
    }
}

} Laravel cron

nfeuvbwi

nfeuvbwi3#

php artisan make:command PushNotification
make changes in app->console->kernal.php write the code below,
  protected function schedule(Schedule $schedule)
    {
        $schedule->command('log:sendPush')->everyMinute();       
    }
crontab -e (in Terminal)
press i to insert in terminal
edit below mentioned line
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
* * * * * cd /var/www/html/api && php artisan schedule:run >> /dev/null 2>&1
press escape then write :wq! and press enter
this is the way to edit in terminal
Then write your code in PushNotification created job in public function handle
Enjoy Laravel Cron Job.

相关问题