kaal-sequel 0.3.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/LICENSE +21 -0
- data/README.md +69 -0
- data/Rakefile +16 -0
- data/lib/kaal/backend/database_adapter.rb +77 -0
- data/lib/kaal/backend/mysql_adapter.rb +69 -0
- data/lib/kaal/backend/postgres_adapter.rb +67 -0
- data/lib/kaal/backend/sqlite_adapter.rb +7 -0
- data/lib/kaal/definition/database_engine.rb +88 -0
- data/lib/kaal/dispatch/database_engine.rb +120 -0
- data/lib/kaal/persistence/database.rb +36 -0
- data/lib/kaal/persistence/migration_templates.rb +97 -0
- data/lib/kaal/sequel/version.rb +11 -0
- data/lib/kaal/sequel.rb +25 -0
- metadata +94 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 20b093d4e62a2bdb94bd8131189c55ecb4510d51cd2bbbf71ba2ba5befd998be
|
|
4
|
+
data.tar.gz: ae2ec7986d15202efc9d5ebb516642fb58a7b74bcde545221ed498afc076ce90
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ec6475077d6c68a411832bade23b8fcc00d303b00cf6225da77dbe9d0f46fda50a4307271c1a4faf930a01eb51d9c794176216e82f23874efb795f3cde349ba1
|
|
7
|
+
data.tar.gz: 53cc4924945510e13288a7e9f6cac8324466d78dbf5d56721a07bdd48c95ee068504465209a0bfcc91c29ce86bfadefc70047df98700f3dfc2626b712dcee466
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Codevedas Inc. and the Kaal Authors
|
|
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,69 @@
|
|
|
1
|
+
# Kaal::Sequel
|
|
2
|
+
|
|
3
|
+
Sequel-backed datastore adapter for Kaal.
|
|
4
|
+
|
|
5
|
+
`kaal-sequel` depends on `kaal` and owns the Sequel persistence layer:
|
|
6
|
+
|
|
7
|
+
- Sequel-backed definition registry
|
|
8
|
+
- Sequel-backed dispatch registry
|
|
9
|
+
- SQLite table-lock adapter
|
|
10
|
+
- PostgreSQL advisory-lock adapter
|
|
11
|
+
- MySQL named-lock adapter
|
|
12
|
+
- Sequel migration templates for Kaal tables
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
gem 'kaal'
|
|
18
|
+
gem 'kaal-sequel'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
SQLite:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
require 'kaal'
|
|
27
|
+
require 'kaal/sequel'
|
|
28
|
+
require 'sequel'
|
|
29
|
+
|
|
30
|
+
database = Sequel.connect(adapter: 'sqlite', database: 'db/kaal.sqlite3')
|
|
31
|
+
|
|
32
|
+
Kaal.configure do |config|
|
|
33
|
+
config.backend = Kaal::Backend::DatabaseAdapter.new(database)
|
|
34
|
+
config.scheduler_config_path = 'config/scheduler.yml'
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
PostgreSQL:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
config.backend = Kaal::Backend::PostgresAdapter.new(database)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
MySQL:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
config.backend = Kaal::Backend::MySQLAdapter.new(database)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
This is the plain Ruby SQL path when you want Sequel to own the tables, registries, and lock adapters without Rails.
|
|
51
|
+
|
|
52
|
+
## Tables
|
|
53
|
+
|
|
54
|
+
The Sequel adapter persists against:
|
|
55
|
+
|
|
56
|
+
- `kaal_definitions`
|
|
57
|
+
- `kaal_dispatches`
|
|
58
|
+
- `kaal_locks`
|
|
59
|
+
|
|
60
|
+
`kaal_locks` is used for SQLite. PostgreSQL and MySQL rely on advisory or named locks and persist definitions and dispatches without a dedicated locks table.
|
|
61
|
+
|
|
62
|
+
## Development
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
bin/rspec-unit
|
|
66
|
+
bin/rspec-e2e sqlite
|
|
67
|
+
bin/rubocop
|
|
68
|
+
bin/reek
|
|
69
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
require 'bundler/gem_tasks'
|
|
8
|
+
require 'rspec/core/rake_task'
|
|
9
|
+
|
|
10
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
11
|
+
|
|
12
|
+
require 'rubocop/rake_task'
|
|
13
|
+
|
|
14
|
+
RuboCop::RakeTask.new
|
|
15
|
+
|
|
16
|
+
task default: %i[spec rubocop]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
require 'sequel'
|
|
8
|
+
require 'kaal/backend/dispatch_logging'
|
|
9
|
+
require_relative '../definition/database_engine'
|
|
10
|
+
require_relative '../dispatch/database_engine'
|
|
11
|
+
require 'kaal/persistence/database'
|
|
12
|
+
|
|
13
|
+
module Kaal
|
|
14
|
+
module Backend
|
|
15
|
+
# Sequel-backed SQL adapter that coordinates locks through the kaal_locks table.
|
|
16
|
+
class DatabaseAdapter < Adapter
|
|
17
|
+
include DispatchLogging
|
|
18
|
+
|
|
19
|
+
def initialize(database, namespace: nil)
|
|
20
|
+
super()
|
|
21
|
+
@database = Kaal::Persistence::Database.new(database)
|
|
22
|
+
@namespace = namespace
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def dispatch_registry
|
|
26
|
+
@dispatch_registry ||= Kaal::Dispatch::DatabaseEngine.new(database: @database.connection, namespace: resolved_namespace)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def definition_registry
|
|
30
|
+
@definition_registry ||= Kaal::Definition::DatabaseEngine.new(database: @database.connection)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def acquire(key, ttl)
|
|
34
|
+
now = Time.now.utc
|
|
35
|
+
expires_at = now + ttl
|
|
36
|
+
|
|
37
|
+
2.times do |attempt|
|
|
38
|
+
cleanup_expired_locks if attempt.positive?
|
|
39
|
+
|
|
40
|
+
begin
|
|
41
|
+
dataset.insert(key: key, acquired_at: now, expires_at: expires_at)
|
|
42
|
+
log_dispatch_attempt(key)
|
|
43
|
+
return true
|
|
44
|
+
rescue ::Sequel::UniqueConstraintViolation
|
|
45
|
+
next
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
false
|
|
50
|
+
rescue StandardError => e
|
|
51
|
+
raise LockAdapterError, "Database acquire failed for #{key}: #{e.message}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def release(key)
|
|
55
|
+
dataset.where(key: key).delete.positive?
|
|
56
|
+
rescue StandardError => e
|
|
57
|
+
raise LockAdapterError, "Database release failed for #{key}: #{e.message}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def cleanup_expired_locks
|
|
61
|
+
dataset.where { expires_at < Time.now.utc }.delete
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def dataset
|
|
67
|
+
@database.locks_dataset
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def resolved_namespace
|
|
71
|
+
@namespace || Kaal.configuration.namespace
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
SQLiteAdapter = DatabaseAdapter
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
require 'digest'
|
|
8
|
+
require 'kaal/backend/dispatch_logging'
|
|
9
|
+
require_relative '../definition/database_engine'
|
|
10
|
+
require_relative '../dispatch/database_engine'
|
|
11
|
+
require 'kaal/persistence/database'
|
|
12
|
+
|
|
13
|
+
module Kaal
|
|
14
|
+
module Backend
|
|
15
|
+
# MySQL named-lock adapter backed by Sequel.
|
|
16
|
+
class MySQLAdapter < Adapter
|
|
17
|
+
include DispatchLogging
|
|
18
|
+
|
|
19
|
+
MAX_LOCK_NAME_LENGTH = 64
|
|
20
|
+
|
|
21
|
+
def initialize(database, namespace: nil)
|
|
22
|
+
super()
|
|
23
|
+
@database = Kaal::Persistence::Database.new(database)
|
|
24
|
+
@namespace = namespace
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def dispatch_registry
|
|
28
|
+
@dispatch_registry ||= Kaal::Dispatch::DatabaseEngine.new(database: @database.connection, namespace: resolved_namespace)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def definition_registry
|
|
32
|
+
@definition_registry ||= Kaal::Definition::DatabaseEngine.new(database: @database.connection)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def acquire(key, _ttl)
|
|
36
|
+
acquired = scalar('SELECT GET_LOCK(?, 0) AS lock_result', self.class.normalize_lock_name(key)) == 1
|
|
37
|
+
log_dispatch_attempt(key) if acquired
|
|
38
|
+
acquired
|
|
39
|
+
rescue StandardError => e
|
|
40
|
+
raise LockAdapterError, "MySQL acquire failed for #{key}: #{e.message}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def release(key)
|
|
44
|
+
scalar('SELECT RELEASE_LOCK(?) AS lock_result', self.class.normalize_lock_name(key)) == 1
|
|
45
|
+
rescue StandardError => e
|
|
46
|
+
raise LockAdapterError, "MySQL release failed for #{key}: #{e.message}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.normalize_lock_name(key)
|
|
50
|
+
return key if key.length <= MAX_LOCK_NAME_LENGTH
|
|
51
|
+
|
|
52
|
+
digest = Digest::SHA256.hexdigest(key)
|
|
53
|
+
prefix_length = MAX_LOCK_NAME_LENGTH - 17
|
|
54
|
+
"#{key[0...prefix_length]}:#{digest[0...16]}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def scalar(sql, *binds)
|
|
60
|
+
row = @database.connection.fetch(sql, *binds).first
|
|
61
|
+
row.values.first
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def resolved_namespace
|
|
65
|
+
@namespace || Kaal.configuration.namespace
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
require 'digest'
|
|
8
|
+
require 'kaal/backend/dispatch_logging'
|
|
9
|
+
require_relative '../definition/database_engine'
|
|
10
|
+
require_relative '../dispatch/database_engine'
|
|
11
|
+
require 'kaal/persistence/database'
|
|
12
|
+
|
|
13
|
+
module Kaal
|
|
14
|
+
module Backend
|
|
15
|
+
# PostgreSQL advisory-lock adapter backed by Sequel.
|
|
16
|
+
class PostgresAdapter < Adapter
|
|
17
|
+
include DispatchLogging
|
|
18
|
+
|
|
19
|
+
SIGNED_64_MAX = 9_223_372_036_854_775_807
|
|
20
|
+
UNSIGNED_64_RANGE = 18_446_744_073_709_551_616
|
|
21
|
+
|
|
22
|
+
def initialize(database, namespace: nil)
|
|
23
|
+
super()
|
|
24
|
+
@database = Kaal::Persistence::Database.new(database)
|
|
25
|
+
@namespace = namespace
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def dispatch_registry
|
|
29
|
+
@dispatch_registry ||= Kaal::Dispatch::DatabaseEngine.new(database: @database.connection, namespace: resolved_namespace)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def definition_registry
|
|
33
|
+
@definition_registry ||= Kaal::Definition::DatabaseEngine.new(database: @database.connection)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def acquire(key, _ttl)
|
|
37
|
+
acquired = scalar('SELECT pg_try_advisory_lock(?) AS acquired', self.class.calculate_lock_id(key)) == true
|
|
38
|
+
log_dispatch_attempt(key) if acquired
|
|
39
|
+
acquired
|
|
40
|
+
rescue StandardError => e
|
|
41
|
+
raise LockAdapterError, "PostgreSQL acquire failed for #{key}: #{e.message}"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def release(key)
|
|
45
|
+
scalar('SELECT pg_advisory_unlock(?) AS released', self.class.calculate_lock_id(key)) == true
|
|
46
|
+
rescue StandardError => e
|
|
47
|
+
raise LockAdapterError, "PostgreSQL release failed for #{key}: #{e.message}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.calculate_lock_id(key)
|
|
51
|
+
hash = Digest::MD5.digest(key).unpack1('Q>')
|
|
52
|
+
hash > SIGNED_64_MAX ? hash - UNSIGNED_64_RANGE : hash
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def scalar(sql, *binds)
|
|
58
|
+
row = @database.connection.fetch(sql, *binds).first
|
|
59
|
+
row.values.first
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def resolved_namespace
|
|
63
|
+
@namespace || Kaal.configuration.namespace
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
require 'json'
|
|
8
|
+
require 'kaal/definition/registry'
|
|
9
|
+
require 'kaal/definition/persistence_helpers'
|
|
10
|
+
require 'kaal/persistence/database'
|
|
11
|
+
|
|
12
|
+
module Kaal
|
|
13
|
+
module Definition
|
|
14
|
+
# Sequel-backed definition registry persisted in kaal_definitions.
|
|
15
|
+
class DatabaseEngine < Registry
|
|
16
|
+
def initialize(database:)
|
|
17
|
+
super()
|
|
18
|
+
@database = Kaal::Persistence::Database.new(database)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def upsert_definition(key:, cron:, enabled: true, source: 'code', metadata: {})
|
|
22
|
+
rows = dataset.where(key: key)
|
|
23
|
+
existing = rows.first
|
|
24
|
+
now = Time.now.utc
|
|
25
|
+
payload = {
|
|
26
|
+
key: key,
|
|
27
|
+
cron: cron,
|
|
28
|
+
enabled: enabled,
|
|
29
|
+
source: source,
|
|
30
|
+
metadata: JSON.generate(metadata || {}),
|
|
31
|
+
created_at: existing ? existing[:created_at] : now,
|
|
32
|
+
updated_at: now,
|
|
33
|
+
disabled_at: PersistenceHelpers.disabled_at_for(existing, enabled, now)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if existing
|
|
37
|
+
rows.update(payload)
|
|
38
|
+
else
|
|
39
|
+
dataset.insert(payload)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
find_definition(key)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def remove_definition(key)
|
|
46
|
+
rows = dataset.where(key: key)
|
|
47
|
+
row = rows.first
|
|
48
|
+
return nil unless row
|
|
49
|
+
|
|
50
|
+
rows.delete
|
|
51
|
+
self.class.normalize_row(row)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def find_definition(key)
|
|
55
|
+
self.class.normalize_row(dataset.where(key: key).first)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def all_definitions
|
|
59
|
+
dataset.order(:key).all.map { |row| self.class.normalize_row(row) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def enabled_definitions
|
|
63
|
+
dataset.where(enabled: true).order(:key).all.map { |row| self.class.normalize_row(row) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.normalize_row(row)
|
|
67
|
+
return nil unless row
|
|
68
|
+
|
|
69
|
+
{
|
|
70
|
+
key: row[:key],
|
|
71
|
+
cron: row[:cron],
|
|
72
|
+
enabled: row[:enabled] ? true : false,
|
|
73
|
+
source: row[:source],
|
|
74
|
+
metadata: PersistenceHelpers.parse_metadata(row[:metadata]),
|
|
75
|
+
created_at: row[:created_at],
|
|
76
|
+
updated_at: row[:updated_at],
|
|
77
|
+
disabled_at: row[:disabled_at]
|
|
78
|
+
}
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def dataset
|
|
84
|
+
@database.definitions_dataset
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
require 'kaal/dispatch/registry'
|
|
8
|
+
require 'kaal/persistence/database'
|
|
9
|
+
|
|
10
|
+
module Kaal
|
|
11
|
+
module Dispatch
|
|
12
|
+
# Sequel-backed dispatch registry stored in kaal_dispatches.
|
|
13
|
+
class DatabaseEngine < Registry
|
|
14
|
+
def initialize(database:, namespace: nil)
|
|
15
|
+
super()
|
|
16
|
+
@database = Kaal::Persistence::Database.new(database)
|
|
17
|
+
@namespace = namespace
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def log_dispatch(key, fire_time, node_id, status = 'dispatched')
|
|
21
|
+
now = Time.now.utc
|
|
22
|
+
storage_key = namespaced_key(key)
|
|
23
|
+
attributes = {
|
|
24
|
+
key: storage_key,
|
|
25
|
+
fire_time: fire_time,
|
|
26
|
+
dispatched_at: now,
|
|
27
|
+
node_id: node_id,
|
|
28
|
+
status: status
|
|
29
|
+
}
|
|
30
|
+
dispatches_dataset = dataset
|
|
31
|
+
update_values = { dispatched_at: now, node_id: node_id, status: status }
|
|
32
|
+
begin
|
|
33
|
+
dispatches_dataset.insert_conflict(
|
|
34
|
+
target: %i[key fire_time],
|
|
35
|
+
update: update_values
|
|
36
|
+
).insert(attributes)
|
|
37
|
+
rescue NoMethodError => e
|
|
38
|
+
raise unless e.name == :insert_conflict
|
|
39
|
+
|
|
40
|
+
begin
|
|
41
|
+
dispatches_dataset.insert(attributes)
|
|
42
|
+
rescue ::Sequel::UniqueConstraintViolation
|
|
43
|
+
dispatches_dataset.where(key: storage_key, fire_time: fire_time).update(update_values)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
find_dispatch(key, fire_time)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def find_dispatch(key, fire_time)
|
|
51
|
+
self.class.normalize_row(dataset.where(key: namespaced_key(key), fire_time: fire_time).first, namespace: @namespace)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def find_by_key(key)
|
|
55
|
+
query(key: namespaced_key(key))
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def find_by_node(node_id)
|
|
59
|
+
query(node_id: node_id)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def find_by_status(status)
|
|
63
|
+
query(status: status)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def cleanup(recovery_window: 86_400)
|
|
67
|
+
cutoff_time = Time.now.utc - recovery_window
|
|
68
|
+
cleanup_dataset.where { fire_time < cutoff_time }.delete
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.normalize_row(row, namespace: nil)
|
|
72
|
+
return nil unless row
|
|
73
|
+
|
|
74
|
+
{
|
|
75
|
+
key: strip_namespace(row[:key], namespace:),
|
|
76
|
+
fire_time: row[:fire_time],
|
|
77
|
+
dispatched_at: row[:dispatched_at],
|
|
78
|
+
node_id: row[:node_id],
|
|
79
|
+
status: row[:status]
|
|
80
|
+
}
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def self.strip_namespace(key, namespace:)
|
|
84
|
+
return key if namespace.to_s.empty?
|
|
85
|
+
|
|
86
|
+
prefix = "#{namespace}:"
|
|
87
|
+
key.start_with?(prefix) ? key.delete_prefix(prefix) : key
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def dataset
|
|
93
|
+
@database.dispatches_dataset
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def namespaced_key(key)
|
|
97
|
+
return key if @namespace.to_s.empty?
|
|
98
|
+
|
|
99
|
+
"#{@namespace}:#{key}"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def query(filters)
|
|
103
|
+
query_dataset(filters).reverse_order(:fire_time).all.map { |row| self.class.normalize_row(row, namespace: @namespace) }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def query_dataset(filters)
|
|
107
|
+
relation = dataset.where(filters)
|
|
108
|
+
return relation if @namespace.to_s.empty? || filters.key?(:key)
|
|
109
|
+
|
|
110
|
+
relation.where(::Sequel.lit('key LIKE ?', "#{@namespace}:%"))
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def cleanup_dataset
|
|
114
|
+
return dataset if @namespace.to_s.empty?
|
|
115
|
+
|
|
116
|
+
dataset.where(::Sequel.lit('key LIKE ?', "#{@namespace}:%"))
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
require 'sequel'
|
|
8
|
+
|
|
9
|
+
module Kaal
|
|
10
|
+
module Persistence
|
|
11
|
+
# Thin wrapper around a Sequel connection to keep table access consistent.
|
|
12
|
+
class Database
|
|
13
|
+
attr_reader :connection
|
|
14
|
+
|
|
15
|
+
def initialize(connection)
|
|
16
|
+
@connection = if connection.is_a?(::Sequel::Database)
|
|
17
|
+
connection
|
|
18
|
+
else
|
|
19
|
+
::Sequel.connect(connection)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def definitions_dataset
|
|
24
|
+
connection[:kaal_definitions]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def dispatches_dataset
|
|
28
|
+
connection[:kaal_dispatches]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def locks_dataset
|
|
32
|
+
connection[:kaal_locks]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
module Kaal
|
|
8
|
+
module Persistence
|
|
9
|
+
# Sequel migration templates emitted by `kaal init`.
|
|
10
|
+
module MigrationTemplates
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def for_backend(backend)
|
|
14
|
+
case backend.to_s
|
|
15
|
+
when 'sqlite'
|
|
16
|
+
{
|
|
17
|
+
'001_create_kaal_dispatches.rb' => dispatches_template,
|
|
18
|
+
'002_create_kaal_locks.rb' => locks_template,
|
|
19
|
+
'003_create_kaal_definitions.rb' => definitions_template
|
|
20
|
+
}
|
|
21
|
+
when 'postgres', 'mysql'
|
|
22
|
+
{
|
|
23
|
+
'001_create_kaal_dispatches.rb' => dispatches_template,
|
|
24
|
+
'002_create_kaal_definitions.rb' => definitions_template
|
|
25
|
+
}
|
|
26
|
+
else
|
|
27
|
+
{}
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def dispatches_template
|
|
32
|
+
<<~RUBY
|
|
33
|
+
Sequel.migration do
|
|
34
|
+
change do
|
|
35
|
+
create_table?(:kaal_dispatches) do
|
|
36
|
+
primary_key :id
|
|
37
|
+
String :key, null: false
|
|
38
|
+
Time :fire_time, null: false
|
|
39
|
+
Time :dispatched_at, null: false
|
|
40
|
+
String :node_id, null: false
|
|
41
|
+
String :status, null: false, default: 'dispatched', size: 50
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
add_index :kaal_dispatches, [:key, :fire_time], unique: true
|
|
45
|
+
add_index :kaal_dispatches, :key
|
|
46
|
+
add_index :kaal_dispatches, :node_id
|
|
47
|
+
add_index :kaal_dispatches, :status
|
|
48
|
+
add_index :kaal_dispatches, :fire_time
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
RUBY
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def locks_template
|
|
55
|
+
<<~RUBY
|
|
56
|
+
Sequel.migration do
|
|
57
|
+
change do
|
|
58
|
+
create_table?(:kaal_locks) do
|
|
59
|
+
primary_key :id
|
|
60
|
+
String :key, null: false
|
|
61
|
+
Time :acquired_at, null: false
|
|
62
|
+
Time :expires_at, null: false
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
add_index :kaal_locks, :key, unique: true
|
|
66
|
+
add_index :kaal_locks, :expires_at
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
RUBY
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def definitions_template
|
|
73
|
+
<<~RUBY
|
|
74
|
+
Sequel.migration do
|
|
75
|
+
change do
|
|
76
|
+
create_table?(:kaal_definitions) do
|
|
77
|
+
primary_key :id
|
|
78
|
+
String :key, null: false
|
|
79
|
+
String :cron, null: false
|
|
80
|
+
TrueClass :enabled, null: false, default: true
|
|
81
|
+
String :source, null: false
|
|
82
|
+
String :metadata, text: true, null: false, default: '{}'
|
|
83
|
+
Time :disabled_at
|
|
84
|
+
Time :created_at, null: false
|
|
85
|
+
Time :updated_at, null: false
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
add_index :kaal_definitions, :key, unique: true
|
|
89
|
+
add_index :kaal_definitions, :enabled
|
|
90
|
+
add_index :kaal_definitions, :source
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
RUBY
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
module Kaal
|
|
8
|
+
module Sequel
|
|
9
|
+
VERSION = '0.3.0'
|
|
10
|
+
end
|
|
11
|
+
end
|
data/lib/kaal/sequel.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright Codevedas Inc. 2025-present
|
|
4
|
+
#
|
|
5
|
+
# This source code is licensed under the MIT license found in the
|
|
6
|
+
# LICENSE file in the root directory of this source tree.
|
|
7
|
+
require 'kaal'
|
|
8
|
+
require 'sequel'
|
|
9
|
+
require_relative 'sequel/version'
|
|
10
|
+
require_relative 'backend/database_adapter'
|
|
11
|
+
require_relative 'backend/postgres_adapter'
|
|
12
|
+
require_relative 'backend/mysql_adapter'
|
|
13
|
+
require_relative 'backend/sqlite_adapter'
|
|
14
|
+
require 'kaal/definition/persistence_helpers'
|
|
15
|
+
require_relative 'definition/database_engine'
|
|
16
|
+
require_relative 'dispatch/database_engine'
|
|
17
|
+
require_relative 'persistence/database'
|
|
18
|
+
require_relative 'persistence/migration_templates'
|
|
19
|
+
|
|
20
|
+
module Kaal
|
|
21
|
+
module Sequel
|
|
22
|
+
# Base error namespace for Sequel-backed Kaal integrations.
|
|
23
|
+
class Error < StandardError; end
|
|
24
|
+
end
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kaal-sequel
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.3.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Nitesh Purohit
|
|
8
|
+
- Codevedas Inc.
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: kaal
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.3.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.3.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: sequel
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '5.0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '5.0'
|
|
41
|
+
description: " Kaal-Sequel provides seamless integration of Kaal with Sequel, allowing
|
|
42
|
+
you to use Sequel models for scheduling and managing cron jobs in a distributed
|
|
43
|
+
environment.\n"
|
|
44
|
+
email:
|
|
45
|
+
- nitesh.purohit.it@gmail.com
|
|
46
|
+
- team@codevedas.com
|
|
47
|
+
executables: []
|
|
48
|
+
extensions: []
|
|
49
|
+
extra_rdoc_files: []
|
|
50
|
+
files:
|
|
51
|
+
- LICENSE
|
|
52
|
+
- README.md
|
|
53
|
+
- Rakefile
|
|
54
|
+
- lib/kaal/backend/database_adapter.rb
|
|
55
|
+
- lib/kaal/backend/mysql_adapter.rb
|
|
56
|
+
- lib/kaal/backend/postgres_adapter.rb
|
|
57
|
+
- lib/kaal/backend/sqlite_adapter.rb
|
|
58
|
+
- lib/kaal/definition/database_engine.rb
|
|
59
|
+
- lib/kaal/dispatch/database_engine.rb
|
|
60
|
+
- lib/kaal/persistence/database.rb
|
|
61
|
+
- lib/kaal/persistence/migration_templates.rb
|
|
62
|
+
- lib/kaal/sequel.rb
|
|
63
|
+
- lib/kaal/sequel/version.rb
|
|
64
|
+
homepage: https://github.com/Code-Vedas/kaal
|
|
65
|
+
licenses:
|
|
66
|
+
- MIT
|
|
67
|
+
metadata:
|
|
68
|
+
bug_tracker_uri: https://github.com/Code-Vedas/kaal/issues
|
|
69
|
+
changelog_uri: https://github.com/Code-Vedas/kaal/blob/main/CHANGELOG.md
|
|
70
|
+
documentation_uri: https://kaal.codevedas.com
|
|
71
|
+
homepage_uri: https://github.com/Code-Vedas/kaal
|
|
72
|
+
source_code_uri: https://github.com/Code-Vedas/kaal.git
|
|
73
|
+
funding_uri: https://github.com/sponsors/Code-Vedas
|
|
74
|
+
support_uri: https://kaal.codevedas.com/support
|
|
75
|
+
rubygems_uri: https://rubygems.org/gems/kaal-sequel
|
|
76
|
+
rubygems_mfa_required: 'true'
|
|
77
|
+
rdoc_options: []
|
|
78
|
+
require_paths:
|
|
79
|
+
- lib
|
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - ">="
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: '3.2'
|
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
requirements: []
|
|
91
|
+
rubygems_version: 3.6.7
|
|
92
|
+
specification_version: 4
|
|
93
|
+
summary: Sequel integration for Kaal, a distributed cron scheduler for Ruby.
|
|
94
|
+
test_files: []
|