Class: Rust::Jobs::Job
Direct Known Subclasses
Instance Method Summary collapse
- #add_task(task = nil, **options, &block) ⇒ Object
-
#initialize(name, **options) ⇒ Job
constructor
A new instance of Job.
- #log(message, type = "INFO") ⇒ Object
- #log_error(message) ⇒ Object
- #log_info(message) ⇒ Object
- #log_warning(message) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(name, **options) ⇒ Job
Returns a new instance of Job.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rust/jobs/jobs.rb', line 64 def initialize(name, **) @name = name @tasks = [] @parallel = false if ['parallel'] @parallel = true @parallel_tasks = 10 end if ['parallel_tasks'].is_a?(Integer) @parallel_tasks = ['parallel_tasks'].to_i end @logger = ['logger'] ? ['logger'] : STDOUT if [:quiet] @logger = File.open(File::NULL, "w") end end |
Instance Method Details
#add_task(task = nil, **options, &block) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rust/jobs/jobs.rb', line 102 def add_task(task=nil, **, &block) if block_given? raise "You gave both a block and a task. Please, choose one" if task task = Task.new(['title'], block) end raise "Expected a task, #{task.class} given instead" unless task.is_a?(Task) @tasks << task end |
#log(message, type = "INFO") ⇒ Object
86 87 88 |
# File 'lib/rust/jobs/jobs.rb', line 86 def log(, type="INFO") @logger << "[#{type}] #{Time.now}: #{.gsub("\n", " -- ")}" end |
#log_error(message) ⇒ Object
98 99 100 |
# File 'lib/rust/jobs/jobs.rb', line 98 def log_error() log(, "ERROR") end |
#log_info(message) ⇒ Object
90 91 92 |
# File 'lib/rust/jobs/jobs.rb', line 90 def log_info() log(, "INFO") end |
#log_warning(message) ⇒ Object
94 95 96 |
# File 'lib/rust/jobs/jobs.rb', line 94 def log_warning() log(, "WARNING") end |
#start ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rust/jobs/jobs.rb', line 113 def start log_info "Job \"#@name\" started" if @parallel else @tasks.each do |t| t.on_complete do log_info "Task \"#{t.title}\" completed" end t.on_error do || log_error "Task \"#{t.title}\" did not complete: #{}" end log_info "Task \"#{t.title}\" started" t.start t.waitfor end end end |