問題描述
如果我調用以下內容:
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 useZREM
. - 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(withEXPIRE
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 中取消作業的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!