Class: Hyrax::Operation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/hyrax/operation.rb

Overview

The database storage of inter-related jobs and their states.

Direct Known Subclasses

BatchCreateOperation

Constant Summary collapse

PENDING =
'pending'
PERFORMING =
'performing'
FAILURE =
'failure'
SUCCESS =
'success'

Instance Method Summary collapse

Instance Method Details

#fail!(message = nil) ⇒ Object

TODO:

Where are these callbacks defined? Document this

Note:

This will run any registered :failure callbacks

Mark this operation as a FAILURE. If this is a child operation, roll up to the parent any failures.

Parameters:

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

    record any failure message

See Also:



77
78
79
80
81
82
# File 'app/models/hyrax/operation.rb', line 77

def fail!(message = nil)
  run_callbacks :failure do
    update(status: FAILURE, message: message)
    parent&.rollup_status
  end
end

#pending_job(job) ⇒ Object

Sets the operation status to PENDING

Parameters:

  • job (#class, #job_id)
    • The job associated with this operation

See Also:



93
94
95
# File 'app/models/hyrax/operation.rb', line 93

def pending_job(job)
  update(job_class: job.class.to_s, job_id: job.job_id, status: Hyrax::Operation::PENDING)
end

#performing!Object

Sets the operation status to PERFORMING

See Also:



86
87
88
# File 'app/models/hyrax/operation.rb', line 86

def performing!
  update(status: PERFORMING)
end

#rollup_messagesObject

Roll up messages for an operation and all of its children



46
47
48
49
50
51
52
53
# File 'app/models/hyrax/operation.rb', line 46

def rollup_messages
  [].tap do |messages|
    messages << message if message.present?
    children&.pluck(:message)&.uniq&.each do |child_message|
      messages << child_message if child_message.present?
    end
  end
end

#rollup_statusObject

If this is a batch job (has children), check to see if all the children are complete



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/hyrax/operation.rb', line 29

def rollup_status
  with_lock do
    # We don't need all of the status of the children, just need to see
    # if there is at least one PENDING, PERFORMING, or FAILURE.
    # however, we can't use distinct with order by (from acts_as_nested_set)
    # with postgres or you get the following error:
    #   ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
    stats = children.pluck(:status).uniq

    # Don't mark as pass or fail until all jobs are complete
    return if stats.include?(PENDING) || stats.include?(PERFORMING)
    return fail! if stats.include?(FAILURE)
    success!
  end
end

#success!Object

TODO:

Where are these callbacks defined? Document this

Note:

This will run any registered :success callbacks

Mark this operation as a SUCCESS. If this is a child operation, roll up to the parent any failures.



62
63
64
65
66
67
# File 'app/models/hyrax/operation.rb', line 62

def success!
  run_callbacks :success do
    update(status: SUCCESS)
    parent&.rollup_status
  end
end