Skip to content

Add support for lock_ttl to be a Proc/class method#879

Merged
mhenrixon merged 2 commits intomhenrixon:mainfrom
brayden-onesignal:lock_ttl_fn/main
May 25, 2025
Merged

Add support for lock_ttl to be a Proc/class method#879
mhenrixon merged 2 commits intomhenrixon:mainfrom
brayden-onesignal:lock_ttl_fn/main

Conversation

@brayden-onesignal
Copy link
Copy Markdown
Contributor

Same as #878 but to upstream into main

Adds the ability to set the lock_ttl to a Proc or class method, allowing callers to set the TTL based on job arguments or other runtime information.

For my case, I need to set the lock_ttl dynamically based on one of the arguments supplied. I can accomplish this with a Sidekiq Middleware that runs before SidekiqUniqueJobs, but I'd prefer to provide a proc to calculate.

Sample Job

class DoSomethingCoolWorker
  # ...
  sidekiq_options lock: :until_expired, lock_ttl: :until_end_of_trial

  def self.until_end_of_trial(args)
    user = User.find(args.first)
    user.trial_ends_at - Time.zone.now
  end
  
  def perform(user_id)
    # ...
  end
end

@brayden-onesignal
Copy link
Copy Markdown
Contributor Author

Codeclimbate seems to be failing on TODOs that existed before this PR

Copy link
Copy Markdown
Owner

@mhenrixon mhenrixon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid addition!

Add the ability to set the lock_ttl to a callable object, such as a Proc
or class method. Gives the ability for callers to set the lock_ttl
based on the job arguments or other runtime information.
@mhenrixon mhenrixon merged commit 8048de5 into mhenrixon:main May 25, 2025
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants