取消Laravel的工作 [英] Cancel Jobs In Laravel

查看:42
本文介绍了取消Laravel的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打以下电话:

return AdventureJob::dispatch($event->character->refresh(), $event->adventure, $event->levelsAtATime)->delay($timeTillFinished);

然后将创建延迟x分钟的作业.我的工作全部通过redis处理,是否有办法再获取此特定作业或从队列中删除此特定作业?

This will then create a job thats delayed x minutes. my jobs are all processed through redis, is there a way to then get this specific job or delete this specific job from the queue?

人们谈论php artisan命令然后删除所有作业,那不是我想要的,我想要获取有关此作业的某种信息(作业ID?或队列ID?Redis ID?),然后存储在数据库中,因此如果玩家随后取消冒险,我可以使用它在队列中找到该作业并将其删除(假设该作业未运行).

People talk about php artisan commands to then delete all jobs, thats not what I want I want to get some kind of information (Job ID? or queue ID? Redis ID?) about this job to then store in the database so that if the player then cancels the adventure, I can use that to find this job on the queue and delete it, assuming it's not running.

推荐答案

没有直接或简单的方法.延迟的作业作为待处理的时间保存在 sorted set 中,作为 score ,而作业有效负载作为 value .

There is no direct or easy way to do it. The delayed jobs are kept in sorted sets as to-be-processed time as score and job payload as the value.

有多种方法可以从已排序的集合中删除元素(大多数方法需要一些工作,具体取决于延迟队列的大小),例如

There are several ways to remove an element from the sorted sets(most of them require some efforts depending on the size of the delayed queue) such as

  • 您获得了确切"的分派作业的有效负载,然后使用 ZREM 将其删除.这很困难,因为对象(具有所有参数的作业的序列化版本)可能很大,并且您无法创建精确"对象.作业,因为它具有唯一的标识符.您可以使用 ZRANGEBYSCORE WITHSCORES 来获取列表.它将为您提供带分数的工作清单.您可以使用分数来识别要延迟的工作.获取值(序列化的有效负载),然后使用 ZREM .
  • 如果您在特定时间仅要处理一项作业,则可以将 ZREMRANGEBYSCORE 与使用处理的时间.如果到那时确实要处理n个作业,则其他作业也可以删除,因为 ZREMRANGEBYSCORE 需要一定的时间间隔.
  • 您可以尝试使用 ZSCAN 扫描整个延迟列表(使用分页)并找到得分和工作标识符,然后使用 ZREMRANGEBYLEX 和标识符将其删除.
  • 另一种方法是将取消条件放在 handle 方法的开头.这需要开发应用程序层.每当您将作业推送到队列时,您都会向该作业发送一个标识符,也将相同的标识符(您可以理解的)也放入Redis中( EXPIRE 大于延迟时间).当您想取消它时,请从Redis中删除它.在handle方法内部,检查给定的标识符是否在Redis中,如果不早于从代码块返回的话.
  • You get the "exact" payload of the dispatched job and then use ZREM to remove it. It is hard because the object(serialized version of the job with all the parameters) can be huge and you can't create the "exact" job because it has a unique identifier. You can get the list of it with ZRANGEBYSCORE and with WITHSCORES. It will give you the list of jobs with their scores. You can use score to identify to be delayed job. Get the value(serialized payload) then use ZREM.
  • If there is only one job to be processed at a specific time you, may use ZREMRANGEBYSCORE with using the processed time. If there are n jobs to be processed exactly that time then other jobs can be deleted too since ZREMRANGEBYSCORE takes time interval.
  • You may try to use ZSCAN to scan the whole delayed list(with pagination) and find the score and identifier of the job, and then use ZREMRANGEBYLEX with the identifier to remove it.
  • Another way could be putting a cancellation condition at the beginning of handle method. This one requires application layer development. Whenever you push a job to the queue you send an identifier to the job, put same identifier(that you can understand) in Redis too(with EXPIRE greater than the delayed time). When you want to cancel it, then delete it from the Redis. Inside the handle method check whether the given identifier exists in Redis, if not early return from the code block.

这篇关于取消Laravel的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