6

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
3
  • Up voted because a rails only way to do this would be nice. Personally I have used cron jobs to run rails scripts.
    – Jon
    Commented Apr 19, 2017 at 23:36
  • Just to clarify something about external gems vs ActiveJob: AJ provides a common interface to various queue adapters making it so the same code using, say, Resque, can be easily switched over to say, Sidekiq. You don't really get much benefit from AJ without using some tool outside of rails core, however.
    – Glyoko
    Commented Apr 20, 2017 at 19:16
  • Did anyone tried active_job ?
    – Aparichith
    Commented Nov 15, 2018 at 9:31

3 Answers 3

4

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?

2
  • 1
    Nice approach! Your approach will allow me to execute the job once a day without using an external gem, but how to run it initially?
    – F404
    Commented Apr 20, 2017 at 22:43
  • just enqueue it the first time from rails console bundle exec rails console - WeatherJob.perform_later will cause it to run immediately which will enqueue the record to run again 24 hours later
    – house9
    Commented Apr 21, 2017 at 1:59
2

Most of the major AJ adapters have some functionality for scheduling jobs, but some have that functionality separated into separate gems. For Resque for example, there's a separate resque-scheduler gem that works alongside the vanilla Resque gem for exactly this.

And here's the bit about recurring jobs. After some basic setup, you create a schedule.yml file defining how/when you want your job to run, e.g.

# config/initializers/resque_scheduler.rb
Resque.schedule = YAML.load_file('../calendar.yml')

# config/calendar.yml
your_recurring_job:
  cron: "0 0 * * *" # Every day at midnight
  class: "YourJobClass"
  queue: your_queue
  args:
  description: "This job does a thing, daily"

I don't know about every adapter, but the major ones usually have similar sister gems that provide this functionality.

2
  • To be clear, although this uses cron syntax, it does not go through cron. It works entirely within Redis/Resque.
    – Glyoko
    Commented Apr 20, 2017 at 19:17
  • Thanks for the explanation and the links. I need to investigate which is the role of Redis in the job scheduler process as both answers have suggested use external gem + Redis. But this will be a good topic for another question. Anyway, I probably use Resque.
    – F404
    Commented Apr 20, 2017 at 21:57
1

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'
2
  • Thank you for your answer. As both answers have confirmed, there is no built-in scheduler on Rails.
    – F404
    Commented Apr 20, 2017 at 21:58
  • No, It's not that ActiveJob provides you every bit of redis and sidekiq but it is the core of rails you'll have to first go through the documentation then you'll be able to handle this but redis and sidekiq will give you much better understanding of this also with ui. Commented Apr 24, 2017 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.