lib/junkie/pyload/observer.rb in junkie-0.0.2 vs lib/junkie/pyload/observer.rb in junkie-0.0.3

- old
+ new

@@ -15,21 +15,27 @@ @config = Config.get_config(self) @api = Junkie::Pyload::Api.new @ready_for_new_links = true - @watchdog = nil + @watchdog_enabled = false @active_downloads = Hash.new + + @skipped_timer_at_first_complete_detection = false end # is a method that is called once from inside the reactor and allows us # to register custom actions into the reactor def setup in_fiber { @api.login cleanup } + + EM.add_periodic_timer(@config[:watchdog_refresh]) do + monitor_progress if @watchdog_enabled + end end # Adds new episode to Pyload which downloads the episode and extracts it # # @param [Junkie::Episode] episode which should be downloaded @@ -47,16 +53,11 @@ @active_downloads[pid] = episode log.info("Added #{episode} to Pyload, Pid=#{pid}") - # start watchdog timer if it does not already run - if @watchdog.nil? - @watchdog = EM::PeriodicTimer.new(@config[:watchdog_refresh]) do - monitor_progress - end - end + (@watchdog_enabled = true) unless @watchdog_enabled end # checks if the Observer is ready to add new episodes to Pyload # # @returns [Boolean] true if new links can be added @@ -76,11 +77,11 @@ catch(:break) { queue_data = @api.call(:getQueueData) if queue_data.empty? log.info("Empty Pyload queue, I cancel the watchdog timer") - unschedule_watchdog + @watchdog_enabled = false throw :break end # extract package IDs and map them to current downloads update_package_ids(queue_data) @@ -93,14 +94,23 @@ # look for complete downloads pids = get_complete_downloads(queue_data) if not pids.empty? - @api.call(:deletePackages, {pids: pids}) - log.info("Complete packages are removed from Pyload Queue") + if @skipped_timer_at_first_complete_detection + @api.call(:deletePackages, {pids: pids}) + log.info("Complete packages are removed from Pyload Queue") + @skipped_timer_at_first_complete_detection = false - @ready_for_new_links = true + @ready_for_new_links = true + else + # If a package is complete, we are sometimes so fast to remove + # it, before the extracting can be started, so we will skip it + # the first time + log.info("Complete package detected, skip the first time") + @skipped_timer_at_first_complete_detection = true + end end } } end @@ -168,16 +178,9 @@ next end raise InvalidStateError, "PackageID #{pid} can't be mapped to active Download" - end - end - - # cancels the watchdog timer - def unschedule_watchdog - if @watchdog_timer - @watchdog_timer.cancel end end def cleanup log.debug("Made a cleanup of Pyload's Queue")