Class: Spree::MaintenanceTasks::Start

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/maintenance_tasks/start.rb

Overview

Creates a run for a registered task and hands it to the runner (docs/plans/6.0-maintenance-tasks.md).

Everything that can be known before any work happens is checked here — the task exists, its parameters validate, and no run of the same task is already in flight — so an operator sees a 422 rather than a run that dies on its first record.

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(task_name: nil, api_type: nil, arguments: {}, dry_run: false, admin_user: nil, api_key: nil, initiated_via: 'api', inline: false) ⇒ Spree::ServiceModule::Base::Result

Returns the run.

Parameters:

  • task_name (String) (defaults to: nil) —

    a registered Spree::MaintenanceTask subclass name

  • api_type (String) (defaults to: nil) —

    the task's short name, in place of task_name — how an API caller names it, so a class name never has to cross the API

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

    task parameters

  • dry_run (Boolean) (defaults to: false) —

    preview mode; only honored by tasks that declare supports_dry_run

  • initiated_via (String) (defaults to: 'api') —

    dashboard / api / cli

  • inline (Boolean) (defaults to: false) —

    run synchronously (seeds, the CLI, the upgrade walk)

Returns:

  • (Spree::ServiceModule::Base::Result) —

    the run



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/spree/maintenance_tasks/start.rb', line 23

def call(task_name: nil, api_type: nil, arguments: {}, dry_run: false, admin_user: nil,
         api_key: nil, initiated_via: 'api', inline: false)
  task_class = if api_type.nil?
                 Spree::MaintenanceTask.find_registered(task_name)
               else
                 Spree::MaintenanceTask.find_by_api_type(api_type)
               end
  return failure(nil, unknown_task_error(api_type || task_name)) if task_class.nil?

  task_name = task_class.name

  arguments = (arguments || {}).stringify_keys
  unknown = arguments.keys - task_class.attribute_names
  return failure(nil, unknown_arguments_error(unknown)) if unknown.any?

  if initiated_via.to_s.in?(%w[api dashboard]) && superseded_step?(task_class, arguments)
    return failure(nil, superseded_error(task_class))
  end

  task = task_class.new(arguments)
  return failure(task, task.errors) unless task.valid?

  active = active_run_for(task_name)
  return failure(active, already_running_error(task_class)) if active

  run = build_run(task_class, arguments, dry_run, admin_user, api_key, initiated_via)
  return failure(run, run.errors) unless run.save

  dispatch(run, inline)

  success(run)
end