-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
threaded.rb
57 lines (48 loc) · 924 Bytes
/
threaded.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 }