Giter VIP home page Giter VIP logo

daemon-spawn's Introduction

daemon-spawn

DESCRIPTION

Daemon launching and management made dead simple.

With daemon-spawn you can start, stop and restart processes that run in the background. Processed are tracked by a simple PID file written to disk.

In addition, you can choose to either execute ruby in your daemonized process or ‘exec’ another process altogether (handy for wrapping other services).

SYNOPSIS

WRITING A DAEMON

To create a new spawner, write a class that extends DaemonSpawn::Base and provides start and stop methods. For example:

class MyServer < DaemonSpawn::Base

  def start(args)
    # process command-line args
    # start your bad self
  end

  def stop
    # stop your bad self
  end
end

MyServer.spawn!(:log_file => '/var/log/echo_server.log',
                :pid_file => '/var/run/echo_server.pid',
                :sync_log => true,
                :working_dir => File.dirname(__FILE__))

If you need command-line parameters, any arguments passed after one of the commands (start, stop, status or restart) will be passed to the start method.

The spawn! method takes a hash of symbolized keys. At a minimum you must specify the :working_dir option. You can also override the default locations for the log and PID files.

If you pass a :processes option to the spawn!, daemon spawn will start that number of processes.

See the test/servers directory for working examples.

RUNNING A DAEMON

Let’s say that you have the example script listed above in bin/my_server. Here are the commands for starting, querying the status, restarting and stopping the daemon:

bin/my_server start
bin/my_server status
bin/my_server restart
bin/my_server stop

Note that if any additional arguments are passed to either start or restart those will be passed to the start method of an instance of your daemon class.

REQUIREMENTS

None!

CONTRIBUTIONS

Feel free to fork this project and send me pull requests with any changes that you have. Please note that I won’t accept any patches with significant formatting changes or ones without tests.

INSTALL

  • sudo gem install daemon-spawn

LICENSE

(The MIT License)

Copyright © 2009 Evri, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

daemon-spawn's People

Contributors

alexvollmer avatar chenyukang avatar emmanuel avatar seamusabshere avatar toaster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

daemon-spawn's Issues

minor issue spawning multiple daemons

First of all, nice gem Alex! I'm using it in production on http://feedmailpro.com and http://seoaholic.com

Anyway, I was using it with Resque and made some changes to accommodate multiple workers, like this:

num_workers = 2
num_workers.times do |i|
  ResqueWorker.spawn!(:log_file => File.join(RAILS_ROOT, "log", "resque_worker_#{i}.log"),
    :pid_file => File.join(RAILS_ROOT, 'tmp', 'pids', "resque_worker_#{i}.pid"),
    :sync_log => true,
    :working_dir => RAILS_ROOT)
end

The only problem was that the second one didn't run because in the spawn! method it called ARGV.shift. This alters ARGV so the second one that runs doesn't get the same arguments.

Maybe it would make more sense to use ARGV[0] there so that it doesn't get modified?

Anyway, it's not a huge deal. I was able to hack around it for now by doing this:

num_workers = 2

argv = ARGV.dup
num_workers.times do |i|
  ResqueWorker.spawn!(:log_file => File.join(RAILS_ROOT, "log", "resque_worker_#{i}.log"),
    :pid_file => File.join(RAILS_ROOT, 'tmp', 'pids', "resque_worker_#{i}.pid"),
    :sync_log => true,
    :working_dir => RAILS_ROOT)
  ARGV = argv
end

Option to force reopen of log file.

When running logrotate to rotate logs generated by daemon-spawn I need to restart the daemon in the postrotate script of logrotate. This works ok but it can take a long time since my daemons load the rails environment each time they start.

Is there a simple way (maybe a signal) that would tell daemon-spawn to reopen it's log file. This would be far more efficient that reloading the whole rails environment on each logrotate.

Worker pools for queues

Hi,
I was facing issue with daemon, switched to daemon-spawn.

