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

#resume(run) ⇒ TaskJob

Resumes a Task.

This method re-instantiates and re-enqueues a job for a Run that was previously paused.

Parameters:

Returns:

  • (TaskJob)

    the enqueued Task job.

Raises:



66
67
68
69
70
71
# File 'app/models/maintenance_tasks/runner.rb', line 66

def resume(run)
  job = instantiate_job(run)
  run.job_id = job.job_id
  run.enqueued!
  enqueue(run, job)
end

#run(name:, csv_file: nil, arguments: {}, run_model: Run, metadata: nil) {|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.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/maintenance_tasks/runner.rb', line 42

def run(name:, csv_file: nil, arguments: {}, run_model: Run, metadata: nil)
  run = run_model.new(task_name: name, arguments: arguments, metadata: )
  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