Limit parallel jobs by args #626
-
Hey there :) I'm trying to limit the number of jobs running at the same time with the same arguments, but I'm not sure how to do it properly. I have a job defined like this:
This job gets inserted every time a user performs an action on their account, using their ClientID as an argument. I want to make sure there's only one "running" job per ClientID. So I tried:
But I have a lot of jobs in my river_job table, so the insertion is very slow. From what I understand from other discussions, it's because I'm not filtering on the default set of states. However, I can't filter on the default states because I want to allow the insertion of new jobs with arguments matching completed jobs. Is there a more efficient way to handle this? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@pablo-ruth based on the ByState: []rivertype.JobState{rivertype.JobStateAvailable, rivertype.JobStateCompleted, rivertype.JobStatePending, rivertype.JobStateRunning, rivertype.JobStateRetryable, rivertype.JobStateScheduled} You can customize this to remove However I want to point out that this isn't quite the same as a limit on parallel job execution. With the default |
Beta Was this translation helpful? Give feedback.
-
@bgentry I pushed the code with your advice into production, and it's working like a charm now! Thanks again ;) |
Beta Was this translation helpful? Give feedback.
Yes that sample looks correct. If you don't specify
ByState
you will get the default list of states in my previous response. However if you customize it there are certain states that are required (pending, scheduled, available, running). So as long as you have those 4 in the list you are free to add/remove the others.Correct, the latest implementation allows customizing the list as I've described above, and if you stay within those constraints the fast index-based unique path will be used. Even better, you can now still use unique jobs when using
InsertMany
.In t…