sequel-listen_job 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c061e74a8c846375e765903a3a0beb396e12f0b2df1e462ca44e026b38636a6d
4
+ data.tar.gz: 1f6b95b9ba2381e31032436556ffa32639466a0821fc7a450f368ff4363fba1e
5
+ SHA512:
6
+ metadata.gz: dcbc23c385192cc46eeacb238c26f3d6b5a41e66b6f830a18ebb5e6cc67525ddbc19be852bd70c2745b5b5ee890da976503ce19468d4724bad71cdde8d156593
7
+ data.tar.gz: 7bdcda6b6f945aef221e023853c64fbb473463c56965b41f3d70a077fd838ed792115a18b842bb2f24e8843fc5d3d8d067ac1ff51aa03e8dccf5badf1e0596ce
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ All notable changes to sequel-listen_job. Format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); SemVer per
5
+ [SemVer 2.0](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [1.0.0] - 2026-12-03
8
+
9
+ ### Added
10
+
11
+ - Initial release, see the README for what the gem does
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ASIO Technology AB
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # sequel-listen_job
2
+
3
+ A Sequel extension that adds `Database#listen_job` — a structured loop over
4
+ PostgreSQL `LISTEN`/`NOTIFY`, built on Sequel's native `:loop` and
5
+ `:after_listen` hooks.
6
+
7
+ The block fires once per notification, once per timeout (with nils), and
8
+ optionally once at startup. One `LISTEN` stays open across all iterations on a
9
+ single backend connection.
10
+
11
+ ## Installation
12
+
13
+ ```ruby
14
+ # Gemfile
15
+ gem 'sequel-listen_job'
16
+ gem 'pg'
17
+ ```
18
+
19
+ ```sh
20
+ bundle install
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ Activate on your Sequel database:
26
+
27
+ ```ruby
28
+ require 'sequel/extensions/listen_job'
29
+ DB.extension :listen_job
30
+ ```
31
+
32
+ Listen on one or more channels:
33
+
34
+ ```ruby
35
+ DB.listen_job([:orders, :payments], timeout: 10, loop: true, warmup: true) do |channel, pid, payload|
36
+ if channel.nil?
37
+ # warmup tick (at startup) or 10s timeout — useful as a heartbeat
38
+ reconcile_pending
39
+ else
40
+ process(channel, payload)
41
+ end
42
+ end
43
+ ```
44
+
45
+ ## Options
46
+
47
+ | Keyword | Default | Meaning |
48
+ |------------|---------|------------------------------------------------------------------------|
49
+ | `:timeout` | `5` | Seconds to wait for a notification before yielding `(nil, nil, nil)`. |
50
+ | `:loop` | `false` | Keep processing after the first iteration. |
51
+ | `:warmup` | `false` | When paired with `loop: true`, invoke the block once with `(nil, nil, nil)` immediately after `LISTEN` is sent, before any waiting. Ignored when `:loop` is false. |
52
+
53
+ The block receives:
54
+
55
+ - `channel` — the channel name as a string, or `nil` on timeout/warmup.
56
+ - `pid` — backend PID that sent the notification, or `nil`.
57
+ - `payload` — the `NOTIFY` payload as a string, or `nil`.
58
+
59
+ `break` from inside the block exits the loop. Cleanup (`UNLISTEN *` and
60
+ draining pending notifications) is handled by Sequel's own `ensure`.
61
+
62
+ ## Example: ping/pong worker
63
+
64
+ ```ruby
65
+ require 'sequel'
66
+ require 'sequel/extensions/listen_job'
67
+
68
+ DB = Sequel.connect(ENV.fetch('DATABASE_URL'))
69
+ DB.extension :listen_job
70
+
71
+ stop = false
72
+ trap('TERM') { stop = true }
73
+ trap('INT') { stop = true }
74
+
75
+ DB.listen_job(:ping, timeout: 5, loop: true) do |channel, _pid, payload|
76
+ break if stop
77
+ if channel
78
+ puts "received: #{payload}"
79
+ else
80
+ puts "tick at #{Time.now.utc.iso8601}"
81
+ end
82
+ end
83
+ ```
84
+
85
+ Trigger from psql:
86
+
87
+ ```sql
88
+ NOTIFY ping, 'hi';
89
+ ```
90
+
91
+ ## How it works
92
+
93
+ `Database#listen_job` is a thin wrapper around
94
+ `Sequel::Postgres::Database#listen`:
95
+
96
+ - The supplied block is invoked from Sequel's `:loop` callable, which fires
97
+ after every `wait_for_notify` return — both real notifications and
98
+ timeouts. A captured-value flag lets the block see notification args on
99
+ notify and nils on timeout in a single call site.
100
+ - `warmup: true` wires to Sequel's `:after_listen` hook, which runs once with
101
+ the live connection right after `LISTEN` is sent and before any waiting.
102
+ - One `LISTEN` stays open across iterations on a single backend connection;
103
+ no per-iteration `LISTEN`/`UNLISTEN` churn.
104
+
105
+ ## Requirements
106
+
107
+ - Ruby ≥ 3.4
108
+ - Sequel ≥ 5.0
109
+ - A PostgreSQL database (and the `pg` gem in your project)
110
+
111
+ ## Development
112
+
113
+ ```sh
114
+ git clone <repo>
115
+ cd sequel-listen_job
116
+ bundle install
117
+ bundle exec rspec
118
+ ```
119
+
120
+ The test suite uses `Sequel.mock` and does not require a real PostgreSQL
121
+ database.
122
+
123
+ ## License
124
+
125
+ MIT. See [LICENSE.txt](LICENSE.txt).
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sequel'
4
+ require_relative '../listen_job/version'
5
+
6
+ module Sequel
7
+ # Sequel extension that adds +listen_job+ for processing PostgreSQL
8
+ # LISTEN/NOTIFY notifications in a structured loop.
9
+ #
10
+ # Built on Sequel's native +:loop+ and +:after_listen+ hooks: a single
11
+ # +LISTEN+ stays open across iterations, and the supplied block fires on
12
+ # every notification, every timeout, and (optionally) once at startup.
13
+ #
14
+ # Activate on a database with:
15
+ # DB.extension :listen_job
16
+ #
17
+ # @example
18
+ # DB.listen_job(:orders, timeout: 10, loop: true, warmup: true) do |channel, pid, payload|
19
+ # if channel.nil?
20
+ # logger.debug('warmup or 10s timeout')
21
+ # else
22
+ # process(payload)
23
+ # end
24
+ # end
25
+ module ListenJob
26
+ # Listens for PostgreSQL notifications on one or more channels and yields
27
+ # to the block for each notification, each timeout, and (optionally) once
28
+ # at startup before any waiting.
29
+ #
30
+ # @param channels [Symbol, String, Array<Symbol, String>] One or more
31
+ # channel names to LISTEN on.
32
+ # @param timeout [Numeric] Seconds to wait for a notification.
33
+ # @param loop [Boolean] Keep processing after the first iteration.
34
+ # @param warmup [Boolean] When paired with +loop: true+, invoke the
35
+ # block once with +(nil, nil, nil)+ immediately after +LISTEN+ is
36
+ # sent, before any waiting. Ignored when +loop+ is false.
37
+ # @yield [channel, pid, payload]
38
+ # @yieldparam channel [String, nil] Channel name, or +nil+ on timeout
39
+ # or warmup.
40
+ # @yieldparam pid [Integer, nil] PID of the notifying backend, or +nil+.
41
+ # @yieldparam payload [String, nil] Notification payload, or +nil+.
42
+ # @return [Object] The return value of the last block invocation.
43
+ # @raise [ArgumentError] If called without a block.
44
+ def listen_job(channels, timeout: 5, loop: false, warmup: false)
45
+ raise ArgumentError, 'Block is required' unless block_given?
46
+
47
+ channels = Array(channels)
48
+ listen_opts = { timeout: }
49
+ result = captured = nil
50
+
51
+ if loop
52
+ listen_opts[:after_listen] = ->(_conn) { result = yield(nil, nil, nil) } if warmup
53
+ listen_opts[:loop] = lambda do |_conn|
54
+ result = yield(*(captured || [nil, nil, nil]))
55
+ captured = nil
56
+ end
57
+ listen(channels, listen_opts) { |c, p, pl| captured = [c, p, pl] }
58
+ else
59
+ listen(channels, listen_opts) { |c, p, pl| captured = [c, p, pl] }
60
+ result = yield(*(captured || [nil, nil, nil]))
61
+ end
62
+
63
+ result
64
+ end
65
+ end
66
+
67
+ Database.register_extension(:listen_job, ListenJob)
68
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sequel
4
+ module ListenJob
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Require shim matching the gem name, the extension lives under
4
+ # sequel/extensions/listen_job.
5
+ require_relative 'sequel/extensions/listen_job'
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-listen_job
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcel Asio
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: sequel
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: "5.0"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: "5.0"
28
+ - !ruby/object:Gem::Dependency
29
+ name: pg
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ -
33
+ - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ -
41
+ - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ - !ruby/object:Gem::Dependency
45
+ name: rake
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ -
49
+ - ~>
50
+ - !ruby/object:Gem::Version
51
+ version: "13.0"
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ -
57
+ - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: "13.0"
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ -
65
+ - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: "3.13"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ -
73
+ - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: "3.13"
76
+ - !ruby/object:Gem::Dependency
77
+ name: rubocop
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ -
81
+ - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: "1.69"
84
+ type: :development
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ -
89
+ - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: "1.69"
92
+ - !ruby/object:Gem::Dependency
93
+ name: rubocop-performance
94
+ requirement: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ -
97
+ - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: "1.23"
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ -
105
+ - ~>
106
+ - !ruby/object:Gem::Version
107
+ version: "1.23"
108
+ - !ruby/object:Gem::Dependency
109
+ name: rubocop-rake
110
+ requirement: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ -
113
+ - ~>
114
+ - !ruby/object:Gem::Version
115
+ version: "0.6"
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ -
121
+ - ~>
122
+ - !ruby/object:Gem::Version
123
+ version: "0.6"
124
+ - !ruby/object:Gem::Dependency
125
+ name: rubocop-rspec
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ -
129
+ - ~>
130
+ - !ruby/object:Gem::Version
131
+ version: "3.3"
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ -
137
+ - ~>
138
+ - !ruby/object:Gem::Version
139
+ version: "3.3"
140
+ - !ruby/object:Gem::Dependency
141
+ name: rubocop-sequel
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ -
145
+ - ~>
146
+ - !ruby/object:Gem::Version
147
+ version: "0.4"
148
+ type: :development
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ -
153
+ - ~>
154
+ - !ruby/object:Gem::Version
155
+ version: "0.4"
156
+ description: "Adds Database#listen_job to Sequel, wrapping LISTEN/NOTIFY with timeouts that fire the block, an optional warmup call, and natural break-to-exit semantics. Built on Sequel's native :loop and :after_listen hooks."
157
+ email:
158
+ - listen_job@asio.se
159
+ executables: []
160
+ extensions: []
161
+ extra_rdoc_files: []
162
+ files:
163
+ - CHANGELOG.md
164
+ - LICENSE.txt
165
+ - README.md
166
+ - lib/sequel-listen_job.rb
167
+ - lib/sequel/extensions/listen_job.rb
168
+ - lib/sequel/listen_job/version.rb
169
+ homepage: "https://github.com/asio/sequel-listen_job"
170
+ licenses:
171
+ - MIT
172
+ metadata:
173
+ homepage_uri: "https://github.com/asio/sequel-listen_job"
174
+ source_code_uri: "https://github.com/asio/sequel-listen_job"
175
+ changelog_uri: "https://github.com/asio/sequel-listen_job/blob/main/CHANGELOG.md"
176
+ rubygems_mfa_required: "true"
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ -
183
+ - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: "3.4"
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ -
189
+ - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: "0"
192
+ requirements: []
193
+ rubygems_version: 4.1.1
194
+ specification_version: 4
195
+ summary: Sequel extension for PostgreSQL LISTEN/NOTIFY in a structured loop
196
+ test_files: []