Skip to content
/ schked Public

Framework agnostic scheduler to run recurring jobs.

License

Notifications You must be signed in to change notification settings

bibendi/schked

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8d4a2e8 · Sep 6, 2023

History

77 Commits
May 23, 2019
Sep 6, 2023
Dec 29, 2018
Nov 15, 2022
Sep 6, 2023
Sep 6, 2023
Sep 6, 2023
Jul 9, 2021
Jun 13, 2019
Jul 9, 2021
Sep 6, 2023
Dec 29, 2018
May 23, 2019
Dec 29, 2018
Dec 29, 2018
Sep 6, 2023
Mar 7, 2020
Sep 6, 2023
Oct 9, 2022
Oct 19, 2022
Oct 19, 2022
Sep 6, 2023

Repository files navigation

Gem Version Build Status

Schked

Framework agnostic Rufus-scheduler wrapper to run recurring jobs.

Sponsored by Evil Martians

Installation

Add this line to your application's Gemfile:

gem "schked"

And then execute:

bundle

Or install it yourself as:

gem install schked

Usage

Ruby on Rails

.schked

--require config/environment.rb

config/schedule.rb

cron "*/30 * * * *", as: "CleanOrphanAttachmentsJob", timeout: "60s", overlap: false do
  CleanOrphanAttachmentsJob.perform_later
end

If you have a Rails engine with own schedule:

engine-path/lib/foo/engine.rb

module Foo
  class Engine < ::Rails::Engine
    initializer "foo" do |app|
      Schked.config.paths << root.join("config", "schedule.rb")
    end
  end
end

And run Schked:

bundle exec schked start

To show schedule:

bundle exec schked show

Duplicate scheduling

When you deploy your schedule to production, you want to start new instance before you shut down the current. And you don't want simultaneous working of both. To achieve a seamless transition, Schked is using Redis for locks.

You can configure Redis client as the following:

Schked.config.redis = {url: ENV.fetch("REDIS_URL") }

Callbacks

Also, you can define callbacks for errors handling:

config/initializers/schked.rb

Schked.config.register_callback(:on_error) do |job, error|
  Raven.capture_exception(error) if defined?(Raven)
end

There are :before_start, :after_finish and :around_job callbacks as well.

Warning: :before_start and :after_finish callbacks are executed in the scheduler thread, not in the work threads (the threads where the job execution really happens).

:around_job callback is executed in the job's thread.

Schked.config.register_callback(:around_job) do |job, &block|
  ...
  block.call
  ...
end

Logging

By default Schked writes logs into stdout. In Rails environment Schked is using application logger. You can change it like this:

config/initializers/schked.rb

Schked.config.logger = Logger.new(Rails.root.join("log", "schked.log"))

Monitoring

Yabeda::Schked - built-in metrics for monitoring Schked recurring jobs out of the box! Part of the yabeda suite.

Testing

describe Schked do
  let(:worker) { described_class.worker.tap(&:pause) }

  around do |ex|
    Time.use_zone("UTC") { Timecop.travel(start_time, &ex) }
  end

  describe "CleanOrphanAttachmentsJob" do
    let(:start_time) { Time.zone.local(2008, 9, 1, 10, 42, 21) }
    let(:job) { worker.job("CleanOrphanAttachmentsJob") }

    specify do
      expect(job.next_time.to_local_time)
        .to eq Time.zone.local(2008, 9, 1, 11, 0, 0)
    end

    it "enqueues job" do
      expect { job.call(false) }
        .to have_enqueued_job(CleanOrphanAttachmentsJob)
    end
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bibendi/schked. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Schked project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.