Sha256: dd7007e85b6d28d07b45e8124fd436c21ae3f1d39f0c08bee0b1badbd7e7fc3e

Contents?: true

Size: 1.49 KB

Versions: 29

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

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 || '-'} (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 => e
        run_log(e, :fatal)
        exception = e
      end
      exception
    end

    def invoke_args
      return [] if args.blank?

      args.split('|')
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
tasks_scheduler-0.9.4 app/models/scheduled_task/runner.rb
tasks_scheduler-0.9.3 app/models/scheduled_task/runner.rb
tasks_scheduler-0.9.2 app/models/scheduled_task/runner.rb
tasks_scheduler-0.9.1 app/models/scheduled_task/runner.rb
tasks_scheduler-0.9.0 app/models/scheduled_task/runner.rb
tasks_scheduler-0.8.0 app/models/scheduled_task/runner.rb
tasks_scheduler-0.7.0 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.13 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.12 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.11 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.10 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.9 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.8 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.7 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.6 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.5 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.4 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.3 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.2 app/models/scheduled_task/runner.rb
tasks_scheduler-0.6.1 app/models/scheduled_task/runner.rb