Module: MaintenanceTasks::Runner

Extended by:
Runner
Included in:
Runner
Defined in:
app/models/maintenance_tasks/runner.rb

Overview

This class is responsible for running a given Task.

Defined Under Namespace

Classes: EnqueuingError

Instance Method Summary collapse

Instance Method Details

#newObject

Deprecated.

Use MaintenanceTasks::Runner directly instead.



9
10
11
12
13
14
# File 'app/models/maintenance_tasks/runner.rb', line 9

def new
  ActiveSupport::Deprecation.warn(
    "Use Runner.run instead of Runner.new.run"
  )
  self
end

#run(name:, csv_file: nil, arguments: {}, run_model: Run) {|run| ... } ⇒ Task

Runs a Task.

This method creates a Run record for the given Task name and enqueues the Run. If a CSV file is provided, it is attached to the Run record.

Parameters:

  • name (String)

    the name of the Task to be run.

  • csv_file (attachable, nil) (defaults to: nil)

    a CSV file that provides the collection for the Task to iterate over when running, in the form of an attachable (see edgeapi.rubyonrails.org/classes/ActiveStorage/Attached/One.html#method-i-attach). Value is nil if the Task does not use CSV iteration.

  • arguments (Hash) (defaults to: {})

    the arguments to persist to the Run and to make accessible to the Task.

Yields:

Returns:

  • (Task)

    the Task that was run.

Raises:

  • (EnqueuingError)

    if an error occurs while enqueuing the Run.

  • (ActiveRecord::RecordInvalid)

    if validation errors occur while creating the Run.

  • (ActiveRecord::ValueTooLong)

    if the creation of the Run fails due to a value being too long for the column type.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/maintenance_tasks/runner.rb', line 50

def run(name:, csv_file: nil, arguments: {}, run_model: Run)
  run = run_model.active.find_by(task_name: name) ||
    run_model.new(task_name: name, arguments: arguments)
  if csv_file
    run.csv_file.attach(csv_file)
    run.csv_file.filename = filename(name)
  end
  job = instantiate_job(run)
  run.job_id = job.job_id
  yield run if block_given?
  run.enqueued!
  enqueue(run, job)
  Task.named(name)
end