Giter VIP home page Giter VIP logo

looper's Introduction

Looper is a dead simple Ruby module for daemonizing your code, meant for use with Rails’ script/runner. No forking involved, no detaching, simply running a nice healthy loop, but allowing your code to bail out of it, and making it responsive to signals.

You implement a class like “DoSomething” that checks for new message objects and then does something to them. This class will include Looper in order to easily loop, respond to shutdown signals, and it can then be run via script/runner as a daemon.

The loop handles sleeping between runs, and will catch any unhandled exceptions that bubble up and keep on truckin’. Thus, if you want to exit on a particular exception, you’ve got to rescue it in your code and set @run to false.

Here’s an example usage:

require 'looper'

class DoSomething
  include Looper
  
  attr_accessor :run
  
  def initialize(config)
    @run = true
    # do config stuff, etc...
    @sleep = config[:sleep].nil? ? 60 : config[:sleep]
  end # initialize
  
  def run
    loopme(@sleep) do
      begin
        # this is where the meat of your code goes...
        messages = twitter.direct_messages({:since => since.strftime("%a, %d %b %Y %H:%M:%S %Z")})
      rescue Twitter::EpicFailure => e
        puts "bailing out, dude!"
        # set run to false to put the kabosh on the next run
        @run = false
      end
    end
  end
end

# and here's how we kick it off:
DoSomething.new( { :sleep => 10 } ).run

Now we can just kick that off any way we like and background the process, but we tend to use it with script/runner in our Rails environments to have access to our models and such. It boils down to:

$ nohup script/runner -e RAILS_ENV /path/to/DoSomething.rb

And then looking up the PID by matching on /path/to/DoSomething.rb via grep and use kill to send the term signal.

looper's People

Contributors

billymeltdown avatar diedthreetimes avatar

Watchers

Osborne Brook Partnership avatar James Cloos 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.