Method: BinScript#run!

Defined in:
lib/bin_script/bin_script.rb

#run!Object

Create lock file, call script code and unlock file even if error happend.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/bin_script/bin_script.rb', line 259

def run!

  # Print usage and exit if asked
  usage_exit if params(:h)

  check_required_params

  # Create and check lock file if enabled
  if self.class.enable_locking
    @lock = LockFile.new(lock_filename)
    @lock.quiet = true # Don't write errors to STDERR

    if(@lock.lock)
      msg = "--- Try start. Buy lock file '#{@lock.path}' already open in exclusive mode. Exit! ---"
      # puts msg # puts is not good idea, because cron will mail it, but this is not error
      warn msg
      exit
    end
  end

  begin
    # Log important info and call script job
    info ""

    log_params = {:env => RailsStub.env, :log_level => (self.class.enable_logging ? @logger.level : nil), :lock_file => (self.class.enable_locking ? @lock.path : nil)}

    info "> Script #{self.class.script_name} started! (#{log_params.inspect})"
    info "- Parameters: #{@params_values.inspect}"

    start = Time.now

    # Инкрементируем счетчик запусков этого скрипта
    inc_counter("#{self.class.script_name}_times")

    do!
    duration = Time.now - start
    info "< Script #{self.class.script_name} finished! (#{"%.4f" % duration.to_f} sec)"
    info "Exit status: #{@exit_status}" if @exit_status

    # Инкрементируем время работы э
    inc_counter("#{self.class.script_name}_long", duration)

    # Log benchmarker info if it's not empty
    log_benchmarker_data
  rescue Exception => e
    # Print error info if it's not test env or exit

    exit_mes = (e.class == SystemExit) || (e.class == Interrupt) || (e.class == SignalException) || (RailsStub.env == 'test')
    unless exit_mes
      msg = self.class.prepare_exception_message(e)
      puts "\n" + msg
      fatal msg
      notify_about_error(e)
    else
      error "Get exit message! #{e.message}"
    end

    # Инкрементируем счетчик ошибок этого скрипта
    inc_counter("#{self.class.script_name}_raised")
  ensure
    # Unlock lock file
    @lock.unlock if self.class.enable_locking && @lock
  end
end