mankai-jobs-sql 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +24 -0
- data/LICENSE +20 -0
- data/README.md +268 -0
- data/lib/mankai/jobs/sql/admin.rb +245 -0
- data/lib/mankai/jobs/sql/backend.rb +236 -0
- data/lib/mankai/jobs/sql/database.rb +103 -0
- data/lib/mankai/jobs/sql/diagnostics.rb +91 -0
- data/lib/mankai/jobs/sql/factory.rb +140 -0
- data/lib/mankai/jobs/sql/hanami_diagnostics.rb +14 -0
- data/lib/mankai/jobs/sql/migration.rb +107 -0
- data/lib/mankai/jobs/sql/options.rb +155 -0
- data/lib/mankai/jobs/sql/process_registry.rb +126 -0
- data/lib/mankai/jobs/sql/processes.rb +451 -0
- data/lib/mankai/jobs/sql/record.rb +122 -0
- data/lib/mankai/jobs/sql/records.rb +91 -0
- data/lib/mankai/jobs/sql/recurring_tasks.rb +66 -0
- data/lib/mankai/jobs/sql/semaphores.rb +111 -0
- data/lib/mankai/jobs/sql/store.rb +558 -0
- data/lib/mankai/jobs/sql/supervisor/forked_children.rb +181 -0
- data/lib/mankai/jobs/sql/supervisor/threaded_children.rb +87 -0
- data/lib/mankai/jobs/sql/supervisor.rb +152 -0
- data/lib/mankai/jobs/sql/tables.rb +51 -0
- data/lib/mankai/jobs/sql/templates/add_jobs_controls.rb.erb +55 -0
- data/lib/mankai/jobs/sql/templates/create_jobs_tables.rb.erb +168 -0
- data/lib/mankai/jobs/sql/version.rb +11 -0
- data/lib/mankai/jobs/sql.rb +51 -0
- data/lib/mankai-jobs-sql.rb +3 -0
- data/mankai-jobs-sql.gemspec +36 -0
- metadata +120 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "dry/schema"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
module Jobs
|
|
7
|
+
module SQL
|
|
8
|
+
# Validates and fills in defaults for the options given to
|
|
9
|
+
# Mankai::Jobs::SQL.backend(...).
|
|
10
|
+
#
|
|
11
|
+
# @api private
|
|
12
|
+
# @since 0.1.0
|
|
13
|
+
module Options
|
|
14
|
+
# @api private
|
|
15
|
+
# @since 0.1.0
|
|
16
|
+
WORKER_DEFAULTS = {queues: ["*"], threads: 3, processes: 1, polling_interval: 0.1}.freeze
|
|
17
|
+
# @api private
|
|
18
|
+
# @since 0.1.0
|
|
19
|
+
DISPATCHER_DEFAULTS = {polling_interval: 1, batch_size: 500, concurrency_maintenance_interval: 600}.freeze
|
|
20
|
+
|
|
21
|
+
# Lifecycle hooks: each is called with the process (in that process) when it starts or stops.
|
|
22
|
+
#
|
|
23
|
+
# @api private
|
|
24
|
+
# @since 0.1.0
|
|
25
|
+
HOOKS = %i[
|
|
26
|
+
supervisor_start supervisor_stop worker_start worker_stop
|
|
27
|
+
dispatcher_start dispatcher_stop scheduler_start scheduler_stop
|
|
28
|
+
].freeze
|
|
29
|
+
|
|
30
|
+
# @api private
|
|
31
|
+
# @since 0.1.0
|
|
32
|
+
DEFAULTS = {
|
|
33
|
+
gateway: "db.gateway",
|
|
34
|
+
table_prefix: "jobs_",
|
|
35
|
+
preserve_finished: true,
|
|
36
|
+
in_transaction: :join,
|
|
37
|
+
heartbeat_interval: 60,
|
|
38
|
+
alive_threshold: 300,
|
|
39
|
+
shutdown_timeout: 5,
|
|
40
|
+
workers: [WORKER_DEFAULTS],
|
|
41
|
+
dispatchers: [DISPATCHER_DEFAULTS],
|
|
42
|
+
scheduler: true,
|
|
43
|
+
dynamic_recurring_tasks: false,
|
|
44
|
+
recurring_reload_interval: 5,
|
|
45
|
+
silence_polling: true,
|
|
46
|
+
fork_boot_timeout: 300,
|
|
47
|
+
retention: {},
|
|
48
|
+
retention_interval: 600,
|
|
49
|
+
hooks: {}
|
|
50
|
+
}.freeze
|
|
51
|
+
|
|
52
|
+
Schema = Dry::Schema.define do
|
|
53
|
+
optional(:gateway)
|
|
54
|
+
optional(:table_prefix).filled(:string)
|
|
55
|
+
optional(:preserve_finished).filled(:bool)
|
|
56
|
+
optional(:in_transaction).filled(:symbol, included_in?: %i[join after_commit raise])
|
|
57
|
+
optional(:heartbeat_interval).filled(:integer, gt?: 0)
|
|
58
|
+
optional(:alive_threshold).filled(:integer, gt?: 0)
|
|
59
|
+
optional(:shutdown_timeout).filled(:integer, gteq?: 0)
|
|
60
|
+
optional(:workers).array(:hash) do
|
|
61
|
+
optional(:queues).array(:string)
|
|
62
|
+
optional(:threads).filled(:integer, gt?: 0)
|
|
63
|
+
optional(:processes).filled(:integer, gt?: 0)
|
|
64
|
+
optional(:polling_interval).filled(:float, gt?: 0)
|
|
65
|
+
end
|
|
66
|
+
optional(:dispatchers).array(:hash) do
|
|
67
|
+
optional(:polling_interval).filled(:float, gt?: 0)
|
|
68
|
+
optional(:batch_size).filled(:integer, gt?: 0)
|
|
69
|
+
optional(:concurrency_maintenance_interval).filled(:integer, gt?: 0)
|
|
70
|
+
end
|
|
71
|
+
optional(:scheduler).filled(:bool)
|
|
72
|
+
optional(:dynamic_recurring_tasks).filled(:bool)
|
|
73
|
+
optional(:recurring_reload_interval).filled(:integer, gt?: 0)
|
|
74
|
+
optional(:silence_polling).filled(:bool)
|
|
75
|
+
optional(:fork_boot_timeout).filled(:integer, gt?: 0)
|
|
76
|
+
optional(:retention).value(:hash)
|
|
77
|
+
optional(:retention_interval).filled(:integer, gt?: 0)
|
|
78
|
+
optional(:hooks).filled(:hash)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
module_function
|
|
82
|
+
|
|
83
|
+
# @return [Hash] options with defaults
|
|
84
|
+
# @raise [ConfigurationError]
|
|
85
|
+
#
|
|
86
|
+
# @api private
|
|
87
|
+
# @since 0.1.0
|
|
88
|
+
def call(options)
|
|
89
|
+
options = coerce_numbers(options)
|
|
90
|
+
result = Schema.call(options)
|
|
91
|
+
raise ConfigurationError, "invalid mankai-jobs-sql options: #{result.errors.to_h}" if result.failure?
|
|
92
|
+
|
|
93
|
+
values = DEFAULTS.merge(result.to_h)
|
|
94
|
+
values[:hooks] = normalize_hooks(values[:hooks])
|
|
95
|
+
values[:retention] = normalize_retention(values[:retention])
|
|
96
|
+
values.merge(
|
|
97
|
+
workers: values[:workers].map { |worker| WORKER_DEFAULTS.merge(worker) },
|
|
98
|
+
dispatchers: values[:dispatchers].map { |dispatcher| DISPATCHER_DEFAULTS.merge(dispatcher) }
|
|
99
|
+
)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# {worker_start: callable or [callables]} -> {worker_start: [callables]}
|
|
103
|
+
#
|
|
104
|
+
# @api private
|
|
105
|
+
# @since 0.1.0
|
|
106
|
+
def normalize_hooks(hooks)
|
|
107
|
+
unknown = hooks.keys.map(&:to_sym) - HOOKS
|
|
108
|
+
raise ConfigurationError, "unknown hooks #{unknown.inspect}; use #{HOOKS.inspect}" if unknown.any?
|
|
109
|
+
|
|
110
|
+
hooks.to_h do |name, callables|
|
|
111
|
+
callables = Array(callables)
|
|
112
|
+
raise ConfigurationError, "hook #{name} must be callable" unless callables.all? { |callable|
|
|
113
|
+
callable.respond_to?(:call)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
[name.to_sym, callables]
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# {"mailers" => 3600, "*" => 86_400, audit: nil} -> String keys; positive seconds or nil.
|
|
121
|
+
#
|
|
122
|
+
# @api private
|
|
123
|
+
# @since 0.1.0
|
|
124
|
+
def normalize_retention(retention)
|
|
125
|
+
retention.to_h do |queue, seconds|
|
|
126
|
+
unless seconds.nil? || (seconds.is_a?(Numeric) && seconds.positive?)
|
|
127
|
+
raise ConfigurationError,
|
|
128
|
+
"retention for #{queue.inspect} must be a positive number of seconds, or nil to keep forever"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
[queue.to_s, seconds]
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Polling intervals are often written as integers; accept them.
|
|
136
|
+
#
|
|
137
|
+
# @api private
|
|
138
|
+
# @since 0.1.0
|
|
139
|
+
def coerce_numbers(options)
|
|
140
|
+
options = options.dup
|
|
141
|
+
%i[workers dispatchers].each do |kind|
|
|
142
|
+
next unless options[kind].is_a?(Array)
|
|
143
|
+
|
|
144
|
+
options[kind] = options[kind].map do |process|
|
|
145
|
+
process = process.to_h.transform_keys(&:to_sym)
|
|
146
|
+
process[:polling_interval] = process[:polling_interval].to_f if process[:polling_interval].is_a?(Integer)
|
|
147
|
+
process
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
options
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
module Jobs
|
|
7
|
+
module SQL
|
|
8
|
+
# The processes table: workers, dispatchers, schedulers and supervisors register here and
|
|
9
|
+
# send heartbeats; processes that stop sending them are pruned and their jobs failed.
|
|
10
|
+
#
|
|
11
|
+
# @api private
|
|
12
|
+
# @since 0.1.0
|
|
13
|
+
class ProcessRegistry
|
|
14
|
+
# @param store [Store] releases or fails the claims of processes that go away
|
|
15
|
+
#
|
|
16
|
+
# @api private
|
|
17
|
+
# @since 0.1.0
|
|
18
|
+
def initialize(database:, store:)
|
|
19
|
+
@database = database
|
|
20
|
+
@store = store
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @return [Integer] the new process id
|
|
24
|
+
#
|
|
25
|
+
# @api private
|
|
26
|
+
# @since 0.1.0
|
|
27
|
+
def register(kind:, name:, pid:, hostname:, supervisor_id: nil, metadata: {})
|
|
28
|
+
current = @database.now
|
|
29
|
+
table.insert(
|
|
30
|
+
kind: kind.to_s, name: name, pid: pid, hostname: hostname, supervisor_id: supervisor_id,
|
|
31
|
+
metadata: JSON.generate(metadata), last_heartbeat_at: current, created_at: current
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @return [Boolean] false if the process row is gone (it was pruned)
|
|
36
|
+
#
|
|
37
|
+
# @api private
|
|
38
|
+
# @since 0.1.0
|
|
39
|
+
def heartbeat(process_id)
|
|
40
|
+
table.where(id: process_id).update(last_heartbeat_at: @database.now).positive?
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Removes a process. Its claimed jobs are released (graceful) or failed (pruned).
|
|
44
|
+
#
|
|
45
|
+
# @api private
|
|
46
|
+
# @since 0.1.0
|
|
47
|
+
def deregister(process_id, pruned: false)
|
|
48
|
+
if pruned
|
|
49
|
+
@store.fail_claims_of(process_id, pruned_error(process_id))
|
|
50
|
+
else
|
|
51
|
+
@store.release_claims(process_id)
|
|
52
|
+
end
|
|
53
|
+
table.where(id: process_id).delete
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Removes a process that exited without deregistering, failing its claimed jobs with
|
|
57
|
+
# `error`.
|
|
58
|
+
#
|
|
59
|
+
# @api private
|
|
60
|
+
# @since 0.1.0
|
|
61
|
+
def remove_exited(process_id, error)
|
|
62
|
+
@store.fail_claims_of(process_id, error)
|
|
63
|
+
table.where(id: process_id).delete
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Deregisters processes whose last heartbeat is older than `alive_threshold` seconds,
|
|
67
|
+
# failing their claimed jobs.
|
|
68
|
+
#
|
|
69
|
+
# @return [Integer] how many processes were pruned
|
|
70
|
+
#
|
|
71
|
+
# @api private
|
|
72
|
+
# @since 0.1.0
|
|
73
|
+
def prune(alive_threshold:, excluding: nil)
|
|
74
|
+
cutoff = @database.now - alive_threshold
|
|
75
|
+
candidates = table.where(Sequel[:last_heartbeat_at] < cutoff)
|
|
76
|
+
candidates = candidates.exclude(id: excluding) if excluding
|
|
77
|
+
candidates.select_map(:id).count { |process_id| prune_one(process_id, cutoff) }
|
|
78
|
+
.tap { |size| @database.event(:prune_processes, size: size) if size.positive? }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# @api private
|
|
82
|
+
# @since 0.1.0
|
|
83
|
+
def find(**conditions)
|
|
84
|
+
table.where(conditions).first
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# @return [Array<Integer>] ids of the processes a supervisor started
|
|
88
|
+
#
|
|
89
|
+
# @api private
|
|
90
|
+
# @since 0.1.0
|
|
91
|
+
def ids_supervised_by(supervisor_id)
|
|
92
|
+
table.where(supervisor_id: supervisor_id).select_map(:id)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
private
|
|
96
|
+
|
|
97
|
+
# Deletes the row only if no other process pruned it, or it heartbeat, meanwhile.
|
|
98
|
+
#
|
|
99
|
+
# @api private
|
|
100
|
+
# @since 0.1.0
|
|
101
|
+
def prune_one(process_id, cutoff)
|
|
102
|
+
deleted = @database.transaction do
|
|
103
|
+
row = @database.skip_locked(table.where(id: process_id)).first
|
|
104
|
+
row && row[:last_heartbeat_at] < cutoff && table.where(id: process_id).delete.positive?
|
|
105
|
+
end
|
|
106
|
+
return false unless deleted
|
|
107
|
+
|
|
108
|
+
@store.fail_claims_of(process_id, pruned_error(process_id))
|
|
109
|
+
true
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# @api private
|
|
113
|
+
# @since 0.1.0
|
|
114
|
+
def pruned_error(process_id)
|
|
115
|
+
ProcessPrunedError.new("process #{process_id} stopped sending heartbeats")
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# @api private
|
|
119
|
+
# @since 0.1.0
|
|
120
|
+
def table
|
|
121
|
+
@database.table(:processes)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|