Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from dsander/threaded-background-workers
Provide a new threaded background worker
- Loading branch information
Showing
8 changed files
with
281 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
# Procfile for development: | ||
# Procfile for development using the new threaded worker (scheduler, twitter stream and delayed job) | ||
web: bundle exec rails server | ||
schedule: bundle exec rails runner bin/schedule.rb | ||
twitter: bundle exec rails runner bin/twitter_stream.rb | ||
dj: bundle exec script/delayed_job run | ||
jobs: bundle exec rails runner bin/threaded.rb | ||
|
||
# Possible Profile configuration for production: | ||
# web: bundle exec unicorn -c config/unicorn/production.rb | ||
# schedule: bundle exec rails runner bin/schedule.rb | ||
# twitter: bundle exec rails runner bin/twitter_stream.rb | ||
# dj: bundle exec script/delayed_job run | ||
# jobs: bundle exec rails runner bin/threaded.rb | ||
|
||
# Old version with seperate processes (use this if you have issues with the threaded version) | ||
#web: bundle exec rails server | ||
#schedule: bundle exec rails runner bin/schedule.rb | ||
#twitter: bundle exec rails runner bin/twitter_stream.rb | ||
#dj: bundle exec script/delayed_job run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'thread' | ||
|
||
def stop | ||
puts 'Exiting...' | ||
@scheduler.stop | ||
@dj.stop | ||
@stream.stop | ||
end | ||
|
||
def safely(&block) | ||
begin | ||
yield block | ||
rescue StandardError => e | ||
STDERR.puts "\nException #{e.message}:\n#{e.backtrace.join("\n")}\n\n" | ||
STDERR.puts "Terminating myself ..." | ||
stop | ||
end | ||
end | ||
|
||
threads = [] | ||
threads << Thread.new do | ||
safely do | ||
@stream = TwitterStream.new | ||
@stream.run | ||
puts "Twitter stream stopped ..." | ||
end | ||
end | ||
|
||
threads << Thread.new do | ||
safely do | ||
@scheduler = HuginnScheduler.new | ||
@scheduler.run! | ||
puts "Scheduler stopped ..." | ||
end | ||
end | ||
|
||
threads << Thread.new do | ||
safely do | ||
require 'delayed/command' | ||
@dj = Delayed::Worker.new | ||
@dj.start | ||
puts "Delayed job stopped ..." | ||
end | ||
end | ||
|
||
# We need to wait a bit to let delayed_job set it's traps so we can override them | ||
sleep 0.5 | ||
|
||
trap('TERM') do | ||
stop | ||
end | ||
|
||
trap('INT') do | ||
stop | ||
end | ||
|
||
threads.collect { |t| t.join } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
deployment/site-cookbooks/huginn_production/files/default/Procfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
web: sudo bundle exec unicorn_rails -c config/unicorn.rb -E production | ||
schedule: sudo RAILS_ENV=production bundle exec rails runner bin/schedule.rb | ||
twitter: sudo RAILS_ENV=production bundle exec rails runner bin/twitter_stream.rb | ||
dj: sudo RAILS_ENV=production bundle exec script/delayed_job run | ||
jobs: sudo RAILS_ENV=production bundle exec rails runner bin/threaded.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require 'rufus/scheduler' | ||
|
||
class HuginnScheduler | ||
attr_accessor :mutex | ||
|
||
def initialize | ||
@rufus_scheduler = Rufus::Scheduler.new | ||
end | ||
|
||
def stop | ||
@rufus_scheduler.stop | ||
end | ||
|
||
def run_schedule(time) | ||
with_mutex do | ||
puts "Queuing schedule for #{time}" | ||
Agent.delay.run_schedule(time) | ||
end | ||
end | ||
|
||
def propagate! | ||
with_mutex do | ||
puts "Queuing event propagation" | ||
Agent.delay.receive! | ||
end | ||
end | ||
|
||
def cleanup_expired_events! | ||
with_mutex do | ||
puts "Running event cleanup" | ||
Event.delay.cleanup_expired! | ||
end | ||
end | ||
|
||
def with_mutex | ||
ActiveRecord::Base.connection_pool.with_connection do | ||
mutex.synchronize do | ||
yield | ||
end | ||
end | ||
end | ||
|
||
def run! | ||
self.mutex = Mutex.new | ||
|
||
tzinfo_friendly_timezone = ActiveSupport::TimeZone::MAPPING[ENV['TIMEZONE'].present? ? ENV['TIMEZONE'] : "Pacific Time (US & Canada)"] | ||
|
||
# Schedule event propagation. | ||
|
||
@rufus_scheduler.every '1m' do | ||
propagate! | ||
end | ||
|
||
# Schedule event cleanup. | ||
|
||
@rufus_scheduler.cron "0 0 * * * " + tzinfo_friendly_timezone do | ||
cleanup_expired_events! | ||
end | ||
|
||
# Schedule repeating events. | ||
|
||
%w[1m 2m 5m 10m 30m 1h 2h 5h 12h 1d 2d 7d].each do |schedule| | ||
@rufus_scheduler.every schedule do | ||
run_schedule "every_#{schedule}" | ||
end | ||
end | ||
|
||
# Schedule events for specific times. | ||
|
||
# Times are assumed to be in PST for now. Can store a user#timezone later. | ||
24.times do |hour| | ||
@rufus_scheduler.cron "0 #{hour} * * * " + tzinfo_friendly_timezone do | ||
if hour == 0 | ||
run_schedule "midnight" | ||
elsif hour < 12 | ||
run_schedule "#{hour}am" | ||
elsif hour == 12 | ||
run_schedule "noon" | ||
else | ||
run_schedule "#{hour - 12}pm" | ||
end | ||
end | ||
end | ||
|
||
@rufus_scheduler.join | ||
end | ||
end |
Oops, something went wrong.