viking-biobot 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  .*swp
2
2
  old
3
3
  pkg
4
+ log
4
5
  test/db.sqlite3
5
6
  test.rb
6
7
  Session.vim
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/bin/biobot CHANGED
@@ -2,7 +2,6 @@
2
2
  require 'optparse'
3
3
  require 'yaml'
4
4
  require 'biobot'
5
- require 'daemonize'
6
5
 
7
6
  options = {}
8
7
  OptionParser.new do |opts|
@@ -25,11 +24,4 @@ raise Errno::ENOENT unless File.exist?(options[:config])
25
24
 
26
25
  biobot = Biobot::Base.new(YAML.load_file(options[:config]))
27
26
  biobot.start
28
- Signal.trap("TERM") do
29
- puts "Stopping biobot..."
30
- biobot.stop
31
- end
32
-
33
- Daemonize.daemonize if options[:daemon]
34
- loop do
35
- end
27
+ biobot.join
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{biobot}
5
- s.version = "0.0.3"
5
+ s.version = "0.0.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jeremy Stephens"]
9
- s.date = %q{2009-06-11}
9
+ s.date = %q{2009-06-12}
10
10
  s.default_executable = %q{biobot}
11
11
  s.description = %q{An XMPP bot for Vanderbilt Biostatistics}
12
12
  s.email = %q{viking415@gmail.com}
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'xmpp4r'
3
+ require 'logger'
3
4
  require 'active_record'
4
5
 
5
6
  require File.dirname(__FILE__) + '/biobot/base'
@@ -7,7 +7,11 @@ module Biobot
7
7
 
8
8
  @@periodicals = []
9
9
  def self.register_periodical(method, delay)
10
- @@periodicals << [method, delay]
10
+ @@periodicals << {
11
+ :method => method,
12
+ :delay => delay,
13
+ :last_run => nil
14
+ }
11
15
  end
12
16
 
13
17
  def initialize(config)
@@ -19,10 +23,12 @@ module Biobot
19
23
  ActiveRecord::Base.establish_connection(config['database'])
20
24
  end
21
25
 
26
+ @log = config['logfile'] ? Logger.new(config['logfile']) : nil
27
+
22
28
  @client = Jabber::Client.new(@jid)
23
29
  @presence = Jabber::Presence.new
24
30
 
25
- @threads = []
31
+ @message_buffer = []
26
32
  end
27
33
 
28
34
  def process(message)
@@ -42,19 +48,40 @@ module Biobot
42
48
  @client.send(@presence)
43
49
 
44
50
  @client.add_message_callback do |message|
45
- process(message) if message.body
51
+ @message_buffer << message if message.body
46
52
  end
53
+ main_loop
54
+ end
55
+
56
+ def main_loop
57
+ @main = Thread.new do
58
+ loop do
59
+ while !@message_buffer.empty?
60
+ process(@message_buffer.shift)
61
+ end
62
+ @@periodicals.size.times do |i|
63
+ last_run = @@periodicals[i][:last_run]
64
+ if last_run.nil? || (Time.now - last_run) >= @@periodicals[i][:delay]
65
+ @log.info "Running #{@@periodicals[i][:method]}" if @log
47
66
 
48
- @@periodicals.each do |(method, delay)|
49
- thread = Thread.new { loop { self.send(method); sleep(delay) } }
50
- @threads << thread
67
+ self.send(@@periodicals[i][:method])
68
+ @@periodicals[i][:last_run] = Time.now
69
+
70
+ @log.info "Done running #{@@periodicals[i][:method]}" if @log
71
+ end
72
+ end
73
+ sleep 1
74
+ end
51
75
  end
52
76
  end
53
77
 
78
+ def join
79
+ @main.join
80
+ end
81
+
54
82
  def stop
55
- @threads.each { |t| t.exit }
56
- @threads.clear
57
83
  @client.close!
84
+ @main.kill
58
85
  end
59
86
  end
60
87
  end
@@ -77,9 +77,10 @@ class TestBase < Test::Unit::TestCase
77
77
 
78
78
  def test_timers_for_periodicals_on_start
79
79
  clear_periodicals
80
- Biobot::Base.register_periodical(:leetsaurus, 0.3)
81
- Thread.expects(:new).once
82
- Biobot::Base.new(@config).start
80
+ Biobot::Base.register_periodical(:leetsaurus, 10)
81
+ biobot = Biobot::Base.new(@config)
82
+ biobot.expects(:leetsaurus)
83
+ biobot.start
83
84
  end
84
85
 
85
86
  def test_active_record_usage
@@ -95,6 +96,18 @@ class TestBase < Test::Unit::TestCase
95
96
  def test_stop
96
97
  @client.expects(:close!)
97
98
  biobot = Biobot::Base.new(@config)
99
+ biobot.start
98
100
  biobot.stop
99
101
  end
102
+
103
+ def test_logger
104
+ Logger.expects(:new).with('biobot.log')
105
+ @config['logfile'] = 'biobot.log'
106
+ biobot = Biobot::Base.new(@config)
107
+ end
108
+
109
+ def test_nil_logger
110
+ Logger.expects(:new).never
111
+ biobot = Biobot::Base.new(@config)
112
+ end
100
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viking-biobot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Stephens
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-11 00:00:00 -07:00
12
+ date: 2009-06-12 00:00:00 -07:00
13
13
  default_executable: biobot
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency