doupao6698 2015-05-15 10:45
浏览 231
已采纳

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 11: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

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

报告相同问题?