Class: Picky::Scheduler

Inherits:
Object show all
Defined in:
lib/picky/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Scheduler

Returns a new instance of Scheduler.



7
8
9
10
11
12
# File 'lib/picky/scheduler.rb', line 7

def initialize options = {}
  @parallel = options[:parallel]
  @factor   = options[:factor] || 2

  configure
end

Instance Attribute Details

#parallelObject (readonly)

Returns the value of attribute parallel.



5
6
7
# File 'lib/picky/scheduler.rb', line 5

def parallel
  @parallel
end

Instance Method Details

#configureObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/picky/scheduler.rb', line 14

def configure
  if fork?
    def schedule &block
      scheduler.schedule &block
    end

    def finish
      scheduler.join
    end

    def scheduler
      @scheduler ||= create_scheduler
    end
    
    def create_scheduler
      Procrastinate::Scheduler.start Procrastinate::SpawnStrategy::Default.new(@factor)
    end
  else
    def schedule
      yield
    end

    def finish
      # Don't do anything.
    end
  end
end

#fork?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/picky/scheduler.rb', line 42

def fork?
  require 'procrastinate'
  parallel && Process.respond_to?(:fork)
rescue LoadError => e
  warn_procrastinate_missing
  return false
end

#warn_procrastinate_missingObject



49
50
51
52
# File 'lib/picky/scheduler.rb', line 49

def warn_procrastinate_missing
  warn_gem_missing 'Procrastinate', 'parallelized indexing (with the procrastinate gem)' unless @gem_missing_warned
  @gem_missing_warned = true
end