Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule an ActiveJob in Rails

I'm doing a weather API which will get, process and save data from another API. In order to get the daily updates (request URL info, get the JSON/XML data, construct my data and save it to my database) I think the most proper way is to use an ActiveJob.

I want to schedule the job to run periodically. I would like something like UNIX cron or Spring @Scheduled annotation for Java.

I have seen another questions on Stack Overflow(this one) about scheduling jobs but they use external gems like whenever. I have been looking for a backend that allows to execute the job in the Rails API (Backends), but it seems that none of the available allows scheduling a job.

Is there anything on the Rails framework (version 5) that allows me to do what I'm trying to? Or I must use an external gem?

Thank you so much.

Edit If is useful for anyone, here is the schema for the job:

class ImportDailyDataJob < ApplicationJob
  queue_as :default

  def perform(*args)
    # Do something later
  end

  private

  def prepare_request
    # return the request object
  end

  def request_data
    # Get the data from external API.
  end

  def process_data
    # Process the data
  end

  def save_processed_data
    # Saves the data to the database
  end
end
like image 819
F404 Avatar asked Apr 19 '17 21:04

F404


People also ask

What is active job in rails?

Active Job is introduced in Rails 4.2 version, Active Job in Rails is a framework for creating, scheduling and executing background Jobs.

What is a background job in rails?

Now it is the time to define Background Job. Active Job is introduced in Rails 4.2 version, Active Job in Rails is a framework for creating, scheduling and executing background Jobs. What are background Jobs?

What kind of jobs can you do with rails?

These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really. The main point is to ensure that all Rails apps will have a job infrastructure in place.

What is active job?

1 What is Active Job? Active Job is a framework for declaring jobs and making them run on a variety of queuing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really.


2 Answers

This is not real scheduling, but you can fake it in certain situations if you don't need the scheduling to be totally accurate, i.e. in your case run the job once a day:

class WeatherJob < ApplicationJob
  def perform
    reschedule_job

    do_work
  end

  def do_work
    # ...
  end

  def reschedule_job
    self.class.set(wait: 24.hours).perform_later
  end
end

enqueuing jobs to run in the future using ActiveJob should work with DelayedJob or Sidekiq, might not be supported in all active job backends?

like image 87
house9 Avatar answered Oct 02 '22 13:10

house9


I think you should go for sidekiq using redis write a task to schedule a job, to do so just install these two gem and can go for their documentation:

gem 'redis-rails'

then install sidekiq

gem 'sidekiq'
like image 29
nitanshu verma Avatar answered Oct 02 '22 11:10

nitanshu verma