Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

delayed_job_recurring

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

delayed_job_recurring

  • 0.3.9
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

delayed_job_recurring

Build Status

Extends delayed_job to support recurring jobs.

Usage

Add it to your Gemfile:

gem 'delayed_job_recurring'

Then define a task class. We like the concept of interactors, so we put our task classes in app/interactors. You could also put them in lib or even app/models.

class MyTask
  include Delayed::RecurringJob
  run_every 1.day
  run_at '11:00am'
  timezone 'US/Pacific'
  queue 'slow-jobs'
  def perform
    # Do some work here!
  end
end

Finally, schedule it:

MyTask.schedule! # run every day at 11am Pacific time (accounting for daylight savings)

The best practice is to add your MyTask.schedule! lines to a rake file, e.g.

# lib/tasks/recurring_jobs.rake

namespace :recurring do
  task init: :environment do
    MyTask.schedule!
    MyOtherTask.schedule!

    if Rails.env.production?
      MyProductionOnlyTask.schedule!
    end
  end
end

and invoke this job by running rake recurring:init on each deployment.

Alternatively, if your app only has one instance running, you can put your schedule! calls into an initializer, and then your jobs will be automatically scheduled when your app starts. However, if you have more than one instance of your app running in production, this will lead to a race condition and you will end up with duplicate recurring jobs in the queue.

ActiveJob

Note: your task class should not inherit from ActiveJob::Base.

Advanced usage

Passing options to schedule

MyTask.schedule(run_at: '12:00')

Running at multiples times each day

MyTask.schedule(run_every: 1.day, run_at: ['11:00', '6:00pm'])

Running on specific days of the week

MyTask.schedule(run_every: 1.week, run_at: ['sunday 8:00am', 'wednesday 8:00am'])

Scheduling multiple jobs of same class

By default, before scheduling a new job, the old jobs scheduled with the same class will be unscheduled.

To schedule multiple jobs with same class, pass an unique matching param job_matching_param and value for that matching param in each job as below:

MyTask.schedule(run_at: '12:00', job_matching_param: 'schedule_id', schedule_id: 2)

This allows you to schedule multiple jobs with same class if value of the unique matching param(which is schedule_id in above example) is different in each job.

Thanks!

Many thanks to @ginjo and @kares for their work! This code was derived from https://gist.github.com/ginjo/3688965.

Contributing

Open Source Helpers

FAQs

Package last updated on 03 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc