Class: Actions::CheckLongRunningTasks

Inherits:
EntryAction show all
Includes:
RecurringAction
Defined in:
app/lib/actions/check_long_running_tasks.rb

Constant Summary collapse

INTERVAL =
2.days
STATES =
['running', 'paused'].freeze

Instance Method Summary collapse

Methods included from RecurringAction

included, #trigger_repeat

Methods inherited from EntryAction

#action_subject, all_action_names, #delay, #drop_all_locks!, #humanized_input, #resource_locks, serializer_class

Methods included from Helpers::Lock

#exclusive_lock!, #link!, #lock!

Methods included from Helpers::ArgsSerialization

#serialize_args

Methods inherited from Base

#already_running?, #humanized_errors, #humanized_input, #humanized_output, #notify_paused, #serializer_class, #task, #task_input, #task_output

Methods included from TaskSynchronization

included, #sync_execution_plan_to_task

Methods included from Helpers::LifecycleLogging

included, #log_task_state_change

Instance Method Details

#humanized_nameObject



41
42
43
# File 'app/lib/actions/check_long_running_tasks.rb', line 41

def humanized_name
  _('Check for long running tasks')
end

#planObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/lib/actions/check_long_running_tasks.rb', line 8

def plan
  time = Time.now.utc
  cutoff = time - INTERVAL
  notification = ::ForemanTasks::TasksMailNotification.find_by(name: "long_running_tasks")
  org_admin_role = Role.find_by(name: 'Organization admin')
  users = User.left_joins(:roles)
              .where(id: UserMailNotification.where(mail_notification_id: notification.id).select(:host_id))
              .or(User.where(admin: true))
              .or(User.where(id: UserRole.where(id: [org_admin_role.id] + org_admin_role.cloned_role_ids).select(:owner_id)))

  query = "state ^ (#{STATES.join(', ')}) AND state_updated_at <= \"#{cutoff}\""
  users.each do |user|
    User.as(user) do
      tasks = ForemanTasks::Task.authorized
                                .search_for(query)
                                .select(:id)
                                .pluck(:id)
      plan_action(DeliverLongRunningTasksNotification,
                  OpenStruct.new(
                    user_id: User.current.id,
                    time: time,
                    interval: INTERVAL,
                    states: STATES,
                    task_uuids: tasks,
                    query: query,
                    # Non serializable fields
                    user: nil,
                    tasks: nil
                  ))
    end
  end
end