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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试