Class: Rust::Jobs::Job

Inherits:
Object show all
Defined in:
lib/rust/jobs/jobs.rb

Direct Known Subclasses

Resumeable

Instance Method Summary collapse

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, **options)
    @name = name
    @tasks = []

    @parallel = false

    if options['parallel']
        @parallel = true
        @parallel_tasks = 10
    end

    if options['parallel_tasks'].is_a?(Integer)
        @parallel_tasks = options['parallel_tasks'].to_i
    end

    @logger = options['logger'] ? options['logger'] : STDOUT

    if options[: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, **options, &block)
    if block_given?
        raise "You gave both a block and a task. Please, choose one" if task
        task = Task.new(options['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(message, type="INFO")
    @logger << "[#{type}] #{Time.now}: #{message.gsub("\n", " -- ")}"
end

#log_error(message) ⇒ Object



98
99
100
# File 'lib/rust/jobs/jobs.rb', line 98

def log_error(message)
    log(message, "ERROR")
end

#log_info(message) ⇒ Object



90
91
92
# File 'lib/rust/jobs/jobs.rb', line 90

def log_info(message)
    log(message, "INFO")
end

#log_warning(message) ⇒ Object



94
95
96
# File 'lib/rust/jobs/jobs.rb', line 94

def log_warning(message)
    log(message, "WARNING")
end

#startObject



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 |message|
                log_error "Task \"#{t.title}\" did not complete: #{message}"
            end

            log_info "Task \"#{t.title}\" started"
            t.start
            t.waitfor
        end
    end
end