Class: ForemanTasks::Task

Inherits:
ApplicationRecord
  • Object
show all
Extended by:
Search
Includes:
Authorizable
Defined in:
app/models/foreman_tasks/task.rb,
app/models/foreman_tasks/task/search.rb

Direct Known Subclasses

DynflowTask

Defined Under Namespace

Modules: Search Classes: DynflowTask, Jail, StatusExplicator, Summarizer, TaskCancelledException

Constant Summary

Constants included from Search

Search::SUPPORTED_DURATION_FORMAT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Search

search_by_duration, search_by_generic_resource, search_by_taxonomy

Class Method Details

.authorized_resource_nameObject



203
204
205
206
# File 'app/models/foreman_tasks/task.rb', line 203

def self.authorized_resource_name
  # We don't want STI subclasses to have separate permissions
  'ForemanTasks::Task'
end

Instance Method Details

#actionObject



234
235
236
237
# File 'app/models/foreman_tasks/task.rb', line 234

def action
  return to_label if super.blank?
  super
end

#action_continuous_outputObject



252
253
254
255
256
# File 'app/models/foreman_tasks/task.rb', line 252

def action_continuous_output
  return unless main_action.is_a?(Actions::Helpers::WithContinuousOutput)
  main_action.continuous_output.sort!
  main_action.continuous_output.raw_outputs
end

#add_missing_task_groups(groups) ⇒ Object



208
209
210
211
212
# File 'app/models/foreman_tasks/task.rb', line 208

def add_missing_task_groups(groups)
  groups = [groups] unless groups.is_a? Array
  (groups - task_groups).each { |group| task_groups << group }
  save!
end

#build_notificationsObject



180
181
182
183
184
185
186
187
188
189
190
# File 'app/models/foreman_tasks/task.rb', line 180

def build_notifications
  notifications = []
  if paused?
    owner = self.owner
    if owner && !owner.hidden?
      notifications << UINotifications::Tasks::TaskPausedOwner.new(self)
    end
    notifications << UINotifications::Tasks::TaskPausedAdmin.new(self)
  end
  notifications
end

#check_permissions_after_saveObject



8
9
10
11
12
13
# File 'app/models/foreman_tasks/task.rb', line 8

def check_permissions_after_save
  # there's no create_tasks permission, tasks are created as a result of internal actions, in such case we
  # don't do authorization, that should have been performed on wrapping action level
  # the same applies for updating
  true
end

#cli_exampleObject



137
138
139
# File 'app/models/foreman_tasks/task.rb', line 137

def cli_example
  ''
end

#delayed?Boolean

returns true if the task was planned to execute in the future

Returns:

  • (Boolean)


165
166
167
# File 'app/models/foreman_tasks/task.rb', line 165

def delayed?
  start_at.to_i != started_at.to_i
end

#execution_typeObject



111
112
113
# File 'app/models/foreman_tasks/task.rb', line 111

def execution_type
  delayed? ? N_('Delayed') : N_('Immediate')
end

#get_humanized(method) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/foreman_tasks/task.rb', line 115

def get_humanized(method)
  attr = case method
         when :humanized_name
           :action
         when :humanized_input
           :input
         when :humanized_output
           :output
         end
  if attr
    humanized[attr]
  else
    _('N/A')
  end
end

#humanizedObject



131
132
133
134
135
# File 'app/models/foreman_tasks/task.rb', line 131

def humanized
  { action: action,
    input:  '',
    output: '' }
end

#inputObject



95
96
97
# File 'app/models/foreman_tasks/task.rb', line 95

def input
  {}
end

#notification_recipients_idsObject

used by Foreman notifications framework



176
177
178
# File 'app/models/foreman_tasks/task.rb', line 176

def notification_recipients_ids
  [owner.id]
end

#outputObject



99
100
101
# File 'app/models/foreman_tasks/task.rb', line 99

def output
  {}
end

#ownerObject



103
104
105
# File 'app/models/foreman_tasks/task.rb', line 103

def owner
  user
end

#paused?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'app/models/foreman_tasks/task.rb', line 151

def paused?
  state == 'paused'
end

#pending?Boolean Also known as: pending

returns true if the task is running or waiting to be run

Returns:

  • (Boolean)


142
143
144
# File 'app/models/foreman_tasks/task.rb', line 142

def pending?
  state != 'stopped'
end

#progressObject



192
193
194
195
196
197
198
199
200
201
# File 'app/models/foreman_tasks/task.rb', line 192

def progress
  case state.to_s
  when 'running', 'paused'
    0.5
  when 'stopped'
    1
  else
    0
  end
end

#recurring?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'app/models/foreman_tasks/task.rb', line 160

def recurring?
  !recurring_logic_task_group_ids.empty?
end

#resumable?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/models/foreman_tasks/task.rb', line 147

def resumable?
  false
end

#scheduled?Boolean

returns true if the task is CURRENTLY waiting to be executed in the future

Returns:

  • (Boolean)


156
157
158
# File 'app/models/foreman_tasks/task.rb', line 156

def scheduled?
  state == 'scheduled'
end

#self_and_parentsObject



169
170
171
172
173
# File 'app/models/foreman_tasks/task.rb', line 169

def self_and_parents
  [self].tap do |ret|
    ret.concat(parent_task.self_and_parents) if parent_task
  end
end

#sub_tasks_countsObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/models/foreman_tasks/task.rb', line 214

def sub_tasks_counts
  result = %w[cancelled error pending success warning].inject({}) do |hash, state|
    hash.update(state => 0)
  end
  result.update sub_tasks.group(:result).count
  sum = result.values.reduce(:+)
  if respond_to?(:main_action) && main_action.respond_to?(:total_count)
    result[:total] = main_action.total_count
    # In case of batch planning there might be some plans still unplanned (not present in database).
    # To get correct counts we need to add them to either:
    #   cancelled when the parent is stopped
    #   pending when the parent is still running.
    key = state == 'stopped' ? 'cancelled' : 'pending'
    result[key] += result[:total] - sum
  else
    result[:total] = sum
  end
  result.symbolize_keys
end

#to_labelObject



239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/models/foreman_tasks/task.rb', line 239

def to_label
  parts = []
  parts << get_humanized(:name)
  parts << Array(get_humanized(:input)).map do |part|
    if part.is_a? Array
      part[1][:text]
    else
      part.to_s
    end
  end.join('; ')
  parts.join(' ').strip
end

#usernameObject



107
108
109
# File 'app/models/foreman_tasks/task.rb', line 107

def username
  owner.try(:login)
end