How to run delayed_job of a specific queue in test envoirnment? - delayed-job

I am trying to run specific delayed_jobs with the following script but its not working :
options[:queues] ||= [BULK_EMAIL_DJ_QUEUE]
Delayed::Worker.new(options).work_off
I have seen other solutions to run specifix delayed_jobs by running commands:
Delayed::Worker.new.run(Delayed::Job.last)
But i want to run all delayed jobs of a specific queue, how can i do that?

Delayed::Worker.new(queues: ['my_queue']).work_off
works for me with Rails 4.2, Delayed Job 4.1 and RSpec 3.5

Related

Sidekiq with Clockwork triggers but doesn't process job on heroku

Running: sidekiq 2.1.17, rails 3.2.21
I'm trying to use clockwork to schedule some recurring tasks to be done by sidekiq workers. Running foreman in development, everything runs perfectly as scheduled.
When I deploy to heroku, however, I get the "Triggering 'NameWorker.perform_async'" message in the logs at the appropriate times but then the respective jobs don't actually run.
When I instead call NameWorker.perform_async in a controller action, the job runs as it should. The jobs I'm trying to schedule just contain puts statements to verify that they're working. Anyone have any ideas about what I'm doing wrong? Thanks in advance.
Solved by adding the Sidekiq.configure_client logic from the sidekiq.rb file into clock.rb. This logic was already there and in unicorn but sidekiq was not being initialized in production for the purposes of the clockwork process.

Prevent sidekiq from executing queued up jobs when starting from command line?

When I start sidekiq in my development environment (Rails 3.2), I use the following command:
bundle exec sidekiq
When I do this, sidekiq will execute all jobs that have been queued up when it was not running. e.g. If I have created a bunch of new user accounts during testing, it will try and send welcome emails to all of the fake accounts (my emails are sent from a sidekiq job).
Is there a way to start sidekiq and tell it to delete all pending jobs? That way I can turn it back on without worrying that it will try and run a bunch of jobs that don't need to run (since this is my dev environment).
I have looked in documentation, but can't find an answer, hopefully it's something simple I overlooked...
redis-cli flushall && bundle exec sidekiq
I found a solution: Using the sidekiq monitoring UI that comes with sidekiq (https://github.com/mperham/sidekiq/wiki/Monitoring), I'm able to view all queues (even when sidekiq is not running). Deleting the queue will remove all of the jobs in it, which solves the problem.

Rails Active Job usage , or running watcher thread automatically with Rails

It's nice to see Rails 4.2 come with Active Job as a common interface for background jobs. But I can't find how to start a worker in the document. It seems that the document is still immature (e.g. the right version of Sneakers is only referred to in Rails' Gemfile), so I'm not sure if the "running workers" part is not in Active Job or just not mentioned in docs.
So with Active Job, do I still need to manually start the job watcher threads like sidekiq or in my case, rake sneakers:run? If so, where should I put these commands to let rails server run these parallel tasks automatically in a develop environment?
ActiveJob is just a common interface. You still need the backend gem, and you still need to launch it separately from your server (it is a separated process, which is the objective).
Sample using resque:
In the Gemfile:
gem 'resque'
In the terminal, launching a worker:
bin/resque work
The case is similary when using sidekick, delayed job or something else.
If you want to launch the server & worker in a single command, you can create a short bash script for it, but I would advise not doing so: having two separated console helps to watch what is happening on each side (web app & worker).
A better solution would be to use the foreman gem to manage starting & stopping your process.
You can create a simple Procfile with the processes to start:
web: bundle exec rails s
job: bundle exec resque work
And then just start both using foreman:
foreman start
By default, foreman will interleave the logs of the process in the console, but this can be configured.
You still have to run the job thread watcher.

Best way to run a nightly report on a Rails app running on Heroku

I have a Rails app I'm running on Heroku.
I'd like to, every midnight, run a few queries on my database (I could do it with ActiveRecord or SQL, not fussy), do some light processing of the results, and send a formatted email with the data (either inline or attached CSV).
What's the best way to do this? Locally I could just schedule a cron job to run a script, with Heroku I'm not sure. I read that heroku has a cron add-on, but couldn't find it, and I'm not sure what functionality is available there anyways. Can I just do a headless attach to a rails console and get the output? Can I do this as a rake task?
Any information would be appreciated.
The [Heroku Scheduler addon] mentioned is a good way to do the scheduling.
class Report
def self.generate_and_send
# Your logic here
end
end
# To run task in Terminal
$ heroku run rails runner Report.generate_and_send
# What to schedule on Heroku Scheduler
rails runner Report.generate_and_send

Why would my rake tasks running via cron get invoked twice?

I have a rails app with the whenever gem installed to setup cron jobs which invoke various rake tasks. For reasons unbeknownst to me, each rake task gets invoked twice at precisely the same time. So my db backup task backs up the db twice at 4:00am.
Inspecting crontab reveals correct syntax for all of the cron jobs, so I don't think this is an issue with the whenever gem not correctly configuring the cron jobs. Also confusing is that in both staging and production environments and can invoke tasks on the command line and they only run once.
Any thoughts on what would cause this? I'm at a complete loss troubleshooting wise.
The number of cron jobs that run depends on the number of application instances running in the server box. Are you have two instances of rails application running in the same server box?

Resources