Clearing stuck jobs in Huginn

  libreops howto huginn database ruby rails

From time to time the job workers in Huginn will lock up. This usually happens if they are subjected to an external resource which can be contacted but never seems to respond. A stuck webapp on the other end is usually the problem. If the connection never dies, or takes a long time to time out it can wreak havoc. However, there's a relatively easy way to fix this. First, you have to shut down your job workers. Depending on how many you have this can take a while... once they're down, though, it's a relatively simple matter to use the Rails console to clean out the bad jobs.

drwho@exocortex:$ cd /path/to/huginn
drwho@exocortex:$ source .env

# The --environment=prod bit might be redundant, but it couldn't hurt.
# Also, this can take a few minutes to come up so be patient.
drwho@exocortex:$ bundle exec rails console --environment=prod

Once the Rails console is up, the following command will blow away the cruft:

Delayed::Job.where("locked_at IS NOT NULL AND locked_by IS NOT NULL AND failed_at IS NULL").destroy_all

This command can take a while to finish. The longest I've waited is about ten minutes. But it will finish. When it does, you can turn the above command into a read-only query to see if there are any bad jobs left:

Delayed::Job.where("locked_at IS NOT NULL AND locked_by IS NOT NULL AND failed_at IS NULL")

However, there shouldn't be.