mhz
mhz

Reputation: 1029

Rails Sucker Punch - How to start jobs without visiting homepage?

I'm trying to figure out how to have my background jobs in SuckerPunch start work without having to load the index page.

Right now, what I do is:

$ rails server #starts the server at localhost:3000

Then I go to my browser URL and enter localhost:3000

This triggers my SuckerPunch jobs, and they start running.

my application_controller.rb

class ApplicationController < ActionController::Base
    
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception

    # call job on application start
    TestEchoJob.new.async.perform("Download CMS Data")
    GetImages.new.async.perform
    DeleteImages.new.async.perform
    SendLead.new.async.perform

end

That's where I invoke the jobs. I'm thinking maybe there is another place I can put them so I can achieve what I want.

Start the jobs on server start - not http request.

Upvotes: 0

Views: 1177

Answers (1)

Mitch VanDuyn
Mitch VanDuyn

Reputation: 2878

Sorry not be very specific, but I think all the info is in the sucker punch gem readme. It will depend on your configuration, so please read that.

For example: (from the github repo)

Active Job

Sucker Punch has been added as an Active Job adapter in Rails 4.2. See the guide for configuration and implementation.

Add Sucker Punch to your Gemfile:

gem 'sucker_punch'

And then configure the backend to use Sucker Punch:

# config/initializers/sucker_punch.rb
Rails.application.configure do
  config.active_job.queue_adapter = :sucker_punch
end

Upvotes: 0

Related Questions