lib/junkie/pyload/observer.rb in junkie-0.0.9 vs lib/junkie/pyload/observer.rb in junkie-0.0.10
- old
+ new
@@ -41,10 +41,12 @@
# to register custom actions into the reactor
def setup
in_fiber {
@api.login
cleanup
+ stat_queued_episodes
+ stat_current_download
}
EM.add_periodic_timer(@config[:watchdog_refresh]) do
monitor_progress if @watchdog_enabled
@@ -63,10 +65,11 @@
return unless is_ready?
return if @found_episodes.empty?
@active_episode = @found_episodes.shift
episode = @active_episode
+ stat_queued_episodes
@ready_for_new_links = false
episode.status = :downloading
package = "%s@%s" % [ episode.id, episode.series ]
@@ -80,10 +83,31 @@
(@watchdog_enabled = true) unless @watchdog_enabled
end
+ # sends the current number of queued episodes out on the info channel
+ def stat_queued_episodes
+ @channels[:info].push({
+ key: "Pending episodes",
+ desc: "Number of episodes queued for download",
+ value: @found_episodes.size
+ })
+ end
+
+
+ # send the info about the current active download out on the
+ # info channel
+ def stat_current_download(complete=nil)
+ @channels[:info].push({
+ key: "Current active download",
+ desc: @active_episode ? @active_episode.to_s : "No active download",
+ value: complete ? "#{complete} %" : "-",
+ })
+ end
+
+
# checks if the Observer is ready to add new episodes to Pyload
#
# @returns [Boolean] true if new links can be added
def is_ready?
@ready_for_new_links
@@ -119,10 +143,12 @@
queue_data = get_queue_data
# check for empty queue and disable watchdog if needed
check_for_empty_queue(queue_data)
+ stat_current_download(get_complete_percentage(queue_data))
+
# extract package IDs and map them to current downloads
update_package_ids(queue_data)
check_for_failed_links(queue_data)
@@ -205,10 +231,11 @@
@active_episode.status = :extracted
log.info("Sending complete episode out on the channel")
@channels[:episodes].push(@active_episode)
@active_episode = nil
+ stat_current_download
end
# remove all complete packages
@api.call(:deletePackages, { pids: pids })
log.info("Complete packages are removed from Pyload Queue #{ pids }")
@@ -221,9 +248,29 @@
# 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
+
+
+ # get the percentage of completeness
+ #
+ # @param [Array] queue_data returned from :getQueueData api method
+ #
+ # @return [int] percentage of current download or nil
+ def get_complete_percentage(queue_data)
+ pids = []
+ queue_data.each do |package|
+ begin
+ sizetotal = package['sizetotal'].to_i
+ sizedone = package['sizedone'].to_i
+
+ return (sizedone * 100) / sizetotal
+ rescue Exception => e
+ end
+ end
+ nil
end
# Looks for complete downloads and returns their pids
#