honker 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fbf6de4cce9d9b068ecf07c59977f5ffea3295bcc55e3dc043033d8855bff648
4
- data.tar.gz: 619580c9de45ccadb5ac20b41bea018c2ef5e11759f6d0a837d13c6239b76de4
3
+ metadata.gz: 04f540681d13eb8cbd385ba2f43d121d5273b73dfde940cf7f689047278fe08d
4
+ data.tar.gz: 1073ccdb529b22484fe275fff403c019963983dc250d145e68ece63abbfcb92b
5
5
  SHA512:
6
- metadata.gz: 3ac9490e51f9dd4edca01a4a4af3a28ec3603113e2f02bbf54d80784d1d6c6946cc8c6c70d805acb3d1df3e84ec3d9d84ff5076901c07315888e35b1de7a25f1
7
- data.tar.gz: db21f18ef6609c2cf330428d2518b21c903fd22dd64ec8156da3ce127ab03188181142d6d5872841e3d690e8ef2cf335a3f7494a2520b9887d3d51de8c0d8801
6
+ metadata.gz: 4b1ae556c89d8b2bdb5c77bc45936e8f465fc62a65f691da4e137df0e066283f209ab88b8064026c6649caf3626ec200f3cb76766705ef3324ad6edeefbec602
7
+ data.tar.gz: b6d8be9a6527d9cd8feb1ae6e57467d4e1c5e619c44823aa984fb52296402b917ab0215cef008bbc6d22eb88a7b5ab79385afcad688b90593f76182dd9bea33f
data/LICENSE ADDED
@@ -0,0 +1,6 @@
1
+ Honker is licensed under either of:
2
+
3
+ * Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
4
+ * MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
5
+
6
+ at your option.
data/LICENSE-APACHE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
data/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Russell Romney
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 CHANGED
@@ -1,6 +1,11 @@
1
1
  # honker (Ruby)
2
2
 
3
- Ruby binding for [Honker](https://honker.dev) durable queues, streams, pub/sub, and scheduler on SQLite.
3
+ Ruby binding for [Honker](https://github.com/russellromney/honker): durable queues, streams, pub/sub, and time-trigger scheduling on SQLite.
4
+
5
+ Full docs:
6
+
7
+ - [Main repo](https://github.com/russellromney/honker)
8
+ - [Docs](https://honker.dev)
4
9
 
5
10
  ## Install
6
11
 
@@ -8,81 +13,49 @@ Ruby binding for [Honker](https://honker.dev) — durable queues, streams, pub/s
8
13
  gem "honker"
9
14
  ```
10
15
 
11
- You'll also need the Honker SQLite extension (`libhonker.dylib` on macOS, `libhonker.so` on Linux). Prebuilds at [GitHub releases](https://github.com/russellromney/honker/releases/latest), or build:
12
-
13
- ```bash
14
- git clone https://github.com/russellromney/honker
15
- cd honker
16
- cargo build --release -p honker-extension
17
- # → target/release/libhonker_extension.{dylib,so}
18
- ```
16
+ You also need the Honker SQLite extension from the main repo.
19
17
 
20
- ## Requirements
18
+ ## Watcher backends
21
19
 
22
- - Ruby 3.0+
23
- - `sqlite3` gem 1.7 (pulled in automatically)
20
+ `Honker::Database.new(..., watcher_backend: "polling")` accepts the
21
+ default polling backend aliases (`"polling"` / `"poll"`). Experimental
22
+ `"kernel"` / `"shm"` requests route through `honker-core` via the loaded
23
+ Honker extension and fail loudly if that extension was not built with
24
+ the matching feature.
24
25
 
25
26
  ## Quick start
26
27
 
27
28
  ```ruby
28
29
  require "honker"
29
30
 
30
- db = Honker::Database.new("app.db", extension_path: "./libhonker_extension.dylib")
31
- q = db.queue("emails")
31
+ db = Honker::Database.new("app.db", extension_path: "./libhonker_ext.dylib")
32
+ q = db.queue("emails")
32
33
 
33
- # Enqueue (atomic-with-your-write via business transactions; see below)
34
- q.enqueue({ to: "alice@example.com" })
34
+ q.enqueue({to: "alice@example.com"})
35
35
 
36
- # Claim + process + ack
37
- job = q.claim_one("worker-1")
38
- if job
36
+ if (job = q.claim_one("worker-1"))
39
37
  send_email(job.payload)
40
38
  job.ack
41
39
  end
42
40
  ```
43
41
 
44
- ## API
45
-
46
- ### `Honker::Database.new(path, extension_path:)`
47
-
48
- Opens (or creates) a SQLite database, loads the Honker extension, applies default PRAGMAs, and bootstraps the schema.
49
-
50
- ### `#queue(name, visibility_timeout_s: 300, max_attempts: 3)`
51
-
52
- Handle to a named queue.
53
-
54
- ### `Queue#enqueue(payload, delay:, run_at:, priority:, expires:)`
55
-
56
- Inserts a job. `payload` is any JSON-serializable value. Returns the row id.
57
-
58
- ### `Queue#claim_batch(worker_id, n)` / `Queue#claim_one(worker_id)`
42
+ Delayed jobs use `run_at:`:
59
43
 
60
- Atomically claim up to N jobs (or 1).
61
-
62
- ### `Job#ack` / `Job#retry(delay_s:, error:)` / `Job#fail(error:)` / `Job#heartbeat(extend_s:)`
63
-
64
- Claim lifecycle. `ack` deletes. `retry` puts back with a delay (or moves to `_honker_dead` if max_attempts reached). `fail` moves to dead unconditionally. `heartbeat` extends the visibility timeout.
65
-
66
- ### `Database#notify(channel, payload)`
67
-
68
- Fire a `pg_notify`-style signal. Returns the notification id.
69
-
70
- ## What's not here yet
44
+ ```ruby
45
+ q.enqueue({to: "later@example.com"}, run_at: Time.now.to_i + 10)
46
+ ```
71
47
 
72
- - `listen` / WAL-based async iterator (watcher API — in progress)
73
- - Streams (durable pub/sub with per-consumer offsets)
74
- - Scheduler (cron-style periodic tasks)
48
+ Recurring schedules use `schedule:`:
75
49
 
76
- All available via raw SQL on the same database (`db.db.execute("SELECT honker_stream_publish(...)")`). Idiomatic Ruby wrappers coming in a future release.
50
+ ```ruby
51
+ sched = db.scheduler
52
+ sched.add(name: "fast", queue: "emails", schedule: "@every 1s", payload: {kind: "tick"})
53
+ ```
77
54
 
78
- ## Testing
55
+ Supported schedule forms:
79
56
 
80
- ```bash
81
- # Build the extension
82
- cd /path/to/honker && cargo build --release -p honker-extension
57
+ - `0 3 * * *`
58
+ - `*/2 * * * * *`
59
+ - `@every 1s`
83
60
 
84
- # Run the tests
85
- cd packages/honker-ruby
86
- bundle install
87
- bundle exec rake test # or: ruby -Ilib -Ispec spec/honker_spec.rb
88
- ```
61
+ `schedule:` is the canonical recurring name. `cron:` still works as a compatibility alias.
data/honker.gemspec CHANGED
@@ -10,20 +10,24 @@ Gem::Specification.new do |spec|
10
10
  spec.summary = "Durable queues, streams, pub/sub, and scheduler on SQLite."
11
11
  spec.description = <<~DESC.strip
12
12
  Ruby binding for Honker — a SQLite-native task runtime. Queues,
13
- streams, pub/sub, cron scheduler, results, locks, rate limits, all
13
+ streams, pub/sub, time-trigger scheduler, results, locks, rate limits, all
14
14
  in one .db file. Thin wrapper around the Honker SQLite loadable
15
15
  extension; no Redis, no external broker.
16
16
  DESC
17
17
  spec.homepage = "https://honker.dev"
18
- spec.license = "Apache-2.0"
18
+ spec.licenses = ["MIT", "Apache-2.0"]
19
19
  spec.required_ruby_version = ">= 3.0.0"
20
20
 
21
21
  spec.metadata["homepage_uri"] = spec.homepage
22
22
  spec.metadata["source_code_uri"] = "https://github.com/russellromney/honker"
23
23
  spec.metadata["documentation_uri"] = "https://honker.dev/"
24
24
 
25
- spec.files = Dir.glob("lib/**/*") + %w[honker.gemspec README.md]
25
+ spec.files = Dir.glob("lib/**/*") + %w[honker.gemspec README.md LICENSE LICENSE-MIT LICENSE-APACHE]
26
26
  spec.require_paths = ["lib"]
27
27
 
28
- spec.add_dependency "sqlite3", ">= 1.7"
28
+ # Honker loads the SQLite extension directly, so the Ruby sqlite3
29
+ # binding must expose Database#enable_load_extension/#load_extension.
30
+ # sqlite3 2.0.4 is the first line compatible with our Ruby >= 3.0 floor
31
+ # that reliably ships those APIs in the supported native builds.
32
+ spec.add_dependency "sqlite3", ">= 2.0.4", "< 3"
29
33
  end
@@ -10,7 +10,7 @@ module Honker
10
10
  end
11
11
  end
12
12
 
13
- # Cron-style scheduler. Register tasks with `add`; `tick` fires all
13
+ # Time-trigger scheduler. Register tasks with `add`; `tick` fires all
14
14
  # boundaries that have elapsed since the last tick and enqueues the
15
15
  # resulting jobs. `run(owner:, stop:)` drives the loop under a
16
16
  # leader-elected advisory lock.
@@ -28,18 +28,30 @@ module Honker
28
28
  # write; too large and a standby waits longer than necessary after
29
29
  # a crash. Matches the Rust binding.
30
30
  HEARTBEAT_S = 20
31
+ UPDATE_POLL_S = 0.05
31
32
 
32
33
  def initialize(db)
33
34
  @db = db
34
35
  end
35
36
 
36
- # Register a cron-scheduled task. Idempotent by `name`; registering
37
- # the same name twice replaces the previous row.
38
- def add(name:, queue:, cron:, payload:, priority: 0, expires_s: nil)
37
+ # Register a scheduled task. `cron:` is kept for backward
38
+ # compatibility; `schedule:` is the clearer name and can hold:
39
+ #
40
+ # - 5-field cron
41
+ # - 6-field cron
42
+ # - `@every <n><unit>` like `@every 1s`
43
+ #
44
+ # Idempotent by `name`; registering the same name twice replaces
45
+ # the previous row.
46
+ def add(name:, queue:, cron: nil, schedule: nil, payload:, priority: 0, expires_s: nil)
47
+ expr = schedule || cron
48
+ raise ArgumentError, "must provide cron: or schedule:" if expr.nil? || expr.empty?
49
+
39
50
  @db.db.get_first_row(
40
51
  "SELECT honker_scheduler_register(?, ?, ?, ?, ?, ?)",
41
- [name, queue, cron, JSON.dump(payload), priority, expires_s],
52
+ [name, queue, expr, JSON.dump(payload), priority, expires_s],
42
53
  )
54
+ @db.mark_updated
43
55
  nil
44
56
  end
45
57
 
@@ -49,7 +61,7 @@ module Honker
49
61
  @db.db.get_first_row(
50
62
  "SELECT honker_scheduler_unregister(?)",
51
63
  [name],
52
- )[0]
64
+ )[0].tap { @db.mark_updated }
53
65
  end
54
66
 
55
67
  # Fire all due boundaries at `now`. Returns an array of
@@ -86,7 +98,7 @@ module Honker
86
98
  until stop_fn.call
87
99
  acquired = lock_try_acquire(LEADER_LOCK, owner, LOCK_TTL_S)
88
100
  unless acquired
89
- sleep_with_stop(5, stop_fn)
101
+ wait_for_update_or_timeout(5, stop_fn)
90
102
  next
91
103
  end
92
104
 
@@ -113,16 +125,6 @@ module Honker
113
125
  "stop must be callable (proc/lambda) or respond to :stop?"
114
126
  end
115
127
 
116
- # Break sleeps into 1s increments so `stop` is honored promptly
117
- # without busy-waiting.
118
- def sleep_with_stop(total_s, stop_fn)
119
- elapsed = 0
120
- while elapsed < total_s && !stop_fn.call
121
- sleep(1)
122
- elapsed += 1
123
- end
124
- end
125
-
126
128
  def leader_loop(owner, stop_fn)
127
129
  last_heartbeat = monotonic_now
128
130
  until stop_fn.call
@@ -138,7 +140,18 @@ module Honker
138
140
 
139
141
  last_heartbeat = monotonic_now
140
142
  end
141
- sleep(1)
143
+
144
+ wait_s = HEARTBEAT_S - (monotonic_now - last_heartbeat)
145
+ wait_s = 0 if wait_s.negative?
146
+
147
+ next_fire = soonest
148
+ if next_fire.positive?
149
+ until_next = next_fire - Time.now.to_i
150
+ until_next = 0 if until_next.negative?
151
+ wait_s = [wait_s, until_next].min
152
+ end
153
+
154
+ wait_for_update_or_timeout(wait_s, stop_fn)
142
155
  end
143
156
  end
144
157
 
@@ -146,6 +159,20 @@ module Honker
146
159
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
147
160
  end
148
161
 
162
+ def wait_for_update_or_timeout(total_s, stop_fn)
163
+ return if total_s <= 0
164
+
165
+ deadline = monotonic_now + total_s
166
+
167
+ until stop_fn.call
168
+ now = monotonic_now
169
+ break if now >= deadline
170
+
171
+ slice = [0.1, deadline - now].min
172
+ return if @db.wait_for_update(slice)
173
+ end
174
+ end
175
+
149
176
  def lock_try_acquire(name, owner, ttl_s)
150
177
  @db.db.get_first_row(
151
178
  "SELECT honker_lock_acquire(?, ?, ?)",
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Honker
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/honker.rb CHANGED
@@ -18,6 +18,7 @@
18
18
  # is one SQL call via the `sqlite3` gem. No extra process, no Redis.
19
19
 
20
20
  require "json"
21
+ require "fiddle"
21
22
  require "sqlite3"
22
23
 
23
24
  require_relative "honker/version"
@@ -27,10 +28,52 @@ require_relative "honker/scheduler"
27
28
  require_relative "honker/lock"
28
29
 
29
30
  module Honker
31
+ class CoreWatcher
32
+ def initialize(db_path, extension_path, backend)
33
+ @lib = Fiddle.dlopen(extension_path)
34
+ @open = Fiddle::Function.new(
35
+ @lib["honker_watcher_open"],
36
+ [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_SIZE_T],
37
+ Fiddle::TYPE_VOIDP,
38
+ )
39
+ @wait = Fiddle::Function.new(
40
+ @lib["honker_watcher_wait"],
41
+ [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG_LONG],
42
+ Fiddle::TYPE_INT,
43
+ )
44
+ @close = Fiddle::Function.new(
45
+ @lib["honker_watcher_close"],
46
+ [Fiddle::TYPE_VOIDP],
47
+ Fiddle::TYPE_VOID,
48
+ )
49
+ err = "\0" * 1024
50
+ @handle = @open.call(db_path.to_s, backend.to_s, err, err.bytesize)
51
+ return unless @handle.to_i.zero?
52
+
53
+ raise ArgumentError, err.delete_suffix("\0").split("\0", 2).first
54
+ end
55
+
56
+ def wait(timeout_s)
57
+ code = @wait.call(@handle, (timeout_s * 1000).ceil)
58
+ return true if code == 1
59
+ return false if code == 0
60
+
61
+ raise Error, "honker update watcher closed or died"
62
+ end
63
+
64
+ def close
65
+ return if @handle.nil? || @handle.to_i.zero?
66
+
67
+ @close.call(@handle)
68
+ @handle = nil
69
+ end
70
+ end
71
+
30
72
  DEFAULT_PRAGMAS = <<~SQL
31
73
  PRAGMA journal_mode = WAL;
32
74
  PRAGMA synchronous = NORMAL;
33
75
  PRAGMA busy_timeout = 5000;
76
+ PRAGMA mmap_size = 0;
34
77
  PRAGMA foreign_keys = ON;
35
78
  PRAGMA cache_size = -32000;
36
79
  PRAGMA temp_store = MEMORY;
@@ -43,19 +86,40 @@ module Honker
43
86
  class Database
44
87
  attr_reader :db
45
88
 
46
- def initialize(path, extension_path:)
89
+ def initialize(path, extension_path:, watcher_backend: nil)
90
+ unless watcher_backend.nil? || watcher_backend.is_a?(String)
91
+ raise ArgumentError, "unknown watcher backend"
92
+ end
93
+
47
94
  @db = SQLite3::Database.new(path)
95
+ @local_update_seq = 0
96
+ @db.busy_timeout = 5000
97
+ @db.execute("PRAGMA mmap_size = 0")
48
98
  @db.enable_load_extension(true)
49
99
  @db.load_extension(extension_path)
50
100
  @db.enable_load_extension(false)
51
101
  @db.execute_batch(DEFAULT_PRAGMAS)
52
102
  @db.execute("SELECT honker_bootstrap()")
103
+ @watcher = CoreWatcher.new(path, extension_path, watcher_backend)
53
104
  end
54
105
 
55
106
  def close
107
+ @watcher&.close
56
108
  @db&.close
57
109
  end
58
110
 
111
+ def mark_updated
112
+ @local_update_seq += 1
113
+ end
114
+
115
+ def update_snapshot
116
+ @local_update_seq
117
+ end
118
+
119
+ def wait_for_update(timeout_s)
120
+ @watcher.wait(timeout_s)
121
+ end
122
+
59
123
  # Returns a Queue handle for a named queue.
60
124
  #
61
125
  # visibility_timeout_s: 300 # claim expiry before reclaim
@@ -69,12 +133,24 @@ module Honker
69
133
  )
70
134
  end
71
135
 
136
+ # Transactional side-effect delivery built on a reserved queue.
137
+ def outbox(name, delivery, visibility_timeout_s: 60, max_attempts: 5, base_backoff_s: 5)
138
+ Outbox.new(
139
+ self,
140
+ name,
141
+ delivery,
142
+ visibility_timeout_s: visibility_timeout_s,
143
+ max_attempts: max_attempts,
144
+ base_backoff_s: base_backoff_s,
145
+ )
146
+ end
147
+
72
148
  # Returns a Stream handle for an append-only ordered log.
73
149
  def stream(name)
74
150
  Stream.new(self, name)
75
151
  end
76
152
 
77
- # Returns the cron-style Scheduler facade. Cheap — no allocation
153
+ # Returns the time-trigger Scheduler facade. Cheap — no allocation
78
154
  # beyond the wrapper object.
79
155
  def scheduler
80
156
  Scheduler.new(self)
@@ -274,6 +350,65 @@ module Honker
274
350
  end
275
351
  end
276
352
 
353
+ class Outbox
354
+ attr_reader :name, :queue, :max_attempts, :base_backoff_s
355
+
356
+ def initialize(db, name, delivery, visibility_timeout_s:, max_attempts:, base_backoff_s:)
357
+ raise ArgumentError, "delivery must respond to #call" unless delivery.respond_to?(:call)
358
+
359
+ @name = name
360
+ @delivery = delivery
361
+ @max_attempts = max_attempts
362
+ @base_backoff_s = base_backoff_s
363
+ @queue = db.queue(
364
+ "_outbox:#{name}",
365
+ visibility_timeout_s: visibility_timeout_s,
366
+ max_attempts: max_attempts,
367
+ )
368
+ end
369
+
370
+ def enqueue(payload, tx: nil, delay: nil, run_at: nil, priority: 0, expires: nil)
371
+ if tx
372
+ @queue.enqueue_tx(tx, payload, delay: delay, run_at: run_at, priority: priority, expires: expires)
373
+ else
374
+ @queue.enqueue(payload, delay: delay, run_at: run_at, priority: priority, expires: expires)
375
+ end
376
+ end
377
+
378
+ def run_once(worker_id)
379
+ job = @queue.claim_one(worker_id)
380
+ return false unless job
381
+
382
+ begin
383
+ if @delivery.arity == 1
384
+ @delivery.call(job.payload)
385
+ else
386
+ @delivery.call(job.payload, job)
387
+ end
388
+ raise "outbox ack failed for job #{job.id}" unless job.ack
389
+ rescue StandardError => e
390
+ delay_s = retry_delay(job.attempts)
391
+ raise "outbox retry failed for job #{job.id}" unless job.retry(delay_s: delay_s, error: "#{e}\n#{e.backtrace&.join("\n")}")
392
+ end
393
+ true
394
+ end
395
+
396
+ def run_worker(worker_id, stop: nil, idle_sleep_s: 0.1)
397
+ until stop&.call
398
+ processed = run_once(worker_id)
399
+ sleep(idle_sleep_s) unless processed
400
+ end
401
+ end
402
+
403
+ private
404
+
405
+ def retry_delay(attempts)
406
+ return 0 if @base_backoff_s <= 0
407
+
408
+ (@base_backoff_s * (2**[attempts - 1, 0].max)).ceil
409
+ end
410
+ end
411
+
277
412
  # A claimed unit of work. `payload` is the decoded JSON value (Hash,
278
413
  # Array, etc.). `id`, `worker_id`, and `attempts` are metadata from
279
414
  # the claim result.
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: honker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Russell Romney
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2026-05-04 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: sqlite3
@@ -15,23 +16,33 @@ dependencies:
15
16
  requirements:
16
17
  - - ">="
17
18
  - !ruby/object:Gem::Version
18
- version: '1.7'
19
+ version: 2.0.4
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
19
23
  type: :runtime
20
24
  prerelease: false
21
25
  version_requirements: !ruby/object:Gem::Requirement
22
26
  requirements:
23
27
  - - ">="
24
28
  - !ruby/object:Gem::Version
25
- version: '1.7'
29
+ version: 2.0.4
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
26
33
  description: |-
27
34
  Ruby binding for Honker — a SQLite-native task runtime. Queues,
28
- streams, pub/sub, cron scheduler, results, locks, rate limits, all
35
+ streams, pub/sub, time-trigger scheduler, results, locks, rate limits, all
29
36
  in one .db file. Thin wrapper around the Honker SQLite loadable
30
37
  extension; no Redis, no external broker.
38
+ email:
31
39
  executables: []
32
40
  extensions: []
33
41
  extra_rdoc_files: []
34
42
  files:
43
+ - LICENSE
44
+ - LICENSE-APACHE
45
+ - LICENSE-MIT
35
46
  - README.md
36
47
  - honker.gemspec
37
48
  - lib/honker.rb
@@ -42,11 +53,13 @@ files:
42
53
  - lib/honker/version.rb
43
54
  homepage: https://honker.dev
44
55
  licenses:
56
+ - MIT
45
57
  - Apache-2.0
46
58
  metadata:
47
59
  homepage_uri: https://honker.dev
48
60
  source_code_uri: https://github.com/russellromney/honker
49
61
  documentation_uri: https://honker.dev/
62
+ post_install_message:
50
63
  rdoc_options: []
51
64
  require_paths:
52
65
  - lib
@@ -61,7 +74,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
74
  - !ruby/object:Gem::Version
62
75
  version: '0'
63
76
  requirements: []
64
- rubygems_version: 4.0.10
77
+ rubygems_version: 3.0.3.1
78
+ signing_key:
65
79
  specification_version: 4
66
80
  summary: Durable queues, streams, pub/sub, and scheduler on SQLite.
67
81
  test_files: []