Git Product home page Git Product logo

shoryuken-later's Introduction

shoryuken-later

A scheduling plugin for Shoryuken that uses Dynamo DB to delay messages arbitrarily far into the future.

Build Status Code Climate

Features

Supports distributed architectures

An SQS message is only queued if a conditional delete of the DDB item is successful. This eliminates any potential race condition, so if more than one shoryuken-later process is polling the same schedule table then no redundant SQS messages will be queued.

NOTE: You shouldn't really need to run more than one process, but if you do it will be safe.

One or more schedule tables

Supports polling one or more DynamoDB tables for messages.

later:
  tables:
    - default_schedule
    - other_schedule

Namespaced configuration

You can use the same configuration file for both Shoryuken and Shoryuken::Later, because the new configuration options are namespaced.

# These keys are used by both Shoryuken and Shoryuken::Later
aws:
  access_key_id:      ...       # or <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key:  ...       # or <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  region:             us-east-1 # or <%= ENV['AWS_REGION'] %>
logfile: some/path/to/file.log
  
# This key is only used by Shoryuken::Later
later:
  delay: 5 * 60   # How frequently to poll the schedule table, in seconds.
  pidfile: some/path/to/file.pid
  tables:
    - table1

# These keys are only used by Shoryuken
concurrency: 3
delay: 0
queues:
  - [queue1, 1]
  - [queue2, 2]

Usage

Starting the schedule poller

Start the shoryuken-later schedule poller with a command like:

bundle exec shoryuken-later --config shoryuken.yml

Run it as a daemon inside of your Rails app with a command like:

bundle exec shoryuken-later --config shoryuken.yml --rails --daemon

Command-line options

Integration with ActiveJob

A custom ActiveJob adapter can used to support delaying messages arbitrarily far into the future.

# config/application.rb
config.active_job.queue_adapter = :shoryuken_later

When you use the :shoryuken_later queue adapter, jobs to be performed farther than 15 minutes into the future (by setting the wait or wait_until ActiveJob options), will be inserted into the default schedule table. You can set the default schedule table in an initializer.

# config/initializers/shoryuken_later.rb
Shoryuken::Later.default_table = "#{Rails.env}_myapp_later"

Integration with Shoryuken::Worker

A new method named perform_later is added to Shoryuken::Worker allowing messages to be delayed arbitrarily far into the future. If the delay is 15 minutes or less, then the message is enqueued into the specified SQS :queue as usual. Otherwise, the message is inserted into the specified DynamoDB :schedule_table.

 require 'shoryuken-later'

 class MyWorker
   include Shoryuken::Worker
  
   shoryuken_options queue: 'default', schedule_table: 'default_schedule'
 end

 # Schedules a message to be processed 30 minutes from now.
 MyWorker.perform_later(Time.now + 30 * 60, 'Foobar')

Requirements

Ruby 2.0 or greater. Ruby 1.9 is no longer supported.

Installation

Add this line to your application's Gemfile:

gem 'shoryuken-later'

Or to get the latest updates:

gem 'shoryuken-later', github: 'joekhoobyar/shoryuken-later', branch: 'master'

And then execute:

$ bundle

Or install it yourself as:

$ gem install shoryuken-later

Documentation

Learn about using Shoryuken::Later at the Shoryuken::Later Wiki.

Learn about using Shoryuken at the Shoryuken Wiki.

Credits

Pablo Cantero, creator of Shoryuken, and everybody who contributed to it. I borrowed a lot of code from Shoryuken itself as a shortcut to making this gem.

Contributing

  1. Fork it ( https://github.com/joekhoobyar/shoryuken-later/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

shoryuken-later's People

Contributors

joekhoobyar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

shoryuken-later's Issues

Poller not finding messages

I may just be doing some math wrong, but I seem to be having a problem.

I have a table, production_scour-audio_shoryuken-later, that has a lot of items in it. One item has a perform_at value of 1427650294.

As I'm writing this, (Time.now + Shoryuken::Later::MAX_QUEUE_DELAY).to_i is 1427675975, which is greater than the perform_at value of that item.

Running client.first_item 'production_scour-audio_shoryuken-later', 'perform_at' => { attribute_value_list: [ (Time.now + Shoryuken::Later::MAX_QUEUE_DELAY).to_i ], comparison_operator: 'LT' } returns nil.

If I do a scan of the table in the AWS Console [perform_at, Number, less than, 1427675975], I get a bunch of results. So maybe this is just an issue with the AWS SDK? As best I could tell, this was all working yesterday.

Make the storage adapter configurable

Would be cool to make the storage adapter configurable to support other storages than DynamoDB such as: Redis, MongoDB, SQL etc.

Shoryuken::Later.storage_adapter = MyStorageAdapter

The storage adapter interface could respond to two methods fetch and create.

class MyStorageAdapter
  def fetch(limit = 1)
    # return an array 
  end

  def create(job)
    # persist job
  end
end

The fetch could accept a limit as SQS allows enqueuing up to 10 messages per send_message.

wdyt?

Wait Interval in certain cases becomes negative and causes a Fatal Crash in shoryken-later .

In cli.rb on line 68, the time_interval becomes negative in certain cases. This cases the process to crash on line 69 if "interval" is passed as a negative number.

interval = @timers.wait_interval
readable, writable = IO.select([@self_read], nil, nil, interval)

We were considering doing a pull request and changing line 68 to the following:

interval = @timers.wait_interval.abs

What do you think?
Thanks

Run shoryuken-later and shoryuken?

Just to make sure I'm understanding this right...

I have an existing setup that is a Rails app and a shoryuken process to consume messages. If I want to keep all that working, but add shoryuken-later, I would need to have both shoryuken-later and shoryuken processes running, right?

(i.e., all shoryuken-later is doing is polling for delayed messages; it won't actually consumer anything on the SQS queue, correct?)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.