Is there a provision we can create worker pools and assign them to queues? Similar to that is available in daemon.

DaemonSpawn 0.4.1 tries loading non-existant pid files.

Upon running my_daemon start for the first time, I get this error message:

/srv/backend/vendor/bundle/ruby/1.8/gems/daemon-spawn-0.4.1/lib/daemon_spawn.rb:145:in `basename': can't convert nil into String (TypeError)
    from /srv/backend/vendor/bundle/ruby/1.8/gems/daemon-spawn-0.4.1/lib/daemon_spawn.rb:145:in `find'
    from /srv/backend/vendor/bundle/ruby/1.8/gems/daemon-spawn-0.4.1/lib/daemon_spawn.rb:175:in `start'
    from /srv/backend/vendor/bundle/ruby/1.8/gems/daemon-spawn-0.4.1/lib/daemon_spawn.rb:164:in `send'
    from /srv/backend/vendor/bundle/ruby/1.8/gems/daemon-spawn-0.4.1/lib/daemon_spawn.rb:164:in `spawn!'

extension undefined

If I rely on the default pid_file option, I always get this error:
daemon_spawn.rb:92:in 'initialize': undefined local variable or method 'extension'

To stop the error from happening, I must specify a pid_file in the options.

Can't dup NilClass

Using bundler I get this:

Installing alexvollmer-daemon-spawn (0.2.0) /usr/local/lib/site_ruby/1.8/rubygems/specification.rb:442:in dup': can't dup NilClass (TypeError) from /usr/local/lib/site_ruby/1.8/rubygems/specification.rb:442:ininitialize_copy'
from /usr/local/lib/site_ruby/1.8/rubygems/specification.rb:439:in each' from /usr/local/lib/site_ruby/1.8/rubygems/specification.rb:439:ininitialize_copy'
from /usr/local/lib/site_ruby/1.8/rubygems/specification.rb:832:in dup' from /usr/local/lib/site_ruby/1.8/rubygems/specification.rb:832:into_ruby_for_cache'
from /usr/local/lib/site_ruby/1.8/rubygems/installer.rb:234:in write_spec' from /usr/local/lib/site_ruby/1.8/rubygems/installer.rb:175:ininstall'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/source.rb:101:in install' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/rubygems_integration.rb:78:inpreserve_paths'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/source.rb:91:in install' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/installer.rb:58:inrun'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/rubygems_integration.rb:93:in with_build_args' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/installer.rb:57:inrun'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/installer.rb:49:in run' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/installer.rb:8:ininstall'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/cli.rb:220:in install' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/vendor/thor/task.rb:22:insend'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/vendor/thor/task.rb:22:in run' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/vendor/thor/invocation.rb:118:ininvoke_task'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/vendor/thor.rb:263:in dispatch' from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/lib/bundler/vendor/thor/base.rb:386:instart'
from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.18/bin/bundle:13
from /usr/bin/bundle:19:in `load'
from /usr/bin/bundle:19

gem not released to rubygems

hey,

I think you may have released 0.3.0 to rubyforge but not rubygems (the new official source).

Best,
Seamus

.spawn! issue on windows

I'm having a few issues getting daemon-spawn to work with a Rails 3 app on Windows. I was trying to start background processes from an initializer - i.e. not from a script as shown in your example. To simplify, I've reverted to one of your examples - simple_server.rb.

First thing was the .spawn! command failed. I had to add a command (e.g. start) to get it to work.

result = SimpleServer.spawn!({:working_dir => File.join(File.dirname(__FILE__), '..', '..'), :log_file => 'simple_server.log', :pid_file => 'simple_server.pid', :sync_log => true, :processes => 2}, ['start'])

Next, when I inspect result I get [nil,nil]. What should be returned ? Does this mean the spawn failed - simple-server.pid doesn't exist ? In my command window I get "Error: Command not recognized Usage: rails COMMAND [ARGS]"

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.