0.0
No commit activity in last 3 years
No release in over 3 years
A easy-use and clean wrapper for sidekiq worker.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

DelayedWorker Build Status Gem Version

This gem is intend for write delayed job with easy and clean.

Philosophy

We hope to see delayed executed business logic clealy in where add it into, so, all we need to be done is just use a do ... end block to wrap delayed job code, it will work as expected.

Getting Started

Install via Rubygems

$ gem install delayed_worker

OR ...

Add to your Gemfile

gem 'delayed_worker'

Usage

run worker With ActiveRecord.

# == Schema Information

# Table name: test_delayed_workers

# some_column                   :string(255)
# delayed_worker_disabled       :boolean          default(TRUE)
# delayed_worker_scheduled_at   :datetime

class TestDelayedWorkerController < ActionController::Base
  def update_column
    record = TestDelayedWorker.find(params[:id])
    
    add_job_into_delayed_worker job_name: 'update some_column value' do
    # all code in block will be run asynchronous in delayed worker.
    # do heavy task here, e.g. invoke exteral API or do heavy SQL query
    # ...
    
    update(some_column: 'new_value') # can use any activerecord object method here to update record
    # ...
    # You can outout log to `log/delayed_worker.log` with following code:
    # DelayedWorker::Logger.logger.info 'logger something'
    end
  end
end

If you want to disabled scheduled job before executed, you can add a delayed_worker_disabled boolean column to table. if this column is true, scheduled job will just do noop.

If you want job is execute in some future date, you need add a delayed_worker_scheduled_at column into table, and pass in scheduled_at named parameter, with a integer(seconds after now) or any time like object which have a to_time method. (e.g. Date, Time, DateTime, ActiveSupport::TimeWithZone)

following is a example:

class TestDelayedWorkerController < ActionController::Base
  def update_column
  record = TestDelayedWorker.find(params[:id], scheduled_at: 3600)

    add_job_into_delayed_worker job_name: 'update some_column value' do
      update(some_column: 'new_value') # can use any activerecord object method here to update record
    end
  end
end

If you want to change scheduled date before job executed, just need change column delayed_worker_scheduled_at value, and run add_job_into_delayed_worker again to add a new job into queue, old job will just do noop, and new job will work.

run worker in controller action

class TestDelayedWorkerController < ActionController::Base
 def update_column
   id = params[:id]
   new_params = {some_column: params[:some_column]}
   
   # we must use `params: {key1: value1, key2...}` to pass local variable into block.
   add_job_into_delayed_worker job_name: 'update some column value use params in controller', subject_id: id, params: new_params do
     record = TestDelayedWorker.find(subject_id)
     record.update(some_column: params[:some_column]) # get passed in value with: params[:some_key] or params['some_key']
   end
 end
end

Run in a simple class

class SimpleDelayedWorker
  include DelayedWorker::Concern
  
  def some_method
    add_job_into_delayed_worker job_name: 'simple delayed worker', time: 10 do
      print 'run asynchronous after 10 seconds'
    end
  end
end

add_job_into_delayed_worker all supported options is list here

IMPORTANT some trap you must to know:

  1. Only support do ...end form block, and do must not same line as end, curly braces {} block is not supported!
  2. External variables defined in add_job_into_delayed_worker context must use params named parameter pass in.

Support

CRuby 2.2 2.3 2.4 2.5 is support.

Dependency

sidekiq method_source

History

See CHANGELOG for details.

Contributing

  • Bug reports
  • Source
  • Patches:
    • Fork on Github.
    • Run bundle install.
    • Create your feature branch: git checkout -b my-new-feature.
    • Commit your changes: git commit -am 'Add some feature'.
    • Push to the branch: git push origin my-new-feature.
    • Send a pull request :D.

license

Released under the MIT license, See LICENSE for details.