Skip to content

Instantly share code, notes, and snippets.

require 'daemons'
module Daemons
class Monitor
def stop
pid = @pid.pid
begin; Process.kill('TERM', pid); rescue ::Exception; end
sleep(0.5)
begin; Process.kill('KILL', pid); rescue ::Exception; end
@slayer
slayer / gist:179611
Created September 2, 2009 08:35 — forked from morhekil/gist:179070
#!/usr/bin/env ruby
require File.dirname(__FILE__) + "/../../config/environment"
$running = true
$working = false
Signal.trap("TERM") do
$stderr.puts "#{Time.now.to_s(:db)}\tGot TERM, exiting..."
$running = false
def destroy
if params[:ids]
@models = Model.find(:all, :conditions => { :id => params[:ids] })
@models.each { |m| m.destroy }
else
@model = Tracker.find(params[:id])
@model.destroy
end
end
def em_start_websockets(options)
options.logger.info "Starting WebSockets daemon on #{options.websocket_host}:#{options.websocket_port} and '#{ENV['RAILS_ENV']}' environment"
@connections = 0
EventMachine::WebSocket.start(:host => options.websocket_host, :port => options.websocket_port, :debug => false) do |ws|
@user = nil
ws.onopen do
@connections = @connections + 1
puts "WebSocket connection open: #{@connections}"
def em_start_websockets(options)
options.logger.info "Starting WebSockets daemon on #{options.websocket_host}:#{options.websocket_port} and '#{ENV['RAILS_ENV']}' environment"
connections = 0
EventMachine::WebSocket.start(:host => options.websocket_host, :port => options.websocket_port, :debug => false) do |ws|
mq = MQ.new
user = nil
token = nil
queue = nil
var sys = require('sys'),
http = require('http');
//var process = require('process');
var amqp = require('./node-amqp/amqp');
var connection = amqp.createConnection({ host: 'localhost' });
var env = process.env.RAILS_ENV || 'development';
sys.puts('Environment: ' + env);
connection.addListener('ready', function () {
@slayer
slayer / .irbrc.rb
Created April 14, 2010 09:38 — forked from dekart/.irbrc.rb
#!/usr/bin/env ruby
require 'rubygems'
#require 'map_by_method' rescue nil
require 'pp'
# Enable tab-completion.
require 'irb/completion' rescue nil
# Enable prompt-less prompts
@slayer
slayer / delayed_job
Created December 17, 2010 02:20 — forked from akmathur/delayed_job
#!/usr/bin/env ruby
# -*- ruby -*-
require 'rubygems'
require 'daemon-spawn'
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
class DelayedJobWorker < DaemonSpawn::Base
def start(args)
ENV['RAILS_ENV'] ||= args.first || 'development'
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
# The problem: In your Rails 3 project, you have a model Request that models a
# user's request for content related to a specific problem or topic. The user
# can tag her request with any number of words. These are modeled by a Tag class
# backed by a tags DB table. Since you want unique records per tag, you have a
# Tagging class backed by a taggings table. Taggings is a a many-to-many table
# with some additional information. Also, other models in the application
# besides requests can be tagged: The Tagging class defines a polymorphic
# relationship "taggable" with those models.
#
# So pretty soon you want to look up all the requests that are tagged with