• Stars
    star
    313
  • Rank 133,714 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Turn any ruby code into a daemon.

Dante

Turn any ruby into a daemon.

Description

Dante is the simplest possible thing that will work to turn arbitrary ruby code into an executable that can be started via command line or start/stop a daemon, and will store a pid file for you.

If you need to create a ruby executable and you want standard daemon start/stop with pid files and no hassle, this gem will be a great way to get started.

Installation

Add to your Gemfile:

# Gemfile

gem "dante"

or to your gemspec:

# mygem.gemspec

Gem::Specification.new do |s|
  s.add_dependency "dante"
end

Usage

Dante is meant to be used from any "bin" executable. For instance, to create a binary for a web server, create a file in bin/myapp:

#!/usr/bin/env ruby

require File.expand_path("../../myapp.rb", __FILE__)

Dante.run('myapp') do |opts|
  # opts: host, pid_path, port, daemonize, user, group
  Thin::Server.start('0.0.0.0', opts[:port]) do
    use Rack::CommonLogger
    use Rack::ShowExceptions
    run MyApp
  end
end

Be sure to properly make your bin executable:

chmod +x bin/myapp

CLI

This gives your binary several useful things for free:

./bin/myapp

will start the app undaemonized in the terminal, handling trapping and stopping the process.

./bin/myapp -l /var/log/myapp.log

will start the app undaemonized in the terminal and redirect all stdout and stderr to the specified logfile.

./bin/myapp -p 8080 -d -P /var/run/myapp.pid -l /var/log/myapp.log

will daemonize and start the process, storing the pid in the specified pid file. All stdout and stderr will be redirected to the specified logfile. If no logfile is specified in daemon mode then all stdout and stderr will be directed to /var/log/.log.

./bin/myapp -k -P /var/run/myapp.pid

will stop all daemonized processes for the specified pid file.

./bin/myapp --help

Will return a useful help banner message explaining the simple usage.

Advanced

In many cases, you will need to add custom flags/options or a custom description to your executable. You can do this easily by using Dante::Runner more explicitly:

#!/usr/bin/env ruby

require File.expand_path("../../myapp.rb", __FILE__)

# Set default port to 8080
runner = Dante::Runner.new('myapp', :port => 8080)
# Sets the description in 'help'
runner.description = "This is myapp"
# Setup custom 'test' option flag
runner.with_options do |opts|
  opts.on("-t", "--test TEST", String, "Test this thing") do |test|
    options[:test] = test
  end
end
# Create validation hook for options
runner.verify_options_hook = lambda { |opts|
  raise Exception.new("Must supply test parameter") if opts[:test].nil?
}
# Parse command-line options and execute the process
runner.execute do |opts|
  # opts: host, pid_path, port, daemonize, user, group
  Thin::Server.start('0.0.0.0', opts[:port]) do
    puts opts[:test] # Referencing my custom option
    use Rack::CommonLogger
    use Rack::ShowExceptions
    run MyApp
  end
end

Now you would be able to do:

./bin/myapp -t custom

and the opts would contain the :test option for use in your script. In addition, help will now contain your customized description in the banner.

You can also use dante programmatically to start, stop and restart arbitrary code:

# daemon start
Dante::Runner.new('gitdocs').execute(:daemonize => true, :pid_path => @pid, :log_path => @log_path) { something! }
# daemon stop
Dante::Runner.new('gitdocs').execute(:kill => true, :pid_path => @pid)
# daemon restart
Dante::Runner.new('gitdocs').execute(:daemonize => true, :restart => true, :pid_path => @pid) { something! }

so you can use dante as part of a more complex CLI executable.

God

Dante can be used well in conjunction with the excellent God process manager. Simply, use Dante to daemonize a process and then you can easily use God to monitor:

# /etc/god/myapp.rb

God.watch do |w|
  w.name            = "myapp"
  w.interval        = 30.seconds
  w.start           = "ruby /path/to/myapp/bin/myapp -d"
  w.stop            = "ruby /path/to/myapp/bin/myapp -k"
  w.start_grace     = 15.seconds
  w.restart_grace   = 15.seconds
  w.pid_file        = "/var/run/myapp.pid"

  w.behavior(:clean_pid_file)

  w.start_if do |start|
    start.condition(:process_running) do |c|
      c.interval = 5.seconds
      c.running = false
    end
  end
end

and that's all. Of course now you can also easily daemonize as well as start/stop the process on the command line as well.

Copyright

Copyright Β© 2011 Nathan Esquenazi. See LICENSE for details.

More Repositories

1

rabl

General ruby templating with json, bson, xml, plist and msgpack support
Ruby
3,648
star
2

gitdocs

Open-source Dropbox using Ruby and Git
JavaScript
1,138
star
3

backburner

Simple and reliable beanstalkd job queue for ruby
Ruby
429
star
4

cap-recipes

Battle-tested capistrano recipes for ruby, rubygems, apache, passenger, delayed_job, juggernaut, thinking_sphinx, mongodb, whenever, among other popular tools
Ruby
359
star
5

sinatra_more

Generators, helpers and extensions enabling complex sinatra apps
Ruby
330
star
6

sheet_mapper

Map spreadsheet rows to a ruby objects
Ruby
120
star
7

active_form

Submission to github of validation plugin for non-activerecord models.
Ruby
25
star
8

conditions_fu

Enhances the conditions hash for activerecord finds
Ruby
18
star
9

backburner_mailer

Plugin for sending asynchronous email with Backburner
Ruby
15
star
10

semantic_form_builder

Semantically valid ActionView form builder
Ruby
14
star
11

padrino-pjax

Demo of padrino with pjax
JavaScript
14
star
12

simple_rails_template

My stab at a simple rails template
JavaScript
8
star
13

resume

A collection of my work history and past projects
8
star
14

mongo_blog

A blogging platform written using Padrino and mongoid
JavaScript
8
star
15

lowpro

Branch of Dan Webb's JavaScript DOM event library. Small JavaScript library built as an extension to Prototype that makes unobtrusive DOM scripting much easier
JavaScript
6
star
16

ideagrid

Ideagrid is a community of diverse individuals with the common goal of improving the environment through communication, planning, and cooperation.
Ruby
6
star
17

tokyo_cabinet_examples

A place where I keep very simple experiments with tokyo cabinet
Ruby
4
star
18

preload_associations

Preloads associations for ActiveRecord models after query is executed
Ruby
4
star
19

prototype_notifier

A simple alert alternative which supports growl-ish notifications
JavaScript
3
star
20

prototype_animator

An enhanced css animation library for prototype.
JavaScript
3
star
21

ajax_wizard_in_rails

A sample experiment in creating a slick wizard type form in a rails application
JavaScript
3
star
22

wikipedia_search

Final project for a college course: rails app with wikipedia search
Ruby
3
star
23

testing_extensions

My personal extensions for test::unit and shoulda for easier testing.
Ruby
3
star
24

taskr

A sample application for using jquery + lowpro and unobtrusive javascript
JavaScript
2
star
25

rubywarrior-ai

Nathan's Ruby Warrior AI
Ruby
2
star
26

loki_locator_samples

Sample javascript for working with loki location api
2
star
27

nesquena.github.com

My github homepage
2
star
28

rack_code_samples

Stores my experiments with building rack apps of all types
Ruby
2
star