doupao6698 2015-05-15 18:45
浏览 224
已采纳

Laravel每5秒运行一次工匠命令

I am working with a system that sends me webhooks anytime a resource in that system changes. The webhook contains the id of the resource that was updated. For example, if someone edits product ID 1234 in this system, my server will receive an alert saying that product 1234 has changed. Then I make a request to their API to fetch the newest data for product 1234 and save it to my system.

I am building this process to work asynchronously. Meaning, every time I receive a webhook, I save the details to a database table that logs the ID of the resource. I then have a WebhookQueue class that contains a run() method, which processes all of the queued requests and updates the appropriate products. Here's the code from the WebhookQueue class:

public static function run()
{
        //get request data
        $requests = WebhookRequest::select(
                        'webhook_type',
                        'object_ext_id',
                        'object_ext_type_id',
                        \DB::raw('max(created_at) as created_at')
                )
                ->groupBy(['webhook_type', 'object_ext_id', 'object_ext_type_id'])
                ->get();

        foreach ($requests as $request) {
                // Get the model for each request.
                // Make sure the model is not currently syncing.
                // Sync the model.
                // Delete all webhook request of the same type that were created before created_at on the request
                if ($request->webhook_type == 'product') {
                        $model = Product::where([
                                        'ext_id'=> $request->object_ext_id,
                                        'ext_type_id'=> $request->object_ext_type_id
                                ])->firstOrFail();

                        if (!$model->is_syncing) {
                                $model->syncWithExternal();

                                WebhookRequest::where([
                                        'webhook_type'=>$request->webhook_type,
                                        'object_ext_id'=>$request->object_ext_id,
                                        'object_ext_type_id'=>$request->object_ext_type_id,
                                ])
                                ->where('created_at', '<=', $request->created_at)
                                ->delete();
                        }
                }
        }
}

I've also created a command that simply executes a line of code to process the queue. This command is php artisan run-webhook-queue.

My plan was to run this command via a cron job every 5 seconds, but I have just come to learn that cron jobs can not be scheduled more granularly than by minute.

How can I get this command to run every 5 seconds, or is there some other way I should be handling this scenario? I don't know anything about Laravel Queues, but it seems like I should be using that.

  • 写回答

1条回答 默认 最新

  • dongmeiran609914 2015-05-15 19:15
    关注

    Laravel Worker Queues handles this pretty well and will allow you to run command every 5 seconds. If you use Forge, setup is hardly any work at all.

    Here is a guide using Forge: https://mattstauffer.co/blog/laravel-forge-adding-a-queue-worker-with-beanstalkd

    Here is a guide if you are not using Forge: http://fideloper.com/ubuntu-beanstalkd-and-laravel4

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'