Giter Site home page Giter Site logo

delayed_worker's Introduction

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.

delayed_worker's People

Contributors

zw963 avatar

Watchers

 avatar  avatar

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.