关于php:laravel8-console调度示例

2次阅读

共计 2050 个字符,预计需要花费 6 分钟才能阅读完成。

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\EsInit::class,
        \App\Console\Commands\EnterpriseStatus::class,
        \App\Console\Commands\ExportExcel::class,
        \App\Console\Commands\OldData::class,
        \App\Console\Commands\DeleteRemoved::class,// 将自定义注入到 console 中
    ];

    /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {$schedule->command('es:init')->dailyAt('1:00');
        $schedule->command('enterprise:status')->dailyAt('2:00');
        $schedule->command('delete:removed')->sundays();// 每周日执行一次}

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

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

namespace App\Console\Commands;

use App\Models\Product\KnowledgeDict;
use App\Models\Product\LibraryCheckList;
use App\Models\Product\LibraryCheckSystem;
use App\Models\Product\LibraryQuestionList;
use App\Models\Product\LibraryQuestionSystem;
use App\Models\Product\LibrarySection;
use App\Models\Product\Notice;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'remove field data';

    protected $removeClass = [
        KnowledgeDict::class,
        LibraryQuestionList::class,
        LibraryQuestionSystem::class,
        LibraryCheckList::class,
        LibraryCheckSystem::class,
        LibrarySection::class,
        Notice::class,
    ];

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function deleteRemoved()
    {if ($this->removeClass) {foreach ($this->removeClass as $value) {
                $model = new $value;
                $model::where('isRemoved', 1)->delete();}
        }
        return true;
    }

    public function handle()
    {$this->deleteRemoved();
    }
}
正文完
 0