📜 ⬆️ ⬇️

loop_dance - quick deployment background scheduler

Recently, in projects it is often necessary to create a demon that periodically checks or sends something to the background.

Usually, a similar problem is solved using packages such as whevenever, daemon_controller, daemon_generator, etc. and everything would seem simple and understandable, but every time I got tired of making a garden and writing the same thing. All I need is just that once a hour some User.notify_all is executed

I present loop_dance - gem for quick deployment of a controlled daemon in a rail environment.

Conditions


  1. Minimum excess code. Just what you need to run and how often.
  2. The code is executed in the rail environment.
  3. The daemon should automatically start / restart when the project is deployed.
  4. The ability to control the daemon through rake-tasks or directly from the application (it's nice to see its status on the admin panel)

Dancing


Insert the package into our Gemfile, memorable then update the bundle
')
gem "loop_dance" 


Create a lib / loop_dance.rb file with tasks and a specified periodicity.

For example: Every 3 hours notify all users. Every 60 seconds check reports.

  class Dancer1 < LoopDance::Dancer every 3.hours do User.notify_all end every 60.seconds do Report.checks end end 


That's all.

We created the first dancer, which will start automatically the next time the rail server is restarted, will hang in the system by an independent daemon and perform tasks with the specified frequency.

You can manage it manually:

 rake loop_dance:start_all rake loop_dance:stop_all rake loop_dance:status rake loop_dance:dancer1:start rake loop_dance:dancer1:stop rake loop_dance:dancer1:status 


Or from the application itself:

 Dancer1.start unless Dancer1.running? 


Project Address: github.com/dapi/loop_dance

Related Links:
ruby-toolbox.com/categories/daemon_management.html
ruby-toolbox.com/categories/daemonizing.html
ruby-toolbox.com/categories/scheduling.html

Source: https://habr.com/ru/post/111676/


All Articles