Class: Junkie::Reactor

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

Constant Summary collapse

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
}

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

#initializeReactor

Returns a new instance of Reactor.



26
27
28
29
30
31
32
33
34
# File 'lib/junkie/reactor.rb', line 26

def initialize
  @config = Config.get_config(self)

  @pyload_observer = Junkie::Pyload::Observer.new()

  @episode_queue = EM::Queue.new
  @found_episodes = Hash.new
  build_procs # has to be called here
end

Instance Method Details

#build_procsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/junkie/reactor.rb', line 36

def build_procs

  # Proc that looks for new episodes on Seriesjunkies.org and adds them to
  # the episode_queue if they are new
  @look_for_new_episodes = Proc.new {
    in_fiber {

      log.debug "Reload Sindex from file"
      # TODO refactor this so that we can detect if we have to reload the
      # index (by MD5sum)
      sindex = Sindex::SeriesIndex.new(index_file: @config[:series_index_file])
      sjunkieex = Sjunkieex::Interface.new(sindex, @config)

      log.info "Looking for new episodes"
      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)
          @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

#startObject

The Reactor ##########################################



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/junkie/reactor.rb', line 81

def start
  log.info("Starting Junkie #{Junkie::VERSION}")

  EM.run do

    # do some initialization work
    @pyload_observer.setup

    # Look for new episodes and add them to the Queue if they haven't
    # been found yet
    EM.next_tick do
      @look_for_new_episodes.call

      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