简体   繁体   English

Laravel 调度程序 Cron 作业

[英]Laravel Scheduler Cron Job

I've created a command named SendAutoEmail and i'm running the command by this我创建了一个名为 SendAutoEmail 的命令,我正在通过它运行命令

php artisan send:autoemail php 工匠发送:autoemail

command is working properly but it's not sending an email, i've added direct Email function into command and also tried to add email function into Controller method and called that controller into Command file but email is not sending but if i'm trying to call the same method via url its sending email successfully, i don't know what is the issue, here is the code command is working properly but it's not sending an email, i've added direct Email function into command and also tried to add email function into Controller method and called that controller into Command file but email is not sending but if i'm trying to call同样的方法通过 url 发送 email 成功,我不知道是什么问题,这里是代码

Commands>SendAutoEmail.php命令>SendAutoEmail.php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Http\Controllers\PassportController;

class SendAutoEmail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send:autoemail';

    /**
     * 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 int
     */
    public function handle()
    {
        $controller = new PassportController();//your controller name
        $controller->sendEMail();
        dd($controller);
        \Log::info('Cron is working fine!');
        
        $this->info('Send:AutoEmail cron Command RUn Seccessfully');
        return 0;
    }
}

Console>Kernel.php控制台>内核.php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        SendAutoEmail::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('send:autoemail')
    }
}

and the controller method from where i'm trying to send email.以及我尝试发送 email 的 controller 方法。

public function sendEMail(){
    
    $alerts = [
        'passport_no'   =>  'ex12312312',
        'name'  =>  'test name',
        'expiry_date'   =>  '01-11-2020',
        'id_no_cnic'    =>  '12312-3123123-1'
    ];
    $emailSent = Mail::to('test.email@gmail.com')->send(new ExpireAlert($alerts));
    DB::table('tbl_test')->insert(
        ['data_text' => 'test data new']
    );
    dd($emailSent);
    // here on dd($emailSent) i'm getting 0 in response
    DB::table('tbl_test')->insert(
        ['data_text' => 'test data']
    );
    print_r($alerts);
}

Have you run the scheduler?你有运行调度程序吗?

php artisan schedule:run php 工匠时间表:运行

Then you should see your mail in the queue table within your database.然后您应该在数据库中的队列表中看到您的邮件。

Now, to execute the queue:现在,执行队列:

php artisan queue:work php 工匠队列:工作

In production, the package Supervisor is recommended.生产中推荐package Supervisor What it does is to make sure the worker always is running.它所做的是确保工作人员始终在运行。


Source: https://laravel.com/docs/8.x/queues#running-the-queue-worker资料来源: https://laravel.com/docs/8.x/queues#running-the-queue-worker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM