2023-05-12 开启多语言插件支持……

delaying job queue on lumen 7 在lumen7中使用延迟队列

php 苏 demo 3500℃
公司有个项目采用的lumen后台框架,由于业务需要使用延迟队列。
技术小伙伴们反馈只用过: 
dispatch(new Jobs());

不知道怎么实现延迟队列,搜了xueyuanjun和中文文档说明的写法:

ProcessPodcast::dispatch($podcast)
              ->delay(now()->addMinutes(10));
提示:
Call to undefined method App\Jobs\GetCityInfoDelay::dispatch()
发现在stackoverflow上也有一些国外的道友有提问:
I have a project on Lumen 7 which includes a lot of queues but I can't get to delay the job.

This is how I dispatch a job:
dispatch(new NotifyJob($user, $template));

but when I add this:
 dispatch(new NotifyJob($user, $template))->delay(now()->addMinutes(10));

I get this error

"Call to undefined method Laravel\\Lumen\\Bus\\PendingDispatch::delay()"

    And doing this EmailJob::dispatch($user, $template); gives me Undefined method dispatch
I just can\'t get the delay to work.
但是, no one answers.
通常什么情况下的提问会是没有人愿意回答呢?
第一种:世上的人都不会,显然这种情况不太可能。
第二种,太简单,懒得理。
记住一句话:当你在使用别人东西的时候,如果发现不会用,一定要好好翻翻原版手册。
记住第二句话:不要经常吃别人嚼过的东西,然后还感觉很香,导致丧失了自己撕咬品尝的能力。
于是找到了lumen的官方文档,关于队列的使用说明 有这么一段:
 you may use the dispatch function to dispatch jobs from anywhere within your Lumen application:
    dispatch(new ExampleJob);
Of course, you may also use the Queue facade.
If you choose to use the facade, be sure to uncomment the call to $app->withFacades() in your bootstrap/app.php file:
Queue::push(new ExampleJob);
Queue 是lumen里的一种门面模式,里面支持Queue的多种方法,例如延迟执行可以使用以下:
Queue::later(Carbon::now()->addMinutes(10),new GetCityInfoDelay());
在lumen里使用队列服务和laravel里是存在区别的,lumen7 不提供jobs生成器,如果需要可以复制ExampleJob 文件,然后改造。
自定义Job继承Job ,job 使用了 InteractsWithQueue, Queueable, SerializesModels traits.

打赏

转载请注明:苏demo的别样人生 » delaying job queue on lumen 7 在lumen7中使用延迟队列

   如果本篇文章对您有帮助,欢迎向博主进行赞助,赞助时请写上您的用户名。
支付宝直接捐助帐号oracle_lee@qq.com 感谢支持!
喜欢 (5)or分享 (0)