All Projects → ondrejbartas → Sidekiq Cron

ondrejbartas / Sidekiq Cron

Licence: mit
Scheduler / Cron for Sidekiq jobs

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Sidekiq Cron

Cronscheduler.aspnetcore
Cron Scheduler for AspNetCore 2.x/3.x or DotNetCore 2.x/3.x Self-hosted
Stars: ✭ 100 (-92.77%)
Mutual labels:  scheduler, scheduled-jobs, cron-jobs
Activejob Scheduler
A background job scheduler for any queue backend
Stars: ✭ 24 (-98.26%)
Mutual labels:  sidekiq, scheduled-jobs, ruby-on-rails
request store-sidekiq
Provides an easy integration between RequestStore and Sidekiq
Stars: ✭ 32 (-97.69%)
Mutual labels:  sidekiq, ruby-gem, ruby-on-rails
Chronos
Fault tolerant job scheduler for Mesos which handles dependencies and ISO8601 based schedules
Stars: ✭ 4,303 (+211.14%)
Mutual labels:  scheduled-jobs, cron-jobs
Pattern
A collection of lightweight, standardized, rails-oriented patterns.
Stars: ✭ 377 (-72.74%)
Mutual labels:  ruby-on-rails, ruby-gem
Devise masquerade
Extension for devise, enable login as functionality. Add link to the masquerade_path(resource) and use it.
Stars: ✭ 380 (-72.52%)
Mutual labels:  ruby-on-rails, ruby-gem
delayed-web
A rails engine that provides a simple web interface for exposing the Delayed::Job queue.
Stars: ✭ 71 (-94.87%)
Mutual labels:  ruby-gem, ruby-on-rails
Motion
Reactive frontend UI components for Rails in pure Ruby
Stars: ✭ 498 (-63.99%)
Mutual labels:  ruby-on-rails, ruby-gem
Matestack Ui Core
Matestack enables you to create sophisticated, reactive UIs in pure Ruby, without touching JavaScript and HTML. You end up writing 50% less code while increasing productivity, maintainability and developer happiness.
Stars: ✭ 469 (-66.09%)
Mutual labels:  ruby-on-rails, ruby-gem
React on rails
Integration of React + Webpack + Rails + rails/webpacker including server-side rendering of React, enabling a better developer experience and faster client performance.
Stars: ✭ 4,815 (+248.16%)
Mutual labels:  ruby-on-rails, ruby-gem
Flask Apscheduler
Adds APScheduler support to Flask
Stars: ✭ 741 (-46.42%)
Mutual labels:  scheduler, cron-jobs
Tiktok
Python web visualize build on the awesome web framework sanic
Stars: ✭ 55 (-96.02%)
Mutual labels:  scheduler, cron-jobs
Loaf
Manages and displays breadcrumb trails in Rails app - lean & mean.
Stars: ✭ 360 (-73.97%)
Mutual labels:  ruby-on-rails, ruby-gem
josk
🏃🤖 Scheduler and manager for jobs and tasks in node.js on multi-server and clusters setup
Stars: ✭ 27 (-98.05%)
Mutual labels:  scheduler, scheduled-jobs
Cloudinary gem
Cloudinary GEM for Ruby on Rails integration
Stars: ✭ 394 (-71.51%)
Mutual labels:  ruby-on-rails, ruby-gem
nokul
The Nokul Project
Stars: ✭ 47 (-96.6%)
Mutual labels:  sidekiq, ruby-on-rails
Quartznet
Quartz Enterprise Scheduler .NET
Stars: ✭ 4,825 (+248.88%)
Mutual labels:  scheduler, scheduled-jobs
arask
Automatic RAils taSKs.
Stars: ✭ 31 (-97.76%)
Mutual labels:  ruby-gem, ruby-on-rails
katapult
Kickstart Rails development!
Stars: ✭ 21 (-98.48%)
Mutual labels:  ruby-gem, ruby-on-rails
Ransack
Object-based searching.
Stars: ✭ 5,020 (+262.98%)
Mutual labels:  ruby-on-rails, ruby-gem

Sidekiq-Cron Gem Version Build Status Coverage Status

Join the chat at https://gitter.im/ondrejbartas/sidekiq-cron

Introduction video about Sidekiq-Cron by Drifting Ruby

A scheduling add-on for Sidekiq.

Runs a thread alongside Sidekiq workers to schedule jobs at specified times (using cron notation * * * * * parsed by Fugit, more about cron notation.

Checks for new jobs to schedule every 30 seconds and doesn't schedule the same job multiple times when more than one Sidekiq worker is running.

Scheduling jobs are added only when at least one Sidekiq process is running, but it is safe to use Sidekiq-Cron in environments where multiple Sidekiq processes or nodes are running.

If you want to know how scheduling work, check out under the hood

Works with ActiveJob (Rails 4.2+)

You don't need Sidekiq PRO, you can use this gem with plain Sidekiq.

Upgrade from <0.6x to 1.0.x

Please be aware that sidekiq-cron < 1.0 was relying on rufus-scheduler < 3.5. Using those older sidekiq-cron with rufus-scheduler >= 3.5 ends up with jobs failing on creation. Sidekiq-cron 1.0 includes a patch that switches from rufus-scheduler to rufus-scheduler's core dependency, fugit.

Requirements

  • Redis 2.8 or greater is required. (Redis 3.0.3 or greater is recommended for large scale use)
  • Sidekiq 5, or 4, or 3 and greater is required (for Sidekiq < 4 use version sidekiq-cron 0.3.1)

Change Log

before upgrading to new version, please read: Change Log

Installation

$ gem install sidekiq-cron

or add to your Gemfile

gem "sidekiq-cron", "~> 1.1"

Getting Started

If you are not using Rails, you need to add require 'sidekiq-cron' somewhere after require 'sidekiq'.

Job properties:

{
 'name'  => 'name_of_job', #must be uniq!
 'cron'  => '1 * * * *',  # execute at 1 minute of every hour, ex: 12:01, 13:01, 14:01, 15:01...etc(HH:MM)
 'class' => 'MyClass',
 #OPTIONAL
 'queue' => 'name of queue',
 'args'  => '[Array or Hash] of arguments which will be passed to perform method',
 'date_as_argument' => true, # add the time of execution as last argument of the perform method
 'active_job' => true,  # enqueue job through rails 4.2+ active job interface
 'queue_name_prefix' => 'prefix', # rails 4.2+ active job queue with prefix
 'queue_name_delimiter' => '.',  # rails 4.2+ active job queue with custom delimiter
 'description' => 'A sentence describing what work this job performs.'  # Optional
}

Time, cron and sidekiq-cron

For testing your cron notation you can use crontab.guru.

sidekiq-cron uses Fugit to parse the cronline. If using Rails, this is evaluated against the timezone configured in Rails, otherwise the default is UTC.

If you want to have your jobs enqueued based on a different time zone you can specify a timezone in the cronline, like this '0 22 * * 1-5 America/Chicago'.

See rufus-scheduler documentation for more information. (note. Rufus scheduler is using Fugit under the hood, so documentation for Rufus Scheduler can help you also)

What objects/classes can be scheduled

Sidekiq Worker

In this example, we are using HardWorker which looks like:

class HardWorker
  include Sidekiq::Worker
  def perform(*args)
    # do something
  end
end

Active Job Worker

You can schedule: ExampleJob which looks like:

class ExampleJob < ActiveJob::Base
  queue_as :default

  def perform(*args)
    # Do something
  end
end

Adding Cron job:

class HardWorker
  include Sidekiq::Worker
  def perform(name, count)
    # do something
  end
end

Sidekiq::Cron::Job.create(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker') # execute at every 5 minutes, ex: 12:05, 12:10, 12:15...etc
# => true

create method will return only true/false if job was saved or not.

job = Sidekiq::Cron::Job.new(name: 'Hard worker - every 5min', cron: '*/5 * * * *', class: 'HardWorker')

if job.valid?
  job.save
else
  puts job.errors
end

#or simple

unless job.save
  puts job.errors #will return array of errors
end

Load more jobs from hash:

hash = {
  'name_of_job' => {
    'class' => 'MyClass',
    'cron'  => '1 * * * *',
    'args'  => '(OPTIONAL) [Array or Hash]'
  },
  'My super iber cool job' => {
    'class' => 'SecondClass',
    'cron'  => '*/5 * * * *'
  }
}

Sidekiq::Cron::Job.load_from_hash hash

Load more jobs from array:

array = [
  {
    'name'  => 'name_of_job',
    'class' => 'MyClass',
    'cron'  => '1 * * * *',
    'args'  => '(OPTIONAL) [Array or Hash]'
  },
  {
    'name'  => 'Cool Job for Second Class',
    'class' => 'SecondClass',
    'cron'  => '*/5 * * * *'
  }
]

Sidekiq::Cron::Job.load_from_array array

Bang-suffixed methods will remove jobs that are not present in the given hash/array, update jobs that have the same names, and create new ones when the names are previously unknown.

Sidekiq::Cron::Job#load_from_hash! hash
Sidekiq::Cron::Job#load_from_array! array

or from YML (same notation as Resque-scheduler)

#config/schedule.yml

my_first_job:
  cron: "*/5 * * * *"
  class: "HardWorker"
  queue: hard_worker

second_job:
  cron: "*/30 * * * *" # execute at every 30 minutes
  class: "HardWorker"
  queue: hard_worker_long
  args:
    hard: "stuff"
#initializers/sidekiq.rb
schedule_file = "config/schedule.yml"

if File.exist?(schedule_file) && Sidekiq.server?
  Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
end

or you can use for loading jobs from yml file sidekiq-cron-tasks which will add rake task bundle exec rake sidekiq_cron:load to your rails application.

Finding jobs

#return array of all jobs
Sidekiq::Cron::Job.all

#return one job by its unique name - case sensitive
Sidekiq::Cron::Job.find "Job Name"

#return one job by its unique name - you can use hash with 'name' key
Sidekiq::Cron::Job.find name: "Job Name"

#if job can't be found nil is returned

Destroy jobs:

#destroys all jobs
Sidekiq::Cron::Job.destroy_all!

#destroy job by its name
Sidekiq::Cron::Job.destroy "Job Name"

#destroy found job
Sidekiq::Cron::Job.find('Job name').destroy

Work with job:

job = Sidekiq::Cron::Job.find('Job name')

#disable cron scheduling
job.disable!

#enable cron scheduling
job.enable!

#get status of job:
job.status
# => enabled/disabled

#enqueue job right now!
job.enque!

How to start scheduling? Just start Sidekiq workers by running:

sidekiq

Web UI for Cron Jobs

If you are using Sidekiq's web UI and you would like to add cron jobs too to this web UI, add require 'sidekiq/cron/web' after require 'sidekiq/web'.

With this, you will get: Web UI

Forking Processes

If you're using a forking web server like Unicorn you may run into an issue where the Redis connection is used before the process forks, causing the following exception

Redis::InheritedError: Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.

to occur. To avoid this, wrap your job creation in the call to Sidekiq.configure_server:

Sidekiq.configure_server do |config|
  schedule_file = "config/schedule.yml"

  if File.exist?(schedule_file)
    Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
  end
end

Note that this API is only available in Sidekiq 3.x.x.

Under the hood

When you start the Sidekiq process, it starts one thread with Sidekiq::Poller instance, which perform the adding of scheduled jobs to queues, retries etc.

Sidekiq-Cron adds itself into this start procedure and starts another thread with Sidekiq::Cron::Poller which checks all enabled Sidekiq cron jobs every 30 seconds, if they should be added to queue (their cronline matches time of check).

Sidekiq-Cron is checking jobs to be enqueued every 30s by default, you can change it by setting:

Sidekiq.options[:poll_interval] = 10

Sidekiq-Cron is safe to use with multiple sidekiq processes or nodes. It uses a Redis sorted set to determine that only the first process who asks can enqueue scheduled jobs into the queue.

Thanks to

Contributing to sidekiq-cron

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2013 Ondrej Bartas. See LICENSE.txt for further details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].