Sha256: 1674fd268ee70a9f940a8abfba5ad4eeaca11b30ff37ea010e52a6d931e3622c

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 KB

Contents

class ScheduledTask < ActiveRecord::Base
  module Runner
    def run
      log_on_start
      run_banner
      return if process_running? && pid != Process.pid
      status_on_start
      exception = invoke_task
      on_end_running(exception, STATUS_FAILED)
    end

    private

    def on_end_running(exception, last_fail_status)
      run_log(exception, :fatal) if exception
      status_on_end(exception, last_fail_status)
      log_on_end(exception)
      run_log("Next run: #{next_run.in_time_zone}")
    end

    def run_log(message, method = :info)
      if message.is_a?(Exception)
        run_log("#{message.class}: #{message.message}")
        if message.backtrace.present?
          run_log(message.backtrace.join("\n"))
        else
          run_log('No backtrace present')
        end
      else
        Rails.logger.send(method, "TASK_RUN(#{id}): #{message}")
      end
    end

    def run_banner
      run_log("Task: #{self}")
      run_log("PID: #{pid ? pid : '-'} (Current: #{Process.pid})")
      run_log("Process running? #{process_running? ? 'Yes' : 'No'}")
      run_log("Rails.env: #{Rails.env}")
    end

    def invoke_task
      exception = nil
      begin
        Rake::Task.clear
        Rails.application.load_tasks
        Rake::Task[task].invoke(*invoke_args)
      rescue StandardError => ex
        run_log(ex, :fatal)
        exception = ex
      end
      exception
    end

    def invoke_args
      return [] unless args.present?
      args.split('|')
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
tasks_scheduler-0.3.0 app/models/scheduled_task/runner.rb
tasks_scheduler-0.2.3 app/models/scheduled_task/runner.rb
tasks_scheduler-0.2.2 app/models/scheduled_task/runner.rb
tasks_scheduler-0.2.1 app/models/scheduled_task/runner.rb
tasks_scheduler-0.2.0 app/models/scheduled_task/runner.rb
tasks_scheduler-0.1.0 app/models/scheduled_task/runner.rb
tasks_scheduler-0.0.10 app/models/scheduled_task/runner.rb
tasks_scheduler-0.0.9 app/models/scheduled_task/runner.rb
tasks_scheduler-0.0.8 app/models/scheduled_task/runner.rb