Class: MaintenanceTasks::TaskDataShow Private

Inherits:
Object
  • Object
show all
Defined in:
app/models/maintenance_tasks/task_data_show.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class that represents the data related to a Task. Such information can be sourced from a Task or from existing Run records for a Task that was since deleted. This class contains detailed information such as the source code, associated runs, parameters, etc.

Instances of this class replace a Task class instance in cases where we don’t need the actual Task subclass.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, runs_cursor: nil, arguments: nil) ⇒ TaskDataShow

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a Task Data with a name.

Parameters:

  • name (String)

    the name of the Task subclass.

  • runs_cursor (String, nil) (defaults to: nil)

    the cursor for the runs page.

  • arguments (Hash, nil) (defaults to: nil)

    the Task arguments.



19
20
21
22
23
# File 'app/models/maintenance_tasks/task_data_show.rb', line 19

def initialize(name, runs_cursor: nil, arguments: nil)
  @name = name
  @arguments = arguments
  @runs_page = RunsPage.new(completed_runs, runs_cursor)
end

Instance Attribute Details

#nameString (readonly) Also known as: to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the name of the Task.

Returns:

  • (String)

    the name of the Task.



40
41
42
# File 'app/models/maintenance_tasks/task_data_show.rb', line 40

def name
  @name
end

#runs_pageRunsPage (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the current page of completed runs, based on the cursor passed in initialize.

Returns:

  • (RunsPage)

    the current page of completed runs, based on the cursor passed in initialize.



45
46
47
# File 'app/models/maintenance_tasks/task_data_show.rb', line 45

def runs_page
  @runs_page
end

Class Method Details

.prepare(name, runs_cursor: nil, arguments: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares a Task Data from a task name.

Parameters:

  • name (String)

    the name of the Task subclass.

  • runs_cursor (String, nil) (defaults to: nil)

    the cursor for the runs page.

  • arguments (Hash, nil) (defaults to: nil)

    the Task arguments.

Raises:

  • (Task::NotFoundError)

    if the Task doesn’t have runs (for the given cursor) and doesn’t exist.



32
33
34
35
36
# File 'app/models/maintenance_tasks/task_data_show.rb', line 32

def prepare(name, runs_cursor: nil, arguments: nil)
  new(name, runs_cursor:, arguments:)
    .load_active_runs
    .ensure_task_exists
end

Instance Method Details

#active_runsActiveRecord::Relation<MaintenanceTasks::Run>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the set of currently active Run records associated with the Task.

Returns:



72
73
74
# File 'app/models/maintenance_tasks/task_data_show.rb', line 72

def active_runs
  @active_runs ||= runs.active
end

#codeString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The Task’s source code.

Returns:

  • (String)

    the contents of the file which defines the Task.

  • (nil)

    if the Task file was deleted.



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

def code
  return if deleted?

  task = Task.named(name)
  file = if Object.respond_to?(:const_source_location)
    Object.const_source_location(task.name).first
  else
    task.instance_method(:process).source_location.first
  end
  File.read(file)
end

#completed_runsActiveRecord::Relation<MaintenanceTasks::Run>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the set of completed Run records associated with the Task. This collection represents a historic of past Runs for information purposes, since the base for Task Data information comes primarily from currently active runs.

Returns:



83
84
85
# File 'app/models/maintenance_tasks/task_data_show.rb', line 83

def completed_runs
  @completed_runs ||= runs.completed
end

#csv_task?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether the Task inherits from CsvTask.

Returns:

  • (Boolean)

    whether the Task inherits from CsvTask.



96
97
98
# File 'app/models/maintenance_tasks/task_data_show.rb', line 96

def csv_task?
  !deleted? && Task.named(name).has_csv_content?
end

#deleted?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether the Task has been deleted.

Returns:

  • (Boolean)

    whether the Task has been deleted.



88
89
90
91
92
93
# File 'app/models/maintenance_tasks/task_data_show.rb', line 88

def deleted?
  Task.named(name)
  false
rescue Task::NotFoundError
  true
end

#ensure_task_existsself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (self)

Raises:

  • (Task::NotFoundError)

    if the Task doesn’t have Runs (for the given cursor) and doesn’t exist.



132
133
134
135
136
137
# File 'app/models/maintenance_tasks/task_data_show.rb', line 132

def ensure_task_exists
  if active_runs.none? && runs_page.records.none?
    Task.named(name)
  end
  self
end

#load_active_runsself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Preloads the records from the active_runs ActiveRecord::Relation

Returns:

  • (self)


125
126
127
128
# File 'app/models/maintenance_tasks/task_data_show.rb', line 125

def load_active_runs
  active_runs.load
  self
end

#newMaintenanceTasks::Task?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/maintenance_tasks/task_data_show.rb', line 111

def new
  return if deleted?

  task = MaintenanceTasks::Task.named(name).new
  begin
    task.assign_attributes(@arguments) if @arguments
  rescue ActiveModel::UnknownAttributeError
    # nothing to do
  end
  task
end

#parameter_namesArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the names of parameters the Task accepts.

Returns:

  • (Array<String>)

    the names of parameters the Task accepts.



101
102
103
104
105
106
107
# File 'app/models/maintenance_tasks/task_data_show.rb', line 101

def parameter_names
  if deleted?
    []
  else
    Task.named(name).attribute_names
  end
end

#refresh?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether the task data needs to be refreshed.

Returns:

  • (Boolean)

    whether the task data needs to be refreshed.



64
65
66
# File 'app/models/maintenance_tasks/task_data_show.rb', line 64

def refresh?
  active_runs.any?
end