lib/junkie/reactor.rb in junkie-0.0.3 vs lib/junkie/reactor.rb in junkie-0.0.4

- old
+ new

@@ -16,21 +16,30 @@ DEFAULT_CONFIG = { :hd_enabled => false, :hoster_id => "ul", :series_index_file => File.join(Dir.home, '.sindex/seriesindex.xml'), - :episode_queue_timer_refresh => 5, # in seconds :episode_search_refresh => 15, # in minutes } def initialize @config = Config.get_config(self) - @pyload_observer = Junkie::Pyload::Observer.new() + log.info("Starting Junkie #{Junkie::VERSION}") - @episode_queue = EM::Queue.new + episode_channel = EM::Channel.new + notification_channel = EM::Channel.new + + @channels = { + episodes: episode_channel, + notifications: notification_channel + } + + @pyload_observer = Junkie::Pyload::Observer.new(@channels) + @twitter_notification = Junkie::Notification::Twitter.new(@channels) + @found_episodes = Hash.new build_procs # has to be called here end def build_procs @@ -50,41 +59,32 @@ sjunkieex.get_links_for_downloads.each do |episode| identifier = "%s@%s" % [episode.id, episode.series] if not @found_episodes.has_key? identifier log.info("Found new episode '#{episode}'") - @episode_queue.push(episode) + + @channels[:episodes].push(episode) @found_episodes[identifier] = episode end end } } - - # Proc that checks is Pyload-Observer is ready for new episodes and the - # episode_queue contains new episodes. - # - # @note Is called from within the reactor - @add_episodes_to_pyload = Proc.new do - if @pyload_observer.is_ready? - @episode_queue.pop do |episode| - log.info("Popped episode '#{episode}' from queue") - in_fiber { - @pyload_observer.add_episode(episode) - } - end - end - end end ########################################################################### #################### The Reactor ########################################## ########################################################################### def start - log.info("Starting Junkie #{Junkie::VERSION}") EM.run do + @channels[:episodes].subscribe do |episode| + next unless episode.status == :extracted + + @channels[:notifications].push(episode) + end + # do some initialization work @pyload_observer.setup # Look for new episodes and add them to the Queue if they haven't # been found yet @@ -94,16 +94,9 @@ EM.add_periodic_timer(@config[:episode_search_refresh] * 60) do @look_for_new_episodes.call end end - # Add found episodes into Pyload if there are any episodes and pyload - # is ready - EM.add_periodic_timer( - @config[:episode_queue_timer_refresh], @add_episodes_to_pyload) - - # for determining blocking operations - # EM.add_periodic_timer(1) { puts Time.now.to_i } end end end end