Laravel-为每个高级用户安排工作

发布于 2025-02-13 18:24:25 字数 487 浏览 0 评论 0原文

用户购买高级时,我需要做的事情

,我给他保费。每周,我都需要在订阅时间再次提供保费。但是,如果用户不再是订户,则他们不应再收到保费。

我认为我需要使用计划的任务

,但我不知道如何以正确的方式做到这一点。

  1. 如何动态添加任务?
  2. 我需要将任务放在数据库任务表中吗?
  3. 如果未订阅用户,如何删除任务? (检查是在我的工作中进行的,但是如果退缩,如何删除任务)

代码

我需要运行此任务。

   $schedule->job(new CreditPremiumUser($userId))-> weeklyOn(1, '10:42:53');
   // 1 = premium purchase day
   // '10:42:53' = subscription time

What I need to do

When user purchase premium, I give him premium money. Every week, I need to give premium money again at subscription time. However, if the user is no longer a subscriber, they should no longer receive their premium money.

Questions

I think I need to use scheduled tasks, but I do not know how to do that on right way.

  1. How to add tasks dynamically ?
  2. Do I need to put the tasks in the database tasks table ?
  3. How to remove task if user is unsubscribed ? (the check is made in my job but how to remove the task if is unsubscribe)

Code

I need to run this task.

   $schedule->job(new CreditPremiumUser($userId))-> weeklyOn(1, '10:42:53');
   // 1 = premium purchase day
   // '10:42:53' = subscription time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2025-02-20 18:24:25

我确实

表任务

  Schema::create('tasks', function (Blueprint $table) {
     $table->id();
     $table->string('type'); // optional
     $table->string('frequency_function');
     $table->string('frequency_params')->nullable(); // optional
     $table->string('job_name')->nullable(); // optional
     $table->integer('user_id')->nullable(); // optional
     $table->timestamps();
   });
   /*
     Example task data
       type = 'subscription_credit'
       frequency_function = 'weeklyOn'
       frequency_params = 'a:2:{i:0;i:3;i:1;s:5:"12:20";}' // serialize(array(3, '12:20'))
       job_name = App/Jobs/Premium/CreditPremiumMoney
       user_id = 1
   */

创建了需要安排作业的 ,因此我有job_name列。

例如,根据您的需求,如果要安排命令的执行,则可以添加command_name列。

内核

在您的 App \ Console \ kernel.php 附表方法写作中

protected function schedule(Schedule $schedule)
{
    $tasks = Task::all();
    
    foreach ($tasks as $task) {
        $type = $task->type; // optional
        $frequencyFunc = $task->frequency_function;
        $frequencyFuncParams = $task->frequency_params
            ? unserialize($task->frequency_params)
            : [];
        
        if ($type === 'subscription_credit') { // optional
            $job = $task->job_name;
            $userId = $task->user_id;
            $schedule->job(new $job($userId), 'low', 'redis')
                ->$frequencyFunc(...$frequencyFuncParams);
        } else if ($type === '...') {
            // Other optional type logic
        }
    }
}

更新 类型逻辑(如果需要类型)。

尝试

将所有任务添加到数据库中。

本地

运行命令PHP工匠时间表:工作,调度程序将运行以在计划的Datatime上处理您的任务

生产

在这里

在每个环境上,调度程序将每分钟运行,并检查是否存在任务表中执行的任务。

What i did

Create table tasks

  Schema::create('tasks', function (Blueprint $table) {
     $table->id();
     $table->string('type'); // optional
     $table->string('frequency_function');
     $table->string('frequency_params')->nullable(); // optional
     $table->string('job_name')->nullable(); // optional
     $table->integer('user_id')->nullable(); // optional
     $table->timestamps();
   });
   /*
     Example task data
       type = 'subscription_credit'
       frequency_function = 'weeklyOn'
       frequency_params = 'a:2:{i:0;i:3;i:1;s:5:"12:20";}' // serialize(array(3, '12:20'))
       job_name = App/Jobs/Premium/CreditPremiumMoney
       user_id = 1
   */

I need to schedule Job so I have job_name column.

For example, depending on your needs, you can add command_name column if you want to schedule the execution of a command.

Update Kernel

In your App\Console\Kernel.php schedule method write:

protected function schedule(Schedule $schedule)
{
    $tasks = Task::all();
    
    foreach ($tasks as $task) {
        $type = $task->type; // optional
        $frequencyFunc = $task->frequency_function;
        $frequencyFuncParams = $task->frequency_params
            ? unserialize($task->frequency_params)
            : [];
        
        if ($type === 'subscription_credit') { // optional
            $job = $task->job_name;
            $userId = $task->user_id;
            $schedule->job(new $job($userId), 'low', 'redis')
                ->$frequencyFunc(...$frequencyFuncParams);
        } else if ($type === '...') {
            // Other optional type logic
        }
    }
}

You can remove type condition if you do not need it or add multiple types logic if you need type.

Try It

Add all your tasks to your database.

Local

Run command php artisan schedule:work and the scheduler will run to process your tasks at the scheduled dataTime

Production

Add CRON to your server like here

On each environment, the scheduler will run every minute and check if there are tasks to execute in tasks table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文