Commit 7c9b04da1ae118f62bc50ce7d9eafc23ed59e632

Authored by Victor Costa
1 parent fe641774

community-hub: improve listener

lib/community_hub_plugin/hub.rb
@@ -34,34 +34,7 @@ class CommunityHubPlugin::Hub < Folder @@ -34,34 +34,7 @@ class CommunityHubPlugin::Hub < Folder
34 true 34 true
35 end 35 end
36 36
37 - def twitter_service  
38 - Twurl::Stream.run(self, nil, hashtags_twitter, File.dirname(__FILE__) + '/../../tweeter_stream/config/twurlrc', proxy_url)  
39 - end  
40 -  
41 - def facebook_service  
42 - facebook_comments(self, nil, facebook_page_id, facebook_pooling_time, facebook_access_token, proxy_url)  
43 - end  
44 -  
45 - def self.start_listen  
46 - hubs = {}  
47 - loop do  
48 - puts "searching for hubs"  
49 - CommunityHubPlugin::Hub.all.each do |hub|  
50 - hub_conf = hubs[hub.id]  
51 - if hub_conf.nil? || hub_conf[:hub].updated_at < hub.updated_at  
52 - hub_conf[:threads].each {|t| t.terminate} if hub_conf  
53 - puts "hub #{hub.id} found!!!!!!"  
54 - threads = []  
55 - threads << Thread.new { hub.twitter_service } if hub.twitter_enabled  
56 - threads << Thread.new { hub.facebook_service } if hub.facebook_enabled  
57 - hubs[hub.id] = {:threads => threads, :hub => hub}  
58 - end  
59 - end  
60 - sleep(5)  
61 - end  
62 - end  
63 -  
64 def view_page 37 def view_page
65 "content_viewer/hub.rhtml" 38 "content_viewer/hub.rhtml"
66 end 39 end
67 -end  
68 \ No newline at end of file 40 \ No newline at end of file
  41 +end
lib/community_hub_plugin/listener.rb 0 → 100644
@@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
  1 +class CommunityHubPlugin::Listener
  2 +
  3 + class << self
  4 +
  5 + def twitter_service(hub)
  6 + Twurl::Stream.run(hub, nil, hub.hashtags_twitter, File.dirname(__FILE__) + '/../../tweeter_stream/config/twurlrc', hub.proxy_url)
  7 + end
  8 +
  9 + def facebook_service(hub)
  10 + facebook_comments(hub, nil, hub.facebook_page_id, hub.facebook_pooling_time, hub.facebook_access_token, hub.proxy_url)
  11 + end
  12 +
  13 + def run
  14 + hubs = {}
  15 + loop do
  16 + log "searching for hubs"
  17 + CommunityHubPlugin::Hub.all.each do |hub|
  18 + hub_conf = hubs[hub.id]
  19 + if hub_conf.nil? || hub_conf[:hub].updated_at < hub.updated_at
  20 + hub_conf[:threads].each {|t| t.terminate} if hub_conf
  21 + log "hub #{hub.id} found!!!!!!"
  22 + threads = []
  23 + threads << Thread.new { twitter_service(hub) } if hub.twitter_enabled
  24 + threads << Thread.new { facebook_service(hub) } if hub.facebook_enabled
  25 + hubs[hub.id] = {:threads => threads, :hub => hub}
  26 + end
  27 + end
  28 + sleep(10)
  29 + end
  30 + end
  31 +
  32 + def initialize_logger
  33 + logdir = File.join(RAILS_ROOT, 'log', CommunityHubPlugin::Listener.name.underscore)
  34 + File.makedirs(logdir) if !File.exist?(logdir)
  35 + logpath = File.join(logdir, "#{ENV['RAILS_ENV']}_#{Time.now.strftime('%F')}.log")
  36 + @logger = Logger.new(logpath)
  37 + end
  38 +
  39 + def log(message)
  40 + initialize_logger unless @initiated
  41 + @initiated ||= true
  42 + @logger << "[#{Time.now.strftime('%F %T %z')}] #{message}\n"
  43 + end
  44 +
  45 + end
  46 +
  47 +end
script/hub_updater
@@ -7,5 +7,5 @@ NOOSFERO_ROOT = File.expand_path(File.dirname(__FILE__) + &#39;/../../../&#39;) @@ -7,5 +7,5 @@ NOOSFERO_ROOT = File.expand_path(File.dirname(__FILE__) + &#39;/../../../&#39;)
7 7
8 Daemons.run_proc('hub') do 8 Daemons.run_proc('hub') do
9 require NOOSFERO_ROOT + '/config/environment' 9 require NOOSFERO_ROOT + '/config/environment'
10 - CommunityHubPlugin::Hub.start_listen 10 + CommunityHubPlugin::Listener.run
11 end 11 end