Module: Bj::Runner::Instance_Methods

Defined in:
lib/bj/runner.rb

Instance Method Summary collapse

Instance Method Details

#fill_morgue(options = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/bj/runner.rb', line 188

def fill_morgue options = {}
  Bj.logger.debug{ "filling the morgue..." }
  Bj.transaction(options) do
    now = Time.now
    jobs = Bj::Table::Job.find :all,
                               :conditions => ["state = 'running' and runner = ?", Bj.hostname]
    jobs.each do |job|
      Bj.logger.info{ "marking #{ job.id } (#{ job.command }) as dead." }
      job.state = 'dead'
      job.finished_at = now
      job.save!
    end
  end
end

#initialize(options = {}, &block) ⇒ Object



96
97
98
99
# File 'lib/bj/runner.rb', line 96

def initialize options = {}, &block
  options.to_options!
  @options, @block = options, block
end

#install_signal_handlersObject



235
236
237
238
239
240
241
242
243
244
# File 'lib/bj/runner.rb', line 235

def install_signal_handlers
  trap('SIGHUP') do
    if sleeping?
      Bj.logger.debug{ "woke up!" }
      throw :wake_up
    else
      sighup true
    end
  end
end

#new_lifeline(ppid) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/bj/runner.rb', line 219

def new_lifeline ppid
  Thread.new do
    loop do
      begin
        Process.kill 0, ppid
      rescue Errno::ESRCH
        STDERR.puts "parent #{ ppid } died"
        Kernel.exit 42
      end
      sleep 42 
    end
  end
end

#register(options = {}) ⇒ Object



266
267
268
269
270
271
272
273
274
275
# File 'lib/bj/runner.rb', line 266

def register options = {}
  Bj.transaction(options) do
    pid = Bj.config[Runner.key]
    if Util.alive?(pid)
      return false
    end
    Bj.config[Runner.key] = Process.pid
    at_exit{ unregister }
  end
end

#runObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/bj/runner.rb', line 101

def run
  wait = options[:wait] || 42
  limit = options[:limit]
  forever = options[:forever]
  ppid = options[:ppid]

  limit = false if forever
  wait = Integer wait
  loopno = 0
  thread = new_lifeline(ppid) if ppid

  Bj.chroot do
    register or exit!(EXIT::WARNING)
    Bj.logger.info{ "START" }
    fill_morgue
    install_signal_handlers

    loop do
      loopno += 1
      break if(limit and n > limit)

      Bj.logger.debug{ "loopno #{ loopno }" }

      sweep

      catch :no_jobs do
        loop do
          job = thread = stdout = stderr = nil

          Bj.transaction(options) do
            job = Bj::Table::Job.find :first,
                                      :conditions => "state = 'pending'",
                                      :order => "priority DESC, submitted_at ASC", 
                                      :limit => 1
            throw :no_jobs unless job

            Bj.logger.info{ "running #{ job.id } (#{ job.command })" }

            command = job.command
            env = job.env || {}
            stdin = job.stdin || ''
            stdout = job.stdout || ''
            stderr = job.stderr || ''
            started_at = Time.now

            q = Queue.new
            thread = Thread.new do
              Thread.current.abort_on_exception = true
              systemu command, :cwd=>Bj.rails_root, :env=>env, :stdin=>stdin, :stdout=>stdout, :stderr=>stderr do |pid|
                q << pid
              end
            end
            pid = q.pop

            job.state = "running"
            job.runner = Bj.hostname
            job.pid = pid
            job.started_at = started_at 
            job.save!
            job.update
          end

          exit_status = thread.value
          finished_at = Time.now

          Bj.transaction(options) do
            job = Bj::Table::Job.find job.id 
            break unless job
            job.state = "finished"
            job.finished_at = finished_at 
            job.stdout = stdout
            job.stderr = stderr
            job.exit_status = exit_status
            job.save!
            job.update
            Bj.logger.info{ "exit_status #{ job.exit_status }" }
          end
        end
      end

      sleep wait

      break unless(limit or limit == false)
    end
  end
end

#sleep(seconds) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/bj/runner.rb', line 246

def sleep seconds
  if sighup
    sighup false
    return
  end
  Bj.logger.debug{ "sleeping #{ seconds }..." }
  catch :wake_up do
    begin
      @sleeping = true
      Kernel.sleep seconds
    ensure
      @sleeping = false
    end
  end
end

#sleeping?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/bj/runner.rb', line 262

def sleeping?
  @sleeping
end

#sweep(options = {}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/bj/runner.rb', line 203

def sweep options = {}
  Bj.logger.debug{ "sweeping..." }
  Bj.transaction(options) do
    now = Time.now
    too_old = now - Bj.ttl
    jobs = Bj::Table::Job.find :all,
                               :conditions => ["(state = 'finished' or state = 'dead') and submitted_at < ?", too_old]
    jobs.each do |job|
      Bj.logger.info{ "archiving #{ job.id } (#{ job.command })." }
      hash = job.to_hash.update(:archived_at => now)
      Bj::Table::JobArchive.create! hash 
      job.destroy
    end
  end
end

#unregister(options = {}) ⇒ Object



277
278
279
280
281
# File 'lib/bj/runner.rb', line 277

def unregister options = {}
  Bj.transaction(options) do
    Bj.config.delete Runner.key
  end
end