From 6cc87c81047fbba7043175822bacfdc80c442ae1 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Wed, 16 Apr 2014 09:46:44 -0300 Subject: [PATCH] community-hub: improve listener --- plugins/community_hub/lib/community_hub_plugin/hub.rb | 29 +---------------------------- plugins/community_hub/lib/community_hub_plugin/listener.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ plugins/community_hub/script/hub_updater | 2 +- 3 files changed, 49 insertions(+), 29 deletions(-) create mode 100644 plugins/community_hub/lib/community_hub_plugin/listener.rb diff --git a/plugins/community_hub/lib/community_hub_plugin/hub.rb b/plugins/community_hub/lib/community_hub_plugin/hub.rb index 5d5c30d..361c53e 100644 --- a/plugins/community_hub/lib/community_hub_plugin/hub.rb +++ b/plugins/community_hub/lib/community_hub_plugin/hub.rb @@ -34,34 +34,7 @@ class CommunityHubPlugin::Hub < Folder true end - def twitter_service - Twurl::Stream.run(self, nil, hashtags_twitter, File.dirname(__FILE__) + '/../../tweeter_stream/config/twurlrc', proxy_url) - end - - def facebook_service - facebook_comments(self, nil, facebook_page_id, facebook_pooling_time, facebook_access_token, proxy_url) - end - - def self.start_listen - hubs = {} - loop do - puts "searching for hubs" - CommunityHubPlugin::Hub.all.each do |hub| - hub_conf = hubs[hub.id] - if hub_conf.nil? || hub_conf[:hub].updated_at < hub.updated_at - hub_conf[:threads].each {|t| t.terminate} if hub_conf - puts "hub #{hub.id} found!!!!!!" - threads = [] - threads << Thread.new { hub.twitter_service } if hub.twitter_enabled - threads << Thread.new { hub.facebook_service } if hub.facebook_enabled - hubs[hub.id] = {:threads => threads, :hub => hub} - end - end - sleep(5) - end - end - def view_page "content_viewer/hub.rhtml" end -end \ No newline at end of file +end diff --git a/plugins/community_hub/lib/community_hub_plugin/listener.rb b/plugins/community_hub/lib/community_hub_plugin/listener.rb new file mode 100644 index 0000000..5ae069f --- /dev/null +++ b/plugins/community_hub/lib/community_hub_plugin/listener.rb @@ -0,0 +1,47 @@ +class CommunityHubPlugin::Listener + + class << self + + def twitter_service(hub) + Twurl::Stream.run(hub, nil, hub.hashtags_twitter, File.dirname(__FILE__) + '/../../tweeter_stream/config/twurlrc', hub.proxy_url) + end + + def facebook_service(hub) + facebook_comments(hub, nil, hub.facebook_page_id, hub.facebook_pooling_time, hub.facebook_access_token, hub.proxy_url) + end + + def run + hubs = {} + loop do + log "searching for hubs" + CommunityHubPlugin::Hub.all.each do |hub| + hub_conf = hubs[hub.id] + if hub_conf.nil? || hub_conf[:hub].updated_at < hub.updated_at + hub_conf[:threads].each {|t| t.terminate} if hub_conf + log "hub #{hub.id} found!!!!!!" + threads = [] + threads << Thread.new { twitter_service(hub) } if hub.twitter_enabled + threads << Thread.new { facebook_service(hub) } if hub.facebook_enabled + hubs[hub.id] = {:threads => threads, :hub => hub} + end + end + sleep(10) + end + end + + def initialize_logger + logdir = File.join(RAILS_ROOT, 'log', CommunityHubPlugin::Listener.name.underscore) + File.makedirs(logdir) if !File.exist?(logdir) + logpath = File.join(logdir, "#{ENV['RAILS_ENV']}_#{Time.now.strftime('%F')}.log") + @logger = Logger.new(logpath) + end + + def log(message) + initialize_logger unless @initiated + @initiated ||= true + @logger << "[#{Time.now.strftime('%F %T %z')}] #{message}\n" + end + + end + +end diff --git a/plugins/community_hub/script/hub_updater b/plugins/community_hub/script/hub_updater index 712f806..8da9ce6 100755 --- a/plugins/community_hub/script/hub_updater +++ b/plugins/community_hub/script/hub_updater @@ -7,5 +7,5 @@ NOOSFERO_ROOT = File.expand_path(File.dirname(__FILE__) + '/../../../') Daemons.run_proc('hub') do require NOOSFERO_ROOT + '/config/environment' - CommunityHubPlugin::Hub.start_listen + CommunityHubPlugin::Listener.run end -- libgit2 0.21.2