Class: GoodJob::JobPerformer

Inherits:
Object
  • Object
show all
Defined in:
lib/good_job/job_performer.rb

Overview

JobPerformer queries the database for jobs and performs them on behalf of a Scheduler. It mainly functions as glue between a Scheduler and the jobs it should be executing.

The JobPerformer must be safe to execute across multiple threads.

Instance Method Summary collapse

Constructor Details

#initialize(queue_string) ⇒ JobPerformer

Returns a new instance of JobPerformer.

Parameters:

  • queue_string (String)

    Queues to execute jobs from



14
15
16
17
18
19
# File 'lib/good_job/job_performer.rb', line 14

def initialize(queue_string)
  @queue_string = queue_string

  @job_query = Concurrent::Delay.new { GoodJob::Job.queue_string(queue_string) }
  @parsed_queues = Concurrent::Delay.new { GoodJob::Job.queue_parser(queue_string) }
end

Instance Method Details

#nameString

A meaningful name to identify the performer in logs and for debugging.

Returns:

  • (String)

    The queues from which Jobs are worked



23
24
25
# File 'lib/good_job/job_performer.rb', line 23

def name
  @queue_string
end

#nextObject?

Perform the next eligible job

Returns:

  • (Object, nil)

    Returns job result or nil if no job was found



29
30
31
# File 'lib/good_job/job_performer.rb', line 29

def next
  job_query.perform_with_advisory_lock
end

#next?(state = {}) ⇒ Boolean

Tests whether this performer should be used in GoodJob’s current state.

For example, state will be a LISTEN/NOTIFY message that is passed down from the Notifier to the Scheduler. The Scheduler is able to ask its performer “does this message relate to you?”, and if not, ignore it to minimize thread wake-ups, database queries, and thundering herds.

Returns:

  • (Boolean)

    whether the performer’s #next method should be called in the current state.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/good_job/job_performer.rb', line 42

def next?(state = {})
  return true unless state[:queue_name]

  if parsed_queues[:exclude]
    parsed_queues[:exclude].exclude?(state[:queue_name])
  elsif parsed_queues[:include]
    parsed_queues[:include].include?(state[:queue_name])
  else
    true
  end
end

#next_at(after: nil, limit: nil, now_limit: nil) ⇒ Array<DateTime, Time>?

The Returns timestamps of when next tasks may be available.

Parameters:

  • after (DateTime, Time, nil) (defaults to: nil)

    future jobs scheduled after this time

  • limit (Integer) (defaults to: nil)

    number of future timestamps to return

  • now_limit (Integer) (defaults to: nil)

    number of past timestamps to return

Returns:

  • (Array<DateTime, Time>, nil)


59
60
61
# File 'lib/good_job/job_performer.rb', line 59

def next_at(after: nil, limit: nil, now_limit: nil)
  job_query.next_scheduled_at(after: after, limit: limit, now_limit: now_limit)
end