Skip to content

Commit

Permalink
added Hobson::Worker and removed Hobson.work! to organize code and su…
Browse files Browse the repository at this point in the history
…pport naming the hobson resque worker hobson instead of resque
  • Loading branch information
Jared Grippe authored and jaredatron committed Jan 20, 2012
1 parent 0f2dd2a commit 9c25507
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bin/hobson
Expand Up @@ -56,7 +56,7 @@ Class.new Thor do
method_option :daemonize, :type => :boolean, :aliases => %w{-d}, :banner => 'deamonize before becoming a resque worker'
method_option :pidfile, :type => :string, :aliases => %w{-p}, :banner => 'path to the pid file'
def work
Hobson.work! :daemonize => options.daemonize?, :pidfile => options.pidfile
Hobson::Worker.new :daemonize => options.daemonize?, :pidfile => options.pidfile
end

desc "web", "start hobson web server (see vegas options)"
Expand Down
41 changes: 10 additions & 31 deletions lib/hobson.rb
@@ -1,3 +1,4 @@
require "hobson/version"
require 'redis'
require 'redis/slave'

Expand All @@ -20,37 +21,15 @@ module Hobson

extend self

autoload :RedisSlave, 'hobson/redis_slave'
autoload :RedisHash, 'hobson/redis_hash'
autoload :Bundler, 'hobson/bundler'
autoload :Landmarks, 'hobson/landmarks'
autoload :Artifacts, 'hobson/artifacts'
autoload :Project, 'hobson/project'
autoload :Server, 'hobson/server'
autoload :CI, 'hobson/ci'

# become a resque-worker and handle hobson resque jobs
def work! options={}
options[:pidfile] ||= ENV['PIDFILE']

work = proc{
worker = resque::Worker.new('*')
worker.verbose = true
worker.very_verbose = $DEBUG
logger.info "started resque worker #{worker}"
File.open(options[:pidfile], 'w') { |f| f << worker.pid } if options[:pidfile]
worker.work
}

if options[:daemonize]
pid = fork{ work.call }
puts "Daemonized a resque worker with pid #{pid}"
Process.detach(pid)
else
puts "Becoming a resque worker"
work.call
end
end
autoload :RedisSlave, 'hobson/redis_slave'
autoload :RedisHash, 'hobson/redis_hash'
autoload :Bundler, 'hobson/bundler'
autoload :Landmarks, 'hobson/landmarks'
autoload :Artifacts, 'hobson/artifacts'
autoload :Project, 'hobson/project'
autoload :Worker, 'hobson/worker'
autoload :Server, 'hobson/server'
autoload :CI, 'hobson/ci'

def root
@root ||= Pathname.new ENV['HOBSON_ROOT'] ||= Dir.pwd
Expand Down
3 changes: 3 additions & 0 deletions lib/hobson/server/helpers.rb
Expand Up @@ -99,6 +99,9 @@ def job_timeline job
left = ((from - test_run.started_at) / test_run_duration) * 100
right = ((((to - test_run.started_at) / test_run_duration) * 100) - 100) * -1

left = 0 if left < 0
right = 100 if right > 100

html_options = {}
html_options[:title] = "#{landmark} for #{distance_of_time_in_words(duration.to_i)}"
html_options[:class] = "landmark-#{classname(landmark)}"
Expand Down
31 changes: 31 additions & 0 deletions lib/hobson/worker.rb
@@ -0,0 +1,31 @@
class Hobson::Worker < Resque::Worker

attr_accessor :options

def initialize options = {}
super('*')
@options = options
options[:pidfile] ||= ENV['PIDFILE']
@verbose = true
@very_verbose = $DEBUG
if options[:daemonize]
pid = ::Process.fork{ work }
Process.detach(pid)
puts "Daemonized a resque worker with pid #{pid}"
else
puts "Becoming a resque worker"
work
end
end

def work
File.open(options[:pidfile], 'w') { |f| f << $$ } if options[:pidfile]
super{ |job| Hobson.logger.info "job complete #{job}" }
end

def procline(string)
$0 = "hobson-#{Hobson::VERSION}: #{string}"
log! $0
end

end

0 comments on commit 9c25507

Please sign in to comment.