good_job 1.4.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,60 +0,0 @@
1
- module GoodJob
2
- #
3
- # Performer queries the database for jobs and performs them on behalf of a
4
- # {Scheduler}. It mainly functions as glue between a {Scheduler} and the jobs
5
- # it should be executing.
6
- #
7
- # The Performer enforces a callable that does not rely on scoped/closure
8
- # variables because they might not be available when executed in a different
9
- # thread.
10
- #
11
- class Performer
12
- # @!attribute [r] name
13
- # @return [String]
14
- # a meaningful name to identify the performer in logs and for debugging.
15
- # This is usually set to the list of queues the performer will query,
16
- # e.g. +"-transactional_messages,batch_processing"+.
17
- attr_reader :name
18
-
19
- # @param target [Object]
20
- # An object that can perform jobs. It must respond to +method_name+ by
21
- # finding and performing jobs and is usually a {Job} query,
22
- # e.g. +GoodJob::Job.where(queue_name: ['queue1', 'queue2'])+.
23
- # @param method_name [Symbol]
24
- # The name of a method on +target+ that finds and performs jobs.
25
- # @param name [String]
26
- # A name for the performer to be used in logs and for debugging.
27
- # @param filter [#call]
28
- # Used to determine whether the performer should be used in GoodJob's
29
- # current state. GoodJob state is a +Hash+ that will be passed as the
30
- # first argument to +filter+ and includes info like the current queue.
31
- def initialize(target, method_name, name: nil, filter: nil)
32
- @target = target
33
- @method_name = method_name
34
- @name = name
35
- @filter = filter
36
- end
37
-
38
- # Find and perform any eligible jobs.
39
- def next
40
- @target.public_send(@method_name)
41
- end
42
-
43
- # Tests whether this performer should be used in GoodJob's current state by
44
- # calling the +filter+ callable set in {#initialize}. Always returns +true+
45
- # if there is no filter.
46
- #
47
- # For example, state will be a LISTEN/NOTIFY message that is passed down
48
- # from the Notifier to the Scheduler. The Scheduler is able to ask
49
- # its performer "does this message relate to you?", and if not, ignore it
50
- # to minimize thread wake-ups, database queries, and thundering herds.
51
- #
52
- # @return [Boolean] whether the performer's {#next} method should be
53
- # called in the current state.
54
- def next?(state = {})
55
- return true unless @filter.respond_to?(:call)
56
-
57
- @filter.call(state)
58
- end
59
- end
60
- end