简体   繁体   English

Laravel队列-限制同时执行的任务数

[英]Laravel queues - limit number of simultaneous tasks

I have written a Laravel task which reads data from db, calls a shell script to process data, and that saves results to db. 我编写了一个Laravel任务,该任务从db读取数据,调用Shell脚本来处理数据,然后将结果保存到db。 Tasks are queued very fast, and multiple tasks always exist in the queue. 任务排入队列非常快,并且队列中始终存在多个任务。 I noticed that tasks are not performed one by one, but instead couple of tasks are executed simultaneously. 我注意到任务不是一个接一个地执行,而是同时执行几个任务。 This is good principle, generally speaking, but it seems that shell script I call(which is btw third-party and I cannot modify it) is not thread-safe and it creates problems when called simultaneously by multiple tasks. 一般而言,这是一个很好的原则,但是似乎我调用的shell脚本(是第三方的,我无法修改)不是线程安全的,并且在由多个任务同时调用时会产生问题。 When I am executing task function synchronously, as part of my main process, everything works smooth. 当我同步执行任务功能时,作为我的主要流程的一部分,一切都会顺利进行。

What I would like to know is if it is possible to tell Laravel queue process one task at a time, because I still want to split this job from my main processing, but also have to avoid these errors. 我想知道的是,是否有可能一次告诉Laravel队列处理一个任务,因为我仍然想将此工作与我的主要处理分开,而且还必须避免这些错误。 I am on Laravel 5, using default 'sync' queue method. 我在Laravel 5上,使用默认的“同步”队列方法。

About the tasks being executed simultaneously, have you tried adding withoutOverlapping ? 关于同时执行的任务,您是否尝试过添加而没有withoutOverlapping http://laravel.com/docs/5.1/scheduling#preventing-task-overlaps http://laravel.com/docs/5.1/scheduling#preventing-task-overlaps

If you're using a closure call, you'd also need to add a name 如果使用闭包调用,则还需要添加一个name

For example: 例如:

$schedule->call(function () {
    DB::table('users')->insert([
        'user_id'       => '3',
        'name'          => 'anton'
    ]);
})->everyHour()
  ->name('nyanpasu')
  ->withoutOverlapping();

I couldn't find any answer that could fit my needs and this question is in top 10 search results for "laravel limit one job", so I decided that I'll share my solution to this problem. 我找不到任何适合我需要的答案,并且这个问题出现在“ laravel限制一项工作”的前10个搜索结果中,因此我决定我将分享我对这个问题的解决方案。 maybe it'll help someone else. 也许会帮助别人。

I had jobs overlapping for an API requests which resulted in 500 error on the external server. 我的API要求有重叠的工作,导致外部服务器上出现500错误。 To overcome this issue I wanted to limit the execution to one job. 为了克服这个问题,我想将执行限制为一项工作。

The simplest solution for me was to create another Supervisor configuration file for one specific queue - in my case "api" - which I limited in the config to one proces with numprocs=1 command. 对我来说,最简单的解决方案是为一个特定的队列 (在我的情况下为“ api”) 创建另一个Supervisor配置文件 ,我使用numprocs=1命令将其在配置中限制为一个进程。

After this I can dispatch all API jobs for external servers to queue "api" and they never overlap. 此后,我可以将外部服务器的所有API作业分派到“ api”队列中,并且它们永远不会重叠。

I don't know if it's the best approach but it fixed my problems with minimum effort. 我不知道这是否是最好的方法,但是它以最小的努力解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM