pgbus 0.1.9 → 0.2.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 +4 -4
- data/lib/pgbus/client.rb +65 -4
- data/lib/pgbus/recurring/scheduler.rb +11 -0
- data/lib/pgbus/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e06c29b37491ad78b26d4799f11baea645e4380b2ebb6950bec74ad7be339f13
|
|
4
|
+
data.tar.gz: 712a67117a92021b45ab361476068c9885b03a8215ff02d8327e58f6b9a5abd2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f3e083cd40e0a1d02577c015283d83e3aeb8a1205bde8bb21e5e4c5e6df8b70f031f894fb1bfc93771caaeb4d26b5e1c7cca08cfa01dfad0d31a2b946db997f4
|
|
7
|
+
data.tar.gz: fe1d70f3a39bb849981a025b5d9fee6e2c447a0fc641ce771e606e42f2da8892a40e7cb68ecaa57aac47fe970c819cdd0c60f8c439d92fe1306662da309f1dfd
|
data/lib/pgbus/client.rb
CHANGED
|
@@ -33,13 +33,12 @@ module Pgbus
|
|
|
33
33
|
@pgmq_mutex = Mutex.new
|
|
34
34
|
@queues_created = Concurrent::Map.new
|
|
35
35
|
@queue_strategy = QueueFactory.for(config)
|
|
36
|
+
@schema_ensured = false
|
|
36
37
|
end
|
|
37
38
|
|
|
38
39
|
def ensure_queue(name)
|
|
40
|
+
ensure_pgmq_schema
|
|
39
41
|
@queue_strategy.physical_queue_names(name).each { |pq| ensure_single_queue(pq) }
|
|
40
|
-
rescue PGMQ::Errors::ConnectionError => e
|
|
41
|
-
raise Pgbus::SchemaNotReady,
|
|
42
|
-
"PGMQ schema is not available (#{e.message}). Run `rails db:migrate` for the pgbus database."
|
|
43
42
|
end
|
|
44
43
|
|
|
45
44
|
def ensure_all_queues
|
|
@@ -201,7 +200,9 @@ module Pgbus
|
|
|
201
200
|
|
|
202
201
|
loop do
|
|
203
202
|
deleted = synchronized do
|
|
204
|
-
|
|
203
|
+
with_raw_connection do |conn|
|
|
204
|
+
conn.exec_params(sql, [older_than, batch_size]).cmd_tuples
|
|
205
|
+
end
|
|
205
206
|
end
|
|
206
207
|
total += deleted
|
|
207
208
|
break if deleted < batch_size
|
|
@@ -254,6 +255,66 @@ module Pgbus
|
|
|
254
255
|
queues.to_a
|
|
255
256
|
end
|
|
256
257
|
|
|
258
|
+
def ensure_pgmq_schema
|
|
259
|
+
return if @schema_ensured
|
|
260
|
+
|
|
261
|
+
synchronized do
|
|
262
|
+
return if @schema_ensured
|
|
263
|
+
|
|
264
|
+
with_raw_connection do |raw_conn|
|
|
265
|
+
exists = raw_conn.exec("SELECT 1 FROM pg_tables WHERE schemaname = 'pgmq' AND tablename = 'meta' LIMIT 1")
|
|
266
|
+
install_pgmq_schema(raw_conn) if exists.ntuples.zero?
|
|
267
|
+
end
|
|
268
|
+
@schema_ensured = true
|
|
269
|
+
end
|
|
270
|
+
rescue StandardError => e
|
|
271
|
+
raise Pgbus::SchemaNotReady,
|
|
272
|
+
"PGMQ schema installation failed (#{e.class}: #{e.message}). " \
|
|
273
|
+
"Ensure the pgbus database exists and migrations have been run."
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def install_pgmq_schema(conn)
|
|
277
|
+
mode = config.pgmq_schema_mode
|
|
278
|
+
|
|
279
|
+
case mode
|
|
280
|
+
when :extension
|
|
281
|
+
Pgbus.logger.info { "[Pgbus] PGMQ schema not found — installing via extension" }
|
|
282
|
+
conn.exec("CREATE EXTENSION IF NOT EXISTS pgmq")
|
|
283
|
+
when :embedded
|
|
284
|
+
Pgbus.logger.info { "[Pgbus] PGMQ schema not found — installing embedded SQL" }
|
|
285
|
+
conn.exec(PgmqSchema.install_sql)
|
|
286
|
+
else # :auto
|
|
287
|
+
ext = conn.exec("SELECT 1 FROM pg_available_extensions WHERE name = 'pgmq' LIMIT 1")
|
|
288
|
+
if ext.ntuples.positive?
|
|
289
|
+
Pgbus.logger.info { "[Pgbus] PGMQ schema not found — installing via extension" }
|
|
290
|
+
conn.exec("CREATE EXTENSION IF NOT EXISTS pgmq")
|
|
291
|
+
else
|
|
292
|
+
Pgbus.logger.info { "[Pgbus] PGMQ schema not found — installing embedded SQL" }
|
|
293
|
+
conn.exec(PgmqSchema.install_sql)
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def with_raw_connection
|
|
299
|
+
opts = config.connection_options
|
|
300
|
+
owned = false
|
|
301
|
+
conn = case opts
|
|
302
|
+
when Proc
|
|
303
|
+
opts.call
|
|
304
|
+
when String
|
|
305
|
+
owned = true
|
|
306
|
+
PG.connect(opts)
|
|
307
|
+
when Hash
|
|
308
|
+
owned = true
|
|
309
|
+
PG.connect(**opts)
|
|
310
|
+
else
|
|
311
|
+
raise ConfigurationError, "Cannot resolve raw PG connection from #{opts.class}"
|
|
312
|
+
end
|
|
313
|
+
yield conn
|
|
314
|
+
ensure
|
|
315
|
+
conn&.close if owned
|
|
316
|
+
end
|
|
317
|
+
|
|
257
318
|
def ensure_single_queue(full_name)
|
|
258
319
|
return if @queues_created[full_name]
|
|
259
320
|
|
|
@@ -17,6 +17,7 @@ module Pgbus
|
|
|
17
17
|
def run
|
|
18
18
|
setup_signals
|
|
19
19
|
start_heartbeat
|
|
20
|
+
sync_recurring_tasks
|
|
20
21
|
|
|
21
22
|
Pgbus.logger.info do
|
|
22
23
|
"[Pgbus] Scheduler started: #{schedule.tasks.size} recurring tasks, " \
|
|
@@ -84,6 +85,16 @@ module Pgbus
|
|
|
84
85
|
|
|
85
86
|
private
|
|
86
87
|
|
|
88
|
+
def sync_recurring_tasks
|
|
89
|
+
tasks = config.recurring_tasks
|
|
90
|
+
return if tasks.nil?
|
|
91
|
+
|
|
92
|
+
RecurringTask.sync_from_config!(tasks)
|
|
93
|
+
Pgbus.logger.info { "[Pgbus] Synced #{tasks.size} recurring task(s) from configuration" }
|
|
94
|
+
rescue StandardError => e
|
|
95
|
+
Pgbus.logger.error { "[Pgbus] Failed to sync recurring tasks: #{e.class}: #{e.message}" }
|
|
96
|
+
end
|
|
97
|
+
|
|
87
98
|
def start_heartbeat
|
|
88
99
|
@heartbeat = Process::Heartbeat.new(
|
|
89
100
|
kind: "scheduler",
|
data/lib/pgbus/version.rb
CHANGED