Class: Blender::Scheduler

Inherits:
Object
  • Object
show all
Includes:
Lock, SchedulerDSL
Defined in:
lib/blender/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SchedulerDSL

#add_handler, #append_task, #ask, #build_task, #concurrency, #config, #driver, #ignore_failure, #lock, #lock_options, #log_level, #members, #on, #ruby_task, #scp_download, #scp_upload, #shell_task, #ssh_task, #strategy

Methods included from Discovery

#build_discovery, #old_search, #search, #search_with_config

Methods included from Utils::Refinements

#camelcase, #symbolize

Constructor Details

#initialize(name, tasks = [], options = {}) ⇒ Scheduler

Returns a new instance of Scheduler.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/blender/scheduler.rb', line 41

def initialize(name, tasks = [], options = {})
  @name = name
  @tasks = tasks
  @events = Blender::EventDispatcher.new
  unless options.delete(:no_doc)
    events.register(Blender::Handlers::Doc.new)
  end
  @metadata = .merge(options)
  @scheduling_strategy = nil
  @lock_properties = {driver: nil, driver_options: {}}
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



38
39
40
# File 'lib/blender/scheduler.rb', line 38

def events
  @events
end

#lock_propertiesObject (readonly)

Returns the value of attribute lock_properties.



39
40
41
# File 'lib/blender/scheduler.rb', line 39

def lock_properties
  @lock_properties
end

#metadataObject (readonly)

Returns the value of attribute metadata.



36
37
38
# File 'lib/blender/scheduler.rb', line 36

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/blender/scheduler.rb', line 36

def name
  @name
end

#scheduling_strategyObject (readonly)

Returns the value of attribute scheduling_strategy.



37
38
39
# File 'lib/blender/scheduler.rb', line 37

def scheduling_strategy
  @scheduling_strategy
end

#tasksObject (readonly)

Returns the value of attribute tasks.



38
39
40
# File 'lib/blender/scheduler.rb', line 38

def tasks
  @tasks
end

Instance Method Details

#concurrent_run(jobs) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/blender/scheduler.rb', line 80

def concurrent_run(jobs)
  c = [:concurrency]
  Log.debug("Invoking concurrent run with concurrency:#{c}")
  pool = Utils::ThreadPool.new(c)
  jobs.each do |job|
    pool.add_job do
      run_job(job)
    end
  end
  pool.run_till_done
end

#default_metadataObject



106
107
108
109
110
111
112
113
# File 'lib/blender/scheduler.rb', line 106

def 
  {
   ignore_failure: false,
   concurrency: 0,
   handlers: [],
   members: []
  }
end

#runObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/blender/scheduler.rb', line 53

def run
  @scheduling_strategy ||= SchedulingStrategy::Default.new
  events.run_started(self)
  events.job_computation_started(scheduling_strategy)
  jobs = scheduling_strategy.compute_jobs(@tasks)
  events.job_computation_finished(self, jobs)
  lock do
    if [:concurrency] > 1
      concurrent_run(jobs)
    else
      serial_run(jobs)
    end
    events.run_finished(self)
    jobs
  end
rescue StandardError => e
  events.run_failed(self, e)
  raise e
end

#run_job(job) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/blender/scheduler.rb', line 92

def run_job(job)
  events.job_started(job)
  Log.debug("Running job #{job.name}")
  job.run
  events.job_finished(job)
rescue StandardError => e
  events.job_failed(job, e)
  if [:ignore_failure]
    Log.warn("Exception: #{e.inspect} was suppressed, ignoring failure")
  else
    raise e
  end
end

#serial_run(jobs) ⇒ Object



73
74
75
76
77
78
# File 'lib/blender/scheduler.rb', line 73

def serial_run(jobs)
  Log.debug('Invoking serial run')
  jobs.each do |job|
    run_job(job)
  end
end