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,181 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "socket"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
module Jobs
|
|
7
|
+
module SQL
|
|
8
|
+
class Supervisor
|
|
9
|
+
# Runs each supervised process as a forked child (mode: :fork). Isolates crashes and memory;
|
|
10
|
+
# a child that exits without deregistering has its claimed jobs failed.
|
|
11
|
+
#
|
|
12
|
+
# @api private
|
|
13
|
+
# @since 0.1.0
|
|
14
|
+
class ForkedChildren
|
|
15
|
+
# @param build [#call] builds a process from a Spec, in the child
|
|
16
|
+
# @param boot_timeout [Numeric] seconds a child has to register itself before it is killed
|
|
17
|
+
# and replaced (e.g. stuck loading the app)
|
|
18
|
+
# @param on_error [#call] logs an exception
|
|
19
|
+
#
|
|
20
|
+
# @api private
|
|
21
|
+
# @since 0.1.0
|
|
22
|
+
def initialize(build:, store:, logger:, on_error:, boot_timeout:)
|
|
23
|
+
@build = build
|
|
24
|
+
@store = store
|
|
25
|
+
@logger = logger
|
|
26
|
+
@on_error = on_error
|
|
27
|
+
@boot_timeout = boot_timeout
|
|
28
|
+
@children = {} # pid => spec
|
|
29
|
+
@booting = {} # pid => monotonic start time, until the child registers
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @api private
|
|
33
|
+
# @since 0.1.0
|
|
34
|
+
def empty?
|
|
35
|
+
@children.empty?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @api private
|
|
39
|
+
# @since 0.1.0
|
|
40
|
+
def start(spec, supervisor_id:)
|
|
41
|
+
# Children must open their own connections; never share sockets across fork.
|
|
42
|
+
@store.db.disconnect
|
|
43
|
+
pid = fork { run_child(spec, supervisor_id) }
|
|
44
|
+
@children[pid] = spec
|
|
45
|
+
@booting[pid] = monotonic
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Kills children stuck booting, and starts a new child for each one that exited.
|
|
49
|
+
#
|
|
50
|
+
# @api private
|
|
51
|
+
# @since 0.1.0
|
|
52
|
+
def replace_dead(supervisor_id:)
|
|
53
|
+
kill_stuck_booting(supervisor_id)
|
|
54
|
+
reap(supervisor_id, warn: true).each { |spec| start(spec, supervisor_id: supervisor_id) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Asks children to stop (they finish running jobs, then deregister), waiting up to
|
|
58
|
+
# `timeout` seconds.
|
|
59
|
+
#
|
|
60
|
+
# @api private
|
|
61
|
+
# @since 0.1.0
|
|
62
|
+
def stop(supervisor_id:, timeout:)
|
|
63
|
+
signal("TERM")
|
|
64
|
+
deadline = monotonic + timeout
|
|
65
|
+
until empty? || monotonic > deadline
|
|
66
|
+
reap(supervisor_id, warn: false)
|
|
67
|
+
sleep 0.1
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Children exit at once; their in-flight jobs are released by the supervisor.
|
|
72
|
+
#
|
|
73
|
+
# @api private
|
|
74
|
+
# @since 0.1.0
|
|
75
|
+
def terminate
|
|
76
|
+
signal("QUIT")
|
|
77
|
+
@children.each_key do |pid|
|
|
78
|
+
::Process.wait(pid)
|
|
79
|
+
rescue Errno::ECHILD
|
|
80
|
+
nil
|
|
81
|
+
end
|
|
82
|
+
@children.clear
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
# @api private
|
|
88
|
+
# @since 0.1.0
|
|
89
|
+
def run_child(spec, supervisor_id)
|
|
90
|
+
process = @build.call(spec, supervisor_id: supervisor_id)
|
|
91
|
+
process.watch_supervisor(::Process.ppid)
|
|
92
|
+
trap("TERM") { process.stop }
|
|
93
|
+
trap("INT") { process.stop }
|
|
94
|
+
trap("QUIT") { exit!(0) }
|
|
95
|
+
::Process.setproctitle("mankai-jobs #{process.kind} (supervisor #{::Process.ppid})")
|
|
96
|
+
process.run
|
|
97
|
+
flush_and_exit!(0)
|
|
98
|
+
rescue Exception => exception # rubocop:disable Lint/RescueException
|
|
99
|
+
@on_error.call(exception)
|
|
100
|
+
flush_and_exit!(1)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# @api private
|
|
104
|
+
# @since 0.1.0
|
|
105
|
+
def kill_stuck_booting(supervisor_id)
|
|
106
|
+
@booting.each do |pid, started|
|
|
107
|
+
if @store.processes.find(pid: pid, supervisor_id: supervisor_id)
|
|
108
|
+
@booting.delete(pid)
|
|
109
|
+
elsif monotonic - started > @boot_timeout
|
|
110
|
+
@logger.warn("[mankai-jobs] process #{pid} did not boot within #{@boot_timeout}s; replacing it")
|
|
111
|
+
@booting.delete(pid)
|
|
112
|
+
::Process.kill("KILL", pid)
|
|
113
|
+
end
|
|
114
|
+
rescue Errno::ESRCH
|
|
115
|
+
@booting.delete(pid)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# @return [Array<Spec>] specs of children that exited
|
|
120
|
+
#
|
|
121
|
+
# @api private
|
|
122
|
+
# @since 0.1.0
|
|
123
|
+
def reap(supervisor_id, warn:)
|
|
124
|
+
exited = []
|
|
125
|
+
loop do
|
|
126
|
+
pid, status = ::Process.waitpid2(-1, ::Process::WNOHANG)
|
|
127
|
+
break unless pid
|
|
128
|
+
|
|
129
|
+
spec = @children.delete(pid) or next
|
|
130
|
+
fail_jobs_of(pid, status, supervisor_id)
|
|
131
|
+
@logger.warn("[mankai-jobs] #{spec.kind} (pid #{pid}) exited: #{status.inspect}") if warn
|
|
132
|
+
exited << spec
|
|
133
|
+
end
|
|
134
|
+
exited
|
|
135
|
+
rescue Errno::ECHILD
|
|
136
|
+
exited
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# A child that exited without deregistering (crash, KILL, out of memory) leaves its row
|
|
140
|
+
# and claims behind: fail those jobs so they can be inspected.
|
|
141
|
+
#
|
|
142
|
+
# @api private
|
|
143
|
+
# @since 0.1.0
|
|
144
|
+
def fail_jobs_of(pid, status, supervisor_id)
|
|
145
|
+
row = @store.processes.find(pid: pid, hostname: Socket.gethostname, supervisor_id: supervisor_id)
|
|
146
|
+
return unless row
|
|
147
|
+
|
|
148
|
+
@store.processes.remove_exited(
|
|
149
|
+
row[:id], ProcessExitError.new("process #{pid} exited unexpectedly: #{status.inspect}")
|
|
150
|
+
)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# @api private
|
|
154
|
+
# @since 0.1.0
|
|
155
|
+
def signal(name)
|
|
156
|
+
@children.each_key do |pid|
|
|
157
|
+
::Process.kill(name, pid)
|
|
158
|
+
rescue Errno::ESRCH
|
|
159
|
+
nil
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# exit! skips at_exit hooks (which belong to the parent) but also IO buffering, so flush.
|
|
164
|
+
#
|
|
165
|
+
# @api private
|
|
166
|
+
# @since 0.1.0
|
|
167
|
+
def flush_and_exit!(code)
|
|
168
|
+
[$stdout, $stderr].each { |stream| stream.flush rescue nil } # rubocop:disable Style/RescueModifier
|
|
169
|
+
exit!(code)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# @api private
|
|
173
|
+
# @since 0.1.0
|
|
174
|
+
def monotonic
|
|
175
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
module Jobs
|
|
5
|
+
module SQL
|
|
6
|
+
class Supervisor
|
|
7
|
+
# Runs each supervised process as a thread in this process (mode: :async). Lighter than
|
|
8
|
+
# forking; good for small apps.
|
|
9
|
+
#
|
|
10
|
+
# @api private
|
|
11
|
+
# @since 0.1.0
|
|
12
|
+
class ThreadedChildren
|
|
13
|
+
# @param build [#call] builds a process from a Spec
|
|
14
|
+
# @param on_error [#call] logs an exception
|
|
15
|
+
#
|
|
16
|
+
# @api private
|
|
17
|
+
# @since 0.1.0
|
|
18
|
+
def initialize(build:, logger:, on_error:)
|
|
19
|
+
@build = build
|
|
20
|
+
@logger = logger
|
|
21
|
+
@on_error = on_error
|
|
22
|
+
@children = {} # thread => [spec, process]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @api private
|
|
26
|
+
# @since 0.1.0
|
|
27
|
+
def empty?
|
|
28
|
+
@children.empty?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @api private
|
|
32
|
+
# @since 0.1.0
|
|
33
|
+
def start(spec, supervisor_id:)
|
|
34
|
+
process = @build.call(spec, supervisor_id: supervisor_id)
|
|
35
|
+
thread = Thread.new do
|
|
36
|
+
Thread.current.name = "mankai-jobs-#{process.kind}"
|
|
37
|
+
process.run
|
|
38
|
+
rescue Exception => exception # rubocop:disable Lint/RescueException
|
|
39
|
+
@on_error.call(exception)
|
|
40
|
+
end
|
|
41
|
+
@children[thread] = [spec, process]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Starts a new thread for each one that exited.
|
|
45
|
+
#
|
|
46
|
+
# @api private
|
|
47
|
+
# @since 0.1.0
|
|
48
|
+
def replace_dead(supervisor_id:)
|
|
49
|
+
@children.each_key.reject(&:alive?).each do |thread|
|
|
50
|
+
spec, = @children.delete(thread)
|
|
51
|
+
@logger.warn("[mankai-jobs] #{spec.kind} thread exited; restarting")
|
|
52
|
+
start(spec, supervisor_id: supervisor_id)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Asks processes to stop (they finish running jobs, then deregister), waiting up to
|
|
57
|
+
# `timeout` seconds.
|
|
58
|
+
#
|
|
59
|
+
# @api private
|
|
60
|
+
# @since 0.1.0
|
|
61
|
+
def stop(timeout:, **)
|
|
62
|
+
@children.each_value { |(_, process)| process.stop }
|
|
63
|
+
deadline = monotonic + timeout
|
|
64
|
+
@children.delete_if { |thread, _| thread.join(0.1) } until empty? || monotonic > deadline
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Thread#kill runs ensure blocks, so each process still deregisters and releases its jobs.
|
|
68
|
+
#
|
|
69
|
+
# @api private
|
|
70
|
+
# @since 0.1.0
|
|
71
|
+
def terminate
|
|
72
|
+
@children.each_key { |thread| thread.kill.join(1) }
|
|
73
|
+
@children.clear
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
# @api private
|
|
79
|
+
# @since 0.1.0
|
|
80
|
+
def monotonic
|
|
81
|
+
::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
module Jobs
|
|
5
|
+
module SQL
|
|
6
|
+
# Starts the configured processes and keeps them running.
|
|
7
|
+
#
|
|
8
|
+
# mode: :fork each process is a forked child (default; isolates crashes and memory)
|
|
9
|
+
# :async each process is a thread in this process (lighter; good for small apps)
|
|
10
|
+
#
|
|
11
|
+
# It also fails jobs left claimed by dead processes: at boot (orphans), when a child exits
|
|
12
|
+
# unexpectedly, and periodically for processes whose heartbeats stopped.
|
|
13
|
+
#
|
|
14
|
+
# Signals (when run from the CLI):
|
|
15
|
+
# TERM, INT graceful: stop processes, wait up to shutdown_timeout, then force them
|
|
16
|
+
# QUIT immediate: children exit at once; their in-flight jobs go back to ready
|
|
17
|
+
#
|
|
18
|
+
# @api private
|
|
19
|
+
# @since 0.1.0
|
|
20
|
+
class Supervisor < Processes::Base
|
|
21
|
+
# @api private
|
|
22
|
+
# @since 0.1.0
|
|
23
|
+
Spec = Data.define(:kind, :options)
|
|
24
|
+
|
|
25
|
+
# @param fork_boot_timeout [Numeric] seconds a forked child has to register itself before
|
|
26
|
+
# it is killed and replaced (e.g. stuck loading the app)
|
|
27
|
+
#
|
|
28
|
+
# @api private
|
|
29
|
+
# @since 0.1.0
|
|
30
|
+
def initialize(specs:, build:, mode: :fork, shutdown_timeout: 5, alive_threshold: 300, fork_boot_timeout: 300,
|
|
31
|
+
**)
|
|
32
|
+
super(**)
|
|
33
|
+
raise ConfigurationError, "mode must be :fork or :async" unless %i[fork async].include?(mode)
|
|
34
|
+
raise ConfigurationError, "no processes to supervise" if specs.empty?
|
|
35
|
+
|
|
36
|
+
@specs = specs
|
|
37
|
+
@mode = mode
|
|
38
|
+
@shutdown_timeout = shutdown_timeout
|
|
39
|
+
@alive_threshold = alive_threshold
|
|
40
|
+
@signals = []
|
|
41
|
+
@children = children_for(mode, build, fork_boot_timeout)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# @api private
|
|
45
|
+
# @since 0.1.0
|
|
46
|
+
def metadata
|
|
47
|
+
{mode: @mode, processes: @specs.map(&:kind)}
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Runs until stopped, handling signals.
|
|
51
|
+
#
|
|
52
|
+
# @api private
|
|
53
|
+
# @since 0.1.0
|
|
54
|
+
def run_with_signals
|
|
55
|
+
previous = %w[TERM INT QUIT].to_h { |signal|
|
|
56
|
+
[signal, trap(signal) {
|
|
57
|
+
@signals << signal
|
|
58
|
+
@sleeper.wake_up
|
|
59
|
+
}]
|
|
60
|
+
}
|
|
61
|
+
run
|
|
62
|
+
ensure
|
|
63
|
+
previous&.each { |signal, handler| trap(signal, handler || "DEFAULT") }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
# @api private
|
|
69
|
+
# @since 0.1.0
|
|
70
|
+
def boot
|
|
71
|
+
super
|
|
72
|
+
store.fail_orphaned_claims
|
|
73
|
+
store.processes.prune(alive_threshold: @alive_threshold, excluding: id)
|
|
74
|
+
@maintenance = Runtime::Timer.new(@heartbeat_interval, on_error: method(:log_error)) {
|
|
75
|
+
store.processes.prune(alive_threshold: @alive_threshold, excluding: id)
|
|
76
|
+
}.start
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# @api private
|
|
80
|
+
# @since 0.1.0
|
|
81
|
+
def work
|
|
82
|
+
@specs.each { |spec| @children.start(spec, supervisor_id: id) }
|
|
83
|
+
|
|
84
|
+
until stopped?
|
|
85
|
+
@sleeper.sleep(1)
|
|
86
|
+
handle_signals
|
|
87
|
+
unless stopped?
|
|
88
|
+
@children.replace_dead(supervisor_id: id)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
@immediate ? terminate_immediately : terminate_gracefully
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Stopping on purpose (TERM past the timeout, or QUIT) is not a crash: children that did not
|
|
96
|
+
# deregister themselves are deregistered here, which puts their jobs back in ready.
|
|
97
|
+
#
|
|
98
|
+
# @api private
|
|
99
|
+
# @since 0.1.0
|
|
100
|
+
def shutdown
|
|
101
|
+
@maintenance&.stop
|
|
102
|
+
release_children if id
|
|
103
|
+
super
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# @api private
|
|
107
|
+
# @since 0.1.0
|
|
108
|
+
def handle_signals
|
|
109
|
+
while (signal = @signals.shift)
|
|
110
|
+
@immediate = true if signal == "QUIT"
|
|
111
|
+
stop
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# @api private
|
|
116
|
+
# @since 0.1.0
|
|
117
|
+
def children_for(mode, build, boot_timeout)
|
|
118
|
+
if mode == :fork
|
|
119
|
+
ForkedChildren.new(
|
|
120
|
+
build: build, store: store, logger: logger, on_error: method(:log_error), boot_timeout: boot_timeout
|
|
121
|
+
)
|
|
122
|
+
else
|
|
123
|
+
ThreadedChildren.new(build: build, logger: logger, on_error: method(:log_error))
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Children wait shutdown_timeout for running jobs, then deregister; allow time for that.
|
|
128
|
+
#
|
|
129
|
+
# @api private
|
|
130
|
+
# @since 0.1.0
|
|
131
|
+
def terminate_gracefully
|
|
132
|
+
@children.stop(supervisor_id: id, timeout: @shutdown_timeout + 2)
|
|
133
|
+
terminate_immediately unless @children.empty?
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# @api private
|
|
137
|
+
# @since 0.1.0
|
|
138
|
+
def terminate_immediately
|
|
139
|
+
@children.terminate
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# @api private
|
|
143
|
+
# @since 0.1.0
|
|
144
|
+
def release_children
|
|
145
|
+
store.processes.ids_supervised_by(id).each { |process_id| store.processes.deregister(process_id) }
|
|
146
|
+
rescue StandardError => exception
|
|
147
|
+
log_error(exception)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
module Jobs
|
|
5
|
+
module SQL
|
|
6
|
+
# Table names, all sharing a configurable prefix ("jobs_" by default).
|
|
7
|
+
#
|
|
8
|
+
# records one row per job: what to run and its final state
|
|
9
|
+
# ready jobs that can run now (the table workers poll; kept small)
|
|
10
|
+
# scheduled jobs waiting for their time (including retries)
|
|
11
|
+
# claimed jobs a worker process is running
|
|
12
|
+
# blocked jobs waiting for a concurrency slot
|
|
13
|
+
# failed jobs that ran out of attempts, kept until retried or discarded
|
|
14
|
+
# processes live worker/dispatcher/scheduler/supervisor processes and heartbeats
|
|
15
|
+
# semaphores concurrency slots per concurrency key
|
|
16
|
+
# pauses paused queue names
|
|
17
|
+
# recurring_tasks recurring tasks added at runtime (static ones come from configuration)
|
|
18
|
+
# recurring_runs one row per recurring task run, to enqueue each run only once
|
|
19
|
+
#
|
|
20
|
+
# @api private
|
|
21
|
+
# @since 0.1.0
|
|
22
|
+
class Tables
|
|
23
|
+
# @api private
|
|
24
|
+
# @since 0.1.0
|
|
25
|
+
NAMES = %i[records ready scheduled claimed blocked failed processes semaphores pauses recurring_tasks
|
|
26
|
+
recurring_runs].freeze
|
|
27
|
+
|
|
28
|
+
attr_reader :prefix
|
|
29
|
+
|
|
30
|
+
# @api private
|
|
31
|
+
# @since 0.1.0
|
|
32
|
+
def initialize(prefix = "jobs_")
|
|
33
|
+
@prefix = prefix.to_s
|
|
34
|
+
@names = NAMES.to_h { |name| [name, :"#{@prefix}#{name}"] }.freeze
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @api private
|
|
38
|
+
# @since 0.1.0
|
|
39
|
+
def [](name)
|
|
40
|
+
@names.fetch(name)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @api private
|
|
44
|
+
# @since 0.1.0
|
|
45
|
+
def to_h
|
|
46
|
+
@names
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adds unique jobs, recorded output, per-job retention, and per-queue cleanup to mankai-jobs-sql.
|
|
4
|
+
#
|
|
5
|
+
# It is idempotent: columns and indexes that already exist are left alone, so running it twice
|
|
6
|
+
# (or having two copies of it) does no harm.
|
|
7
|
+
<%= migration_dsl %>.migration do
|
|
8
|
+
up do
|
|
9
|
+
table = :<%= t[:records] %>
|
|
10
|
+
columns = schema(table, reload: true).map(&:first)
|
|
11
|
+
indexes = indexes(table).values.map { |index| index[:columns] }
|
|
12
|
+
|
|
13
|
+
alter_table(table) do
|
|
14
|
+
add_column :unique_key, String, size: 64 unless columns.include?(:unique_key)
|
|
15
|
+
add_column :unique_period, Integer unless columns.include?(:unique_period)
|
|
16
|
+
add_column :preserve_finished, TrueClass unless columns.include?(:preserve_finished)
|
|
17
|
+
add_column :output, String, text: true unless columns.include?(:output)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
alter_table(table) do
|
|
21
|
+
add_index :unique_key, unique: true unless indexes.include?([:unique_key])
|
|
22
|
+
add_index [:queue, :finished_at] unless indexes.include?([:queue, :finished_at])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# MySQL/MariaDB connected with fractional_seconds=true: give whole-second time columns
|
|
26
|
+
# (from an install made without it) microseconds.
|
|
27
|
+
if database_type == :mysql && supports_timestamp_usecs?
|
|
28
|
+
%i[<%= t.to_h.values.join(" ") %>].each do |name|
|
|
29
|
+
schema(name, reload: true).each do |column, info|
|
|
30
|
+
next unless info[:db_type] == "datetime"
|
|
31
|
+
|
|
32
|
+
alter_table(name) { set_column_type column, :"datetime(6)", null: info[:allow_null] }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
down do
|
|
39
|
+
table = :<%= t[:records] %>
|
|
40
|
+
next unless table_exists?(table)
|
|
41
|
+
|
|
42
|
+
columns = schema(table, reload: true).map(&:first)
|
|
43
|
+
indexes = indexes(table).values.map { |index| index[:columns] }
|
|
44
|
+
|
|
45
|
+
alter_table(table) do
|
|
46
|
+
drop_index [:queue, :finished_at] if indexes.include?([:queue, :finished_at])
|
|
47
|
+
drop_index :unique_key if indexes.include?([:unique_key])
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
alter_table(table) do
|
|
51
|
+
%i[output preserve_finished unique_period unique_key].each { |column| drop_column(column) if columns.include?(column) }
|
|
52
|
+
end
|
|
53
|
+
# Time columns keep their microseconds: narrowing them would lose data.
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Tables for mankai-jobs-sql. This migration is yours: adapt it as needed, and add later schema
|
|
4
|
+
# changes as regular migrations.
|
|
5
|
+
#
|
|
6
|
+
# It is idempotent: tables that already exist are left alone, so running it twice (or having two
|
|
7
|
+
# copies of it) does no harm. Rolling it back drops the tables, whichever copy is rolled back.
|
|
8
|
+
<%= migration_dsl %>.migration do
|
|
9
|
+
up do
|
|
10
|
+
create_table? :<%= t[:records] %> do
|
|
11
|
+
primary_key :id, type: :Bignum
|
|
12
|
+
String :uuid, size: 36, null: false
|
|
13
|
+
String :job, null: false
|
|
14
|
+
String :args, text: true, null: false
|
|
15
|
+
String :queue, null: false
|
|
16
|
+
Integer :priority, null: false, default: 0
|
|
17
|
+
Integer :attempts, null: false, default: 0
|
|
18
|
+
Time :scheduled_at
|
|
19
|
+
Time :enqueued_at, null: false
|
|
20
|
+
Time :finished_at
|
|
21
|
+
String :outcome, size: 16
|
|
22
|
+
String :concurrency_key
|
|
23
|
+
Integer :concurrency_limit
|
|
24
|
+
Integer :concurrency_duration
|
|
25
|
+
String :concurrency_on_conflict, size: 16
|
|
26
|
+
Bignum :semaphore_id
|
|
27
|
+
String :metadata, text: true, null: false, default: "{}"
|
|
28
|
+
Time :created_at, null: false
|
|
29
|
+
Time :updated_at, null: false
|
|
30
|
+
|
|
31
|
+
index :uuid, unique: true
|
|
32
|
+
index :finished_at
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
create_table? :<%= t[:ready] %> do
|
|
36
|
+
primary_key :id, type: :Bignum
|
|
37
|
+
foreign_key :job_id, :<%= t[:records] %>, type: :Bignum, null: false, on_delete: :cascade
|
|
38
|
+
String :queue, null: false
|
|
39
|
+
Integer :priority, null: false, default: 0
|
|
40
|
+
Time :created_at, null: false
|
|
41
|
+
|
|
42
|
+
index :job_id, unique: true
|
|
43
|
+
index [:priority, :job_id]
|
|
44
|
+
index [:queue, :priority, :job_id]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
create_table? :<%= t[:scheduled] %> do
|
|
48
|
+
primary_key :id, type: :Bignum
|
|
49
|
+
foreign_key :job_id, :<%= t[:records] %>, type: :Bignum, null: false, on_delete: :cascade
|
|
50
|
+
String :queue, null: false
|
|
51
|
+
Integer :priority, null: false, default: 0
|
|
52
|
+
Time :scheduled_at, null: false
|
|
53
|
+
Time :created_at, null: false
|
|
54
|
+
|
|
55
|
+
index :job_id, unique: true
|
|
56
|
+
index [:scheduled_at, :priority, :job_id]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
create_table? :<%= t[:claimed] %> do
|
|
60
|
+
primary_key :id, type: :Bignum
|
|
61
|
+
foreign_key :job_id, :<%= t[:records] %>, type: :Bignum, null: false, on_delete: :cascade
|
|
62
|
+
Bignum :process_id
|
|
63
|
+
Time :created_at, null: false
|
|
64
|
+
|
|
65
|
+
index :job_id, unique: true
|
|
66
|
+
index [:process_id, :job_id]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
create_table? :<%= t[:blocked] %> do
|
|
70
|
+
primary_key :id, type: :Bignum
|
|
71
|
+
foreign_key :job_id, :<%= t[:records] %>, type: :Bignum, null: false, on_delete: :cascade
|
|
72
|
+
String :queue, null: false
|
|
73
|
+
Integer :priority, null: false, default: 0
|
|
74
|
+
String :concurrency_key, null: false
|
|
75
|
+
Time :expires_at, null: false
|
|
76
|
+
Time :created_at, null: false
|
|
77
|
+
|
|
78
|
+
index :job_id, unique: true
|
|
79
|
+
index [:concurrency_key, :priority, :job_id]
|
|
80
|
+
index [:expires_at, :concurrency_key]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
create_table? :<%= t[:failed] %> do
|
|
84
|
+
primary_key :id, type: :Bignum
|
|
85
|
+
foreign_key :job_id, :<%= t[:records] %>, type: :Bignum, null: false, on_delete: :cascade
|
|
86
|
+
String :error, text: true
|
|
87
|
+
Time :created_at, null: false
|
|
88
|
+
|
|
89
|
+
index :job_id, unique: true
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
create_table? :<%= t[:processes] %> do
|
|
93
|
+
primary_key :id, type: :Bignum
|
|
94
|
+
String :kind, null: false
|
|
95
|
+
String :name, null: false
|
|
96
|
+
Integer :pid, null: false
|
|
97
|
+
String :hostname
|
|
98
|
+
Bignum :supervisor_id
|
|
99
|
+
String :metadata, text: true
|
|
100
|
+
Time :last_heartbeat_at, null: false
|
|
101
|
+
Time :created_at, null: false
|
|
102
|
+
|
|
103
|
+
index :name, unique: true
|
|
104
|
+
index :last_heartbeat_at
|
|
105
|
+
index :supervisor_id
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
create_table? :<%= t[:semaphores] %> do
|
|
109
|
+
primary_key :id, type: :Bignum
|
|
110
|
+
String :key, null: false
|
|
111
|
+
Integer :value, null: false, default: 1
|
|
112
|
+
Time :expires_at, null: false
|
|
113
|
+
Time :created_at, null: false
|
|
114
|
+
Time :updated_at, null: false
|
|
115
|
+
|
|
116
|
+
index :key, unique: true
|
|
117
|
+
index :expires_at
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
create_table? :<%= t[:pauses] %> do
|
|
121
|
+
primary_key :id, type: :Bignum
|
|
122
|
+
String :queue, null: false
|
|
123
|
+
Time :created_at, null: false
|
|
124
|
+
|
|
125
|
+
index :queue, unique: true
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
create_table? :<%= t[:recurring_tasks] %> do
|
|
129
|
+
primary_key :id, type: :Bignum
|
|
130
|
+
String :key, null: false
|
|
131
|
+
String :job, null: false
|
|
132
|
+
String :schedule, null: false
|
|
133
|
+
String :args, text: true, null: false, default: "{}"
|
|
134
|
+
String :queue
|
|
135
|
+
Integer :priority
|
|
136
|
+
String :description
|
|
137
|
+
Time :created_at, null: false
|
|
138
|
+
Time :updated_at, null: false
|
|
139
|
+
|
|
140
|
+
index :key, unique: true
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
create_table? :<%= t[:recurring_runs] %> do
|
|
144
|
+
primary_key :id, type: :Bignum
|
|
145
|
+
String :task_key, null: false
|
|
146
|
+
Time :run_at, null: false
|
|
147
|
+
Bignum :job_id
|
|
148
|
+
Time :created_at, null: false
|
|
149
|
+
|
|
150
|
+
index [:task_key, :run_at], unique: true
|
|
151
|
+
index :job_id
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
down do
|
|
156
|
+
drop_table? :<%= t[:recurring_runs] %>
|
|
157
|
+
drop_table? :<%= t[:recurring_tasks] %>
|
|
158
|
+
drop_table? :<%= t[:pauses] %>
|
|
159
|
+
drop_table? :<%= t[:semaphores] %>
|
|
160
|
+
drop_table? :<%= t[:processes] %>
|
|
161
|
+
drop_table? :<%= t[:failed] %>
|
|
162
|
+
drop_table? :<%= t[:blocked] %>
|
|
163
|
+
drop_table? :<%= t[:claimed] %>
|
|
164
|
+
drop_table? :<%= t[:scheduled] %>
|
|
165
|
+
drop_table? :<%= t[:ready] %>
|
|
166
|
+
drop_table? :<%= t[:records] %>
|
|
167
|
+
end
|
|
168
|
+
end
|