繁体   English   中英

由于自定义命令,php artisan 命令不起作用

[英]php artisan command not working due to custom command

我一直在从事需要调度程序的项目,并通过每分钟调用一个命令找到了解决方案。 我将该类注册到Kernel$commands变量中,一旦我尝试为该应用程序提供服务,它就不起作用,也不发送任何响应。

调度程序守护进程:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

use \Carbon\Carbon;

use App\ActivityLog;
use App\Booking;
use App\News;

use DB;
use Exception;
use Log;
use Mail;

class SchedulerDaemon extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'schedule:cron {--queue}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run the scheduler without cron (For use with Heroku etc)';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {
        $this->info('Waiting '. $this->nextMinute(). ' for next run of scheduler');
        sleep($this->nextMinute());
        $this->runScheduler();
    }

    /**
     * Main recurring loop function.
     * Runs the scheduler every minute.
     * If the --queue flag is provided it will run the scheduler as a queue job.
     * Prevents overruns of cron jobs but does mean you need to have capacity to run the scheduler
     * in your queue within 60 seconds.
     *
     */
    protected function runScheduler() {
        // $fn = $this->option('queue') ? 'queue' : 'call';

        $this->info('Running scheduler');

        // Artisan::$fn('schedule:run');
        $this->updateBookings();

        $this->info('completed, sleeping..');
        sleep($this->nextMinute());
        $this->runScheduler();
    }

    /**
     * Works out seconds until the next minute starts;
     *
     * @return int
     */
    protected function nextMinute() {
        $current = Carbon::now();
        return 60 -$current->second;
    }

    private function updateBookings() {
        if (Carbon::now()->timezone('Asia/Manila')->format('h:i') == '07:00' || Carbon::now()->timezone('Asia/Manila')->format('h:i') == '10:00')
            $stoppedAt = null;
            try {
                $bookings = Booking::where('pickup_date', '=', Carbon::now()->timezone('Asia/Manila')->format('Y-m-d'))
                    ->where('status', '=', '2')
                    ->get();

                Log::info($bookings);

                foreach ($bookings as $b) {
                    DB::beginTransaction();
                    Mail::send(
                        'templates.emails.pickup',
                        ['booking' => $b, 'status' => 'pickup'],
                        function ($m) use ($b) {
                            $m->from('no-reply@kane.com');
                            $m->to($b->user->email, $b->user->getName())
                                ->subject('Pick up Status');
                        }
                    );

                    $b->status = 3;
                    $b->save();

                    $stoppedAt = $b->user;

                    DB::commit();
                }

                ActivityLog::log('Updating all bookings with pick up date set for today.', 0);
                ActivityLog::log('Sending emails to all clients with pick up date set for today.', 0);
            } catch (Exception $e) {
                ActivityLog::log('Daily booking update and mailing task failed (stopped at '.$stoppedAt->email.'): '.$e->getMessage(), 0);
                Log::error($e);
                DB::rollback();
            }
        }
    }
}

核心

<?php

namespace App\Console;

use Illuminate\Support\Facades\Http;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

use \Carbon\Carbon;

use App\ActivityLog;
use App\Booking;
use App\News;

use DB;
use Exception;
use Log;
use Mail;

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

    /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule) {
        // UPDATE BOOKINGS FOR THAT DAY
        // $schedule->call(function() {
        //  $stoppedAt = null;
        //  try {
        //      $bookings = Booking::where('pickup_date', '=', Carbon::now()->timezone('Asia/Manila')->format('Y-m-d'))
        //          ->where('status', '=', '2')
        //          ->get();

        //      // Log::info($bookings);

        //      foreach ($bookings as $b) {
        //          DB::beginTransaction();
        //          Mail::send(
        //              'templates.emails.pickup',
        //              ['booking' => $b, 'status' => 'pickup'],
        //              function ($m) use ($b) {
        //                  $m->from('no-reply@kane.com');
        //                  $m->to($b->user->email, $b->user->getName())
        //                      ->subject('Pick up Status');
        //              }
        //          );

        //          $b->status = 3;
        //          $b->save();

        //          $stoppedAt = $b->user;

        //          DB::commit();
        //      }

        //      ActivityLog::log('Updating all bookings with pick up date set for today.', 0);
        //      ActivityLog::log('Sending emails to all clients with pick up date set for today.', 0);
        //  } catch (Exception $e) {
        //      ActivityLog::log('Daily booking update and mailing task failed (stopped at '.$stoppedAt->email.'): '.$e->getMessage(), 0);
        //      Log::error($e);
        //      DB::rollback();
        //  }
        // })
        // // ->everyFiveMinutes()
        // ->timezone('Asia/Manila')
        // ->twiceDaily(7, 10)
        // ->name('daily-pickup-mailer')
        // ->withoutOverlapping();

        // $schedule->call(function() {
        //  try {
        //      DB::beginTransaction();

        //      $queryString = http_build_query([
        //          'access_key' => env('MEDIASTACK_ACCESS_KEY'),
        //          'countries' => 'ph',
        //          'date' => Carbon::yesterday()->format('Y-m-d').','.Carbon::now()->format('Y-m-d'),
        //          'limit' => 1
        //      ]);

        //      $ch = curl_init(sprintf('%s?%s', 'https://api.mediastack.com/v1/news', $queryString));
        //      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //      $json = curl_exec($ch);
        //      curl_close($ch);
        //      $response = json_decode($json, true);

        //      DB::commit();
        //  } catch (Exception $e) {
        //      ActivityLog::log('Failed to fetch new news for today.');
        //      DB::rollback();
        //  }
        // })
        // ->timezone('Asia/Manila')
        // ->daily()
        // ->name('daily-news-fetcher')
        // ->withoutOverlapping();
    }
}

一旦我在内核中注释掉了 SchedulerDaemon,工匠就可以正常工作,就像什么也没发生一样。

你应该改变你的内核类。

<?php

namespace App\Console;

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

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

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

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

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

暂无
暂无

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

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