GithubHelp home page GithubHelp logo

jchinkle / daemon_objects Goto Github PK

View Code? Open in Web Editor NEW

This project forked from betterwithranch/daemon_objects

0.0 1.0 0.0 75 KB

An object-oriented approach to managing ruby daemons

License: MIT License

Ruby 100.00%

daemon_objects's Introduction

DaemonObjects

Build Status Code Climate Gem Version

Daemon Objects is designed to simplify using daemons in your ruby applications. Under the hood, it uses the daemons gem, which is a mature and tested solution. But, it adds support for managing via rake tasks, error handling and instrumentation.

The daemons gem also is intended to be used to daemonize a ruby script. DaemonObjects provides an object-oriented framework for developing daemons. This allows the application developer to focus on the specific behavior of the daemon instead of the infrastructure of daemon management.

Installation

Add this line to your application's Gemfile:

gem 'daemon_objects'

And then execute:

$ bundle

Or install it yourself as:

$ gem install daemon_objects

Usage

DaemonObjects will create daemons based on a simple convention. It will search a directory for files name *Daemon.rb. These typically will just inherit from the base Daemon class.

class MyDaemon < DaemonObjects::Base; end

This provides the basic daemon control methods (start, stop, run and restart) to your daemon.

To add behavior to your daemon, you will need a consumer. DaemonObjects will load the consumer using the name of the daemon and will search in the same directory for it. For example, if your daemon is name MyDaemon, the consumer should be named MyConsumer.

A consumer needs to inherit from the consumer base and implement run. For example,

class MyConsumer < DaemonObjects::ConsumerBase

  def run
    loop do
      "I'm looping"
      sleep 5
    end
  end

end

Environment

You can pass an environment argument to the consumer in two ways. If the project is using Rails, it will automatically use Rails.env. Otherwise, you can use the DAEMON_ENV environment variable.

Application directory

Application directory can be set a number of ways. If the project is using Rails, the application directory is Rails.root. If the task was started with Rake, it will be Rake.original_dir. You can also override this value by defining an app_directory method in the DaemonBase subclass.

class MyDaemon < DaemonObjects::Base
  def app_directory
    "/some/other/directory"
  end
end

Rake tasks

Once you have defined the daemon, you can control it with rake tasks. To access the rake tasks, you will need to include the daemon_objects railtie in config/application.rb.

require 'daemon_objects/railtie'

Rake tasks are created using the daemon file name. The rake syntax is:

rake daemon:<daemon_file_name>:<command>

For example, to start the MyDaemon daemon:

rake daemon:my_daemon:start

Four commands are supported

  • start - Starts the daemon
  • stop - Stops the daemon
  • restart - Stops and then starts the daemon
  • run - Runs the daemon synchronously

Amqp Support

DaemonObjects also has support for monitoring amqp queues. This is done with the bunny gem. To support this with your daemon, add consumes_amqp to your daemon class, one pair of daemon/consumer classes per queue you wish to support. Queues are constructed as 'durable' by default.

class MyQueueProcessingDaemon < DaemonObjects::Base
  consumes_amqp :endpoint     => "http://localhost:5672",
                :queue_name   => "my_awesome_queue"
end

This will add the code to monitor the queue, so all you need now is code to handle the messages.

class MyQueueProcessingConsumer < DaemonObjects::ConsumerBase

  handle_messages_with do |payload|
    puts payload
  end

end

Logging

DaemonObjects will create a new log file for your daemon using the pattern daemon_file_name_daemon.log. In a rails project, this will be created in the log directory of your application.

If the daemon does not have access to create the log, it will log errors to /tmp/daemon_name.output.

Configuration

Configuration is done by creating a config/daemons.yml file. Configuration is optional.

Example config:

development:
  my_test:
    count: 2

This will start 2 instances of the MyTestDaemon class.

Support for third-party libraries

DaemonObjects supports the following third-party libraries. If they are required in your application, your daemon will use them.

  • Airbrake - any errors that occur in the daemon will be reported to Airbrake.
  • NewRelic - amqp daemons will have instrument the handle_message method and report to New Relic.

Contributing

  1. Fork it
  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 new Pull Request

daemon_objects's People

Contributors

betterwithranch avatar cmaxw avatar meadoch1 avatar bglusman avatar

Watchers

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.