tjplurker 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/tjplurker.rb +72 -10
  2. metadata +3 -3
data/lib/tjplurker.rb CHANGED
@@ -96,9 +96,9 @@ class TJPlurker
96
96
  $tjpLog.warn(self.class){"Request timeout, retry."}
97
97
  retry
98
98
  rescue => ex
99
- $tjpLog.fatal(self.class){"Unknown error, stop."}
100
- puts ex.backtrace
101
- exit
99
+ $tjpLog.fatal(self.class){"Unknown error, skip."}
100
+ puts ex.class, ex.message, ex.backtrace
101
+ return
102
102
  end
103
103
  #Update the offset
104
104
  @user_channel['comet_server'].sub!(/[\d]+$/, json["new_offset"].to_s)
@@ -142,9 +142,9 @@ class TJPlurker
142
142
  rescue JSON::ParserError => ex
143
143
  $tjpLog.error(self.class){"JSON parse error, request failed."}
144
144
  rescue => ex
145
- $tjpLog.fatal(self.class){"Unknown error, stop"}
145
+ $tjpLog.fatal(self.class){"Unknown error, skip."}
146
146
  puts ex.class, ex.message, ex.backtrace
147
- exit
147
+ return
148
148
  end
149
149
  return json
150
150
  end
@@ -214,7 +214,7 @@ class TJPlurker
214
214
  begin
215
215
  service.serve(@tjplurker, data)
216
216
  rescue => ex
217
- $tjpLog.error(self.class){"An error occured in #{service.class}, message:\n#{ex.message}"}
217
+ $tjpLog.error(self.class){"An error occured in #{service.class}, class: #{ex.class}, message: #{ex.message}, backtrace: #{ex.backtrace}"}
218
218
  end
219
219
  }
220
220
  }
@@ -237,19 +237,81 @@ class TJPlurker
237
237
 
238
238
  class Service
239
239
  attr_reader :name
240
- def initialize name=self.class.to_s,&proc
240
+ def initialize name=self.class.to_s, &proc
241
241
  @name = name
242
242
  if block_given?
243
- @serve = proc
243
+ @serve_proc = proc
244
+ end
245
+ end
246
+
247
+ def decide tjplurker, data
248
+ true
249
+ end
250
+
251
+ def action tjplurker, data
252
+ p data
253
+ end
254
+
255
+ def serve tjplurker, data
256
+ if @serve_proc
257
+ @serve_proc[tjplurker, data]
244
258
  else
245
- @serve = Proc.new{|tjplurker, data| p data}
259
+ action(tjplurker, data) if decide(tjplurker, data)
246
260
  end
247
261
  end
262
+ end
263
+
264
+ class ThreadService < Service
265
+ def initialize name=self.class.to_s, lasting=60, refresh=1, &proc
266
+ super name
267
+ @lasting = lasting
268
+ @refresh = refresh
269
+ @plurk_id_hash = Hash.new
270
+ @mutex = Mutex.new
271
+ end
248
272
 
273
+ def decide tjplurker, data
274
+ true
275
+ end
276
+
277
+ def action tjplurker, data
278
+ @serve_proc[tjplurker, data]
279
+ end
280
+
281
+ def final tjplurker, data
282
+ end
249
283
  def serve tjplurker, data
250
- @serve[tjplurker, data]
284
+ if data.type=='new_plurk' && decide(tjplurker, data)
285
+ @plurk_id_hash[data.plurk_id] = Thread.new(data.plurk_id){|plurk_id|
286
+ $tjpLog.debug(self.class){"Create thread in plurk ##{plurk_id}"}
287
+ Thread.current[:last_time] = Time.now.to_i
288
+ loop{
289
+ sleep(@refresh)
290
+ $tjpLog.debug(self.class){"Plurk ##{plurk_id} remain #{Thread.current[:last_time]+@lasting-Time.now.to_i} seconds."}
291
+ @mutex.lock
292
+ if Time.now.to_i - Thread.current[:last_time] > @lasting
293
+ @mutex.unlock
294
+ break
295
+ end
296
+ @mutex.unlock
297
+ }
298
+ $tjpLog.debug(self.class){"Terminate thread in plurk ##{plurk_id}"}
299
+ @mutex.lock
300
+ @plurk_id_hash.delete(plurk_id)
301
+ @mutex.unlock
302
+ final(tjplurker, data)
303
+ }
304
+ else
305
+ @mutex.lock
306
+ if data.type=='new_response' && @plurk_id_hash.has_key?(data.plurk_id)
307
+ @plurk_id_hash[data.plurk_id][:last_time] = Time.now.to_i
308
+ action(tjplurker, data)
309
+ end
310
+ @mutex.unlock
311
+ end
251
312
  end
252
313
  end
314
+
253
315
  end
254
316
 
255
317
  if __FILE__ == $0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tjplurker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
17
- requirement: &18733956 !ruby/object:Gem::Requirement
17
+ requirement: &19617900 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 1.4.2
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *18733956
25
+ version_requirements: *19617900
26
26
  description: A safe Plurk API wrapper based on Ruby and mainly designed for easily
27
27
  making plurk robot.
28
28
  email: tonytonyjan.cs97@g2.nctu.edu.tw