bvandenbos/resque-scheduler — pull request #179 by bsimpson (2012-08-29 01:04:13+00:00)
Title: Add remove_delayed_selection

I was working on a project for a client today and hit a snag with removing delayed jobs for deactivated accounts. We have numerous delayed jobs, and I wanted to deactivate all jobs for a given account. I found that using `remove_delayed` I would have to recreate the arguments to pass to `remove_delayed` exactly as they were passed to `enqueued_at`. The problem is these arguments aren't stored anywhere except in delayed_job. My delayed jobs took multiple arguments - one for an account.id, another for a user.id, and some take tertiary arguments as well. I was faced with generating every possible argument combination for a given account, for each delayed job class. This was simply too expensive to be practical.

I hope that this commit is something useful to others seeking a similar solution. I have created a new method `remove_delayed_selection` that takes a block, and optionally deletes a delayed job depending on the result of the block. I opted to pull the delayed_jobs into Ruby, decode the arguments, and expose them to the user. Like Ruby's `Array#delete_if`, if the block returns true, the delayed job is deleted. Otherwise, it is untouched.

I have also added test coverage for this new method, and updated the README to demonstrate its usage to users.

Thanks for the consideration in advance!