laravel 动态添加调度器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38801334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:15:21  来源:igfitidea点击:

laravel add scheduler dynamically

laravelscheduled-tasks

提问by SexyMF

I have a system where the user can create background tasks via the UI.
The task interval are every few hours (user choice in the UI).

我有一个系统,用户可以在其中通过 UI 创建后台任务。
任务间隔是每隔几个小时(用户在 UI 中选择)。

when the user creates a task via the ui i want to add it to the scheduler dynamically.

当用户通过 ui 创建任务时,我想将其动态添加到调度程序中。

As the example states, this is static and not dynamic.

正如示例所述,这是静态的而不是动态的。

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        DB::table('recent_users')->delete();
    })->daily();
}

Is it possible? if not, what are the alternatives?

是否可以?如果没有,有哪些替代方案?

Thanks

谢谢

回答by Jonathon

I don't see why it wouldn't be possible. The Kernel::schedulemethod will be run every time php artisan schedule:runis run. If you set it up like the documentation, should be every minute via a cron.

我不明白为什么这不可能。该Kernel::schedule方法将在每次运行时php artisan schedule:run运行。如果你像文档一样设置它,应该每分钟通过一个 cron。

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

With that in mind, I don't see why you can't do something like this:

考虑到这一点,我不明白为什么你不能做这样的事情:

protected function schedule(Schedule $schedule)
{
    // Get all tasks from the database
    $tasks = Task::all();

    // Go through each task to dynamically set them up.
    foreach ($tasks as $task) {
        // Use the scheduler to add the task at its desired frequency
        $schedule->call(function() use($task) {
            // Run your task here
            $task->execute();
        })->cron($task->frequency);
    }
}

Depending on what you store, you can use whatever you like here instead of the CRON method. You might have a string stored in your database that represents one of Laravel's predefined frequencies and in which case you could do something like this:

根据您存储的内容,您可以在此处使用您喜欢的任何内容,而不是 CRON 方法。您可能在数据库中存储了一个字符串,代表 Laravel 的预定义频率之一,在这种情况下,您可以执行以下操作:

$frequency = $task->frequency; // everyHour, everyMinute, twiceDaily etc.
$schedule->call(function() {
    $task->execute();
})->$frequency();

The main thing to note here, is that the schedule isn't actually scheduling in tasks in the database or in a cron that it manages. Every time the scheduler runs (Every minute) it runs and it determines what to run based on the frequencies you give each task.

这里要注意的主要事情是,调度实际上并不是在数据库中的任务或它管理的 cron 中进行调度。调度程序每次运行时(每分钟)都会运行,并根据您为每个任务分配的频率确定要运行的内容。

Example:

例子:

  • You have a task set up using ->hourly(), that is, to run on the hour, every hour.
  • At 00:00, the schedule runs, the ->hourly()filter passes, because the time is on the hour, so your task runs.
  • At 00:01, the schedule runs and but this time the ->hourly()filter fails, so your task does not run.
  • 您有一个使用 设置的任务->hourly(),即每小时运行一次。
  • 00:00,计划运行,->hourly()过滤器通过,因为时间是整点,所以您的任务运行。
  • 在 时00:01,计划运行,但这次->hourly()过滤器失败,因此您的任务不会运行。