Class: Junkie::Pyload::Observer

Inherits:
Object
  • Object
show all
Includes:
Config, Helper, Log
Defined in:
lib/junkie/pyload/observer.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
    watchdog_refresh: 10,          # interval the watchdog_timer is fired
}

Instance Method Summary collapse

Methods included from Log

#log

Methods included from Helper

#in_fiber

Methods included from Config

collect_default_configs, get_config, included

Constructor Details

#initialize(config = {}) ⇒ Observer

Returns a new instance of Observer.



14
15
16
17
18
19
20
21
22
# File 'lib/junkie/pyload/observer.rb', line 14

def initialize(config={})
  @config = Config.get_config(self)

  @api = Junkie::Pyload::Api.new

  @ready_for_new_links = true
  @watchdog = nil
  @active_downloads = Hash.new
end

Instance Method Details

#add_episode(episode) ⇒ Object

Note:

should only be called if ‘is_ready?` returns true

Adds new episode to Pyload which downloads the episode and extracts it

Parameters:

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/junkie/pyload/observer.rb', line 38

def add_episode(episode)
  raise InvalidStateError, "Observer is not ready" unless is_ready?
  @ready_for_new_links = false

  episode.status = :downloading

  package = "%s@%s" % [ episode.id, episode.series ]

  pid = @api.call(:addPackage, {name: package, links: [episode.link]})

  @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
end

#is_ready?Boolean

checks if the Observer is ready to add new episodes to Pyload

Returns:

  • (Boolean)


63
64
65
# File 'lib/junkie/pyload/observer.rb', line 63

def is_ready?
  @ready_for_new_links
end

#setupObject

is a method that is called once from inside the reactor and allows us to register custom actions into the reactor



26
27
28
29
30
31
# File 'lib/junkie/pyload/observer.rb', line 26

def setup
  in_fiber {
    @api.
    cleanup
  }
end