rumpy 0.9.12 → 0.9.14

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.
Files changed (3) hide show
  1. data/README.rdoc +1 -0
  2. data/lib/rumpy.rb +30 -18
  3. metadata +4 -4
data/README.rdoc CHANGED
@@ -30,6 +30,7 @@ Our goal is 'DO NOT REINVENT THE WHEEL'.
30
30
  @pid_file :: Optional variable, sets location of the file to which pid of detached process will be saved. If not set, it equals NameOfYourBotClass.downcase + '.pid'.
31
31
  @log_file :: Optional variable, sets location of the file for errors. Default is STDERR.
32
32
  @log_level :: Optional variable, sets the verbosity of log. Possible values are Logger::DEBUG < Logger::INFO < Logger::WARN < Logger::ERROR < Logger::FATAL < Logger::UNKNOWN. Default is Logger::INFO
33
+ @logger :: If you wanna specify more parameters to logger, you always can create logger yourself. If this variable set, @log_file & @log_level are ignored.
33
34
  @bot_name :: Optional name of the bot. Default is name of bot's class.
34
35
  @bot_version :: Optional version of the bot. Default is 1.0.0.
35
36
  * Write 3 methods:
data/lib/rumpy.rb CHANGED
@@ -3,11 +3,12 @@ require 'xmpp4r/client'
3
3
  require 'xmpp4r/roster'
4
4
  require 'xmpp4r/version'
5
5
  require 'active_record'
6
- require 'active_record/validations'
7
6
  require 'logger'
8
7
 
9
8
  module Rumpy
10
9
 
10
+ # Create new instance of `botclass`, start it in new process,
11
+ # detach this process and save the pid of process in pid_file
11
12
  def self.start(botclass)
12
13
  bot = botclass.new
13
14
  pf = pid_file bot
@@ -22,6 +23,8 @@ module Rumpy
22
23
  true
23
24
  end
24
25
 
26
+ # Determine the name of pid_file, read pid from this file
27
+ # and try to kill process with this pid
25
28
  def self.stop(botclass)
26
29
  pf = pid_file botclass.new
27
30
  return false unless File.exist? pf
@@ -35,25 +38,33 @@ module Rumpy
35
38
  true
36
39
  end
37
40
 
41
+ # Create new instance of `botclass` and start it without detaching
38
42
  def self.run(botclass)
39
43
  botclass.new.start
40
44
  end
41
45
 
46
+ # Determine the name of file where thid pid will stored to
42
47
  def self.pid_file(bot)
43
48
  pid_file = bot.pid_file
44
49
  pid_file = bot.class.to_s.downcase + '.pid' if pid_file.nil?
45
50
  pid_file
46
51
  end
47
52
 
53
+ # include this module into your bot's class
48
54
  module Bot
49
55
  attr_reader :pid_file
50
56
 
57
+ # one and only public function, defined in this module
58
+ # simply initializes bot's variables, connection, etc.
59
+ # and starts bot
51
60
  def start
52
- @log_file ||= STDERR
53
- @log_level ||= Logger::INFO
54
- @logger = Logger.new @log_file
55
- @logger.level = @log_level
56
- @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
61
+ if @logger.nil? then # if user haven't created his own logger
62
+ @log_file ||= STDERR
63
+ @log_level ||= Logger::INFO
64
+ @logger = Logger.new @log_file
65
+ @logger.level = @log_level
66
+ @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
67
+ end
57
68
  Signal.trap :TERM do |signo|
58
69
  @logger.info 'terminating'
59
70
  @logger.close
@@ -67,12 +78,22 @@ module Rumpy
67
78
  connect
68
79
  @logger.debug 'clear wrong users'
69
80
  clear_users
81
+
70
82
  set_subscription_callback
71
83
  set_message_callback
72
84
  set_iq_callback
85
+
86
+ start_backend_thread
87
+
73
88
  @logger.info 'Bot is going ONLINE'
74
89
  @client.send Jabber::Presence.new.set_priority(@priority).set_status(@status)
75
90
 
91
+ Thread.stop
92
+ end
93
+
94
+ private
95
+
96
+ def start_backend_thread
76
97
  Thread.new do
77
98
  begin
78
99
  loop do
@@ -90,11 +111,8 @@ module Rumpy
90
111
  $logger.error e.backtrace
91
112
  end
92
113
  end if self.respond_to? :backend_func
93
- Thread.stop
94
114
  end
95
115
 
96
- private
97
-
98
116
  def init
99
117
 
100
118
  xmppconfig = YAML::load_file @config_path + '/xmpp.yml'
@@ -127,10 +145,6 @@ module Rumpy
127
145
  def @main_model.find_by_jid(jid)
128
146
  super jid.strip.to_s
129
147
  end
130
-
131
- @mutexes = Hash.new do |h, k|
132
- h[k] = Mutex.new
133
- end
134
148
  end
135
149
 
136
150
  def connect
@@ -144,7 +158,7 @@ module Rumpy
144
158
  def clear_users
145
159
  @main_model.find_each do |user|
146
160
  items = @roster.find user.jid
147
- if items.count != 1 then
161
+ if items.count.zero? then
148
162
  @logger.info "deleting from database user with jid #{user.jid}"
149
163
  user.destroy
150
164
  end
@@ -160,6 +174,7 @@ module Rumpy
160
174
  user.destroy
161
175
  end
162
176
  end
177
+ @main_model.connection_pool.release_connection
163
178
  end
164
179
 
165
180
  def set_subscription_callback
@@ -207,10 +222,7 @@ module Rumpy
207
222
  pars_results = parser_func msg.body
208
223
  @logger.debug "parsed message: #{pars_results.inspect}"
209
224
 
210
- message = ""
211
- @mutexes[user.jid].synchronize do
212
- message = do_func user, pars_results
213
- end
225
+ message = do_func user, pars_results
214
226
  send_msg msg.from, message
215
227
  else
216
228
  @logger.debug "uknown user #{msg.from}"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rumpy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 35
4
+ hash: 39
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 12
10
- version: 0.9.12
9
+ - 14
10
+ version: 0.9.14
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tsokurov A.G.
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-08-10 00:00:00 +03:00
19
+ date: 2011-08-14 00:00:00 +03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency