kaal 0.3.0 → 0.4.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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -11
  3. data/lib/kaal/active_record_support.rb +82 -0
  4. data/lib/kaal/backend/mysql.rb +41 -0
  5. data/lib/kaal/backend/postgres.rb +41 -0
  6. data/lib/kaal/backend/sqlite.rb +41 -0
  7. data/lib/kaal/definition/database_engine.rb +88 -0
  8. data/lib/kaal/dispatch/database_engine.rb +120 -0
  9. data/lib/kaal/internal/active_record/base_record.rb +16 -0
  10. data/lib/kaal/internal/active_record/connection_support.rb +96 -0
  11. data/lib/kaal/internal/active_record/database_backend.rb +73 -0
  12. data/lib/kaal/internal/active_record/definition_record.rb +16 -0
  13. data/lib/kaal/internal/active_record/definition_registry.rb +81 -0
  14. data/lib/kaal/internal/active_record/dispatch_record.rb +16 -0
  15. data/lib/kaal/internal/active_record/dispatch_registry.rb +100 -0
  16. data/lib/kaal/internal/active_record/lock_record.rb +16 -0
  17. data/lib/kaal/internal/active_record/migration_templates.rb +108 -0
  18. data/lib/kaal/internal/active_record/mysql_backend.rb +71 -0
  19. data/lib/kaal/internal/active_record/postgres_backend.rb +69 -0
  20. data/lib/kaal/internal/active_record.rb +17 -0
  21. data/lib/kaal/internal/sequel/database_backend.rb +74 -0
  22. data/lib/kaal/internal/sequel/mysql_backend.rb +69 -0
  23. data/lib/kaal/internal/sequel/postgres_backend.rb +67 -0
  24. data/lib/kaal/internal/sequel.rb +12 -0
  25. data/lib/kaal/persistence/database.rb +35 -0
  26. data/lib/kaal/persistence/migration_templates.rb +97 -0
  27. data/lib/kaal/registry.rb +0 -2
  28. data/lib/kaal/sequel_support.rb +82 -0
  29. data/lib/kaal/version.rb +1 -1
  30. data/lib/kaal.rb +6 -0
  31. metadata +26 -1
@@ -0,0 +1,12 @@
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/internal/sequel/database_backend'
8
+ require 'kaal/internal/sequel/postgres_backend'
9
+ require 'kaal/internal/sequel/mysql_backend'
10
+ require 'kaal/definition/database_engine'
11
+ require 'kaal/dispatch/database_engine'
12
+ require 'kaal/persistence/database'
@@ -0,0 +1,35 @@
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
+ # Thin wrapper around a Sequel connection to keep table access consistent.
10
+ class Database
11
+ attr_reader :connection
12
+
13
+ def initialize(connection)
14
+ Kaal::Sequel.require_sequel!
15
+ @connection = if connection.is_a?(::Sequel::Database)
16
+ connection
17
+ else
18
+ ::Sequel.connect(connection)
19
+ end
20
+ end
21
+
22
+ def definitions_dataset
23
+ connection[:kaal_definitions]
24
+ end
25
+
26
+ def dispatches_dataset
27
+ connection[:kaal_dispatches]
28
+ end
29
+
30
+ def locks_dataset
31
+ connection[:kaal_locks]
32
+ end
33
+ end
34
+ end
35
+ 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
data/lib/kaal/registry.rb CHANGED
@@ -20,9 +20,7 @@ module Kaal
20
20
 
21
21
  ##
22
22
  # Entry class representing a single registered cron job
23
- # rubocop:disable Style/RedundantStructKeywordInit
24
23
  Entry = Struct.new(:key, :cron, :enqueue, keyword_init: true)
25
- # rubocop:enable Style/RedundantStructKeywordInit
26
24
 
27
25
  ##
28
26
  # Initialize a new Registry instance.
@@ -0,0 +1,82 @@
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 'fileutils'
8
+
9
+ module Kaal
10
+ # Sequel migration/install support for SQL-backed Kaal backends.
11
+ module Sequel
12
+ module_function
13
+
14
+ def install_postgres_migration(target_dir:, migration_name: 'create_kaal_postgres_backend')
15
+ install_migrations(target_dir:, backend: 'postgres', migration_name:)
16
+ end
17
+
18
+ def install_mysql_migration(target_dir:, migration_name: 'create_kaal_mysql_backend')
19
+ install_migrations(target_dir:, backend: 'mysql', migration_name:)
20
+ end
21
+
22
+ def install_sqlite_migration(target_dir:, migration_name: 'create_kaal_sqlite_backend')
23
+ install_migrations(target_dir:, backend: 'sqlite', migration_name:)
24
+ end
25
+
26
+ def install_migrations(target_dir:, backend:, migration_name: nil)
27
+ require_sequel!
28
+
29
+ normalized_name = normalize_migration_name(migration_name, fallback: default_migration_name_for(backend))
30
+ base_path = File.expand_path(target_dir)
31
+ FileUtils.mkdir_p(base_path)
32
+
33
+ Kaal::Persistence::MigrationTemplates.for_backend(backend).map.with_index do |(_name, contents), index|
34
+ suffix = migration_suffixes_for(backend).fetch(index)
35
+ path = File.expand_path("#{timestamp(index)}_#{normalized_name}_#{suffix}.rb", base_path)
36
+ File.write(path, contents)
37
+ path
38
+ end
39
+ end
40
+
41
+ def require_sequel!
42
+ require 'sequel'
43
+ rescue LoadError => e
44
+ raise LoadError,
45
+ "#{e.message}. Add `gem 'sequel'` to your Gemfile to use Sequel-backed Kaal SQL support.",
46
+ cause: e
47
+ end
48
+
49
+ def normalize_migration_name(name, fallback:)
50
+ normalized = name.to_s.each_char.with_object(+'') do |char, buffer|
51
+ if letter?(char) || digit?(char)
52
+ buffer << char.downcase
53
+ elsif !buffer.empty? && !buffer.end_with?('_')
54
+ buffer << '_'
55
+ end
56
+ end.delete_suffix('_')
57
+ normalized.empty? ? fallback : normalized
58
+ end
59
+
60
+ def default_migration_name_for(backend)
61
+ "create_kaal_#{backend}_backend"
62
+ end
63
+
64
+ def migration_suffixes_for(backend)
65
+ return %w[dispatches locks definitions] if backend.to_s == 'sqlite'
66
+
67
+ %w[dispatches definitions]
68
+ end
69
+
70
+ def timestamp(offset = 0)
71
+ (Time.now.utc + offset).strftime('%Y%m%d%H%M%S')
72
+ end
73
+
74
+ def letter?(char)
75
+ char.between?('a', 'z') || char.between?('A', 'Z')
76
+ end
77
+
78
+ def digit?(char)
79
+ char.between?('0', '9')
80
+ end
81
+ end
82
+ end
data/lib/kaal/version.rb CHANGED
@@ -5,5 +5,5 @@
5
5
  # This source code is licensed under the MIT license found in the
6
6
  # LICENSE file in the root directory of this source tree.
7
7
  module Kaal
8
- VERSION = '0.3.0'
8
+ VERSION = '0.4.0'
9
9
  end
data/lib/kaal.rb CHANGED
@@ -16,8 +16,14 @@ require 'kaal/definition/redis_engine'
16
16
  require 'kaal/backend/adapter'
17
17
  require 'kaal/backend/memory_adapter'
18
18
  require 'kaal/backend/redis_adapter'
19
+ require 'kaal/backend/sqlite'
20
+ require 'kaal/backend/postgres'
21
+ require 'kaal/backend/mysql'
19
22
  require 'kaal/backend/dispatch_registry_accessor'
20
23
  require 'kaal/backend/dispatch_attempt_logger'
24
+ require 'kaal/persistence/migration_templates'
25
+ require 'kaal/sequel_support'
26
+ require 'kaal/active_record_support'
21
27
  require 'kaal/utils'
22
28
  require 'kaal/register_conflict_support'
23
29
  require 'kaal/definitions/registry_accessor'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nitesh Purohit
@@ -84,12 +84,16 @@ files:
84
84
  - config/scheduler.yml
85
85
  - exe/kaal
86
86
  - lib/kaal.rb
87
+ - lib/kaal/active_record_support.rb
87
88
  - lib/kaal/backend/adapter.rb
88
89
  - lib/kaal/backend/dispatch_attempt_logger.rb
89
90
  - lib/kaal/backend/dispatch_logging.rb
90
91
  - lib/kaal/backend/dispatch_registry_accessor.rb
91
92
  - lib/kaal/backend/memory_adapter.rb
93
+ - lib/kaal/backend/mysql.rb
94
+ - lib/kaal/backend/postgres.rb
92
95
  - lib/kaal/backend/redis_adapter.rb
96
+ - lib/kaal/backend/sqlite.rb
93
97
  - lib/kaal/cli.rb
94
98
  - lib/kaal/config.rb
95
99
  - lib/kaal/config/configuration.rb
@@ -99,15 +103,35 @@ files:
99
103
  - lib/kaal/core/coordinator.rb
100
104
  - lib/kaal/core/enabled_entry_enumerator.rb
101
105
  - lib/kaal/core/occurrence_finder.rb
106
+ - lib/kaal/definition/database_engine.rb
102
107
  - lib/kaal/definition/memory_engine.rb
103
108
  - lib/kaal/definition/persistence_helpers.rb
104
109
  - lib/kaal/definition/redis_engine.rb
105
110
  - lib/kaal/definition/registry.rb
106
111
  - lib/kaal/definitions/registration_service.rb
107
112
  - lib/kaal/definitions/registry_accessor.rb
113
+ - lib/kaal/dispatch/database_engine.rb
108
114
  - lib/kaal/dispatch/memory_engine.rb
109
115
  - lib/kaal/dispatch/redis_engine.rb
110
116
  - lib/kaal/dispatch/registry.rb
117
+ - lib/kaal/internal/active_record.rb
118
+ - lib/kaal/internal/active_record/base_record.rb
119
+ - lib/kaal/internal/active_record/connection_support.rb
120
+ - lib/kaal/internal/active_record/database_backend.rb
121
+ - lib/kaal/internal/active_record/definition_record.rb
122
+ - lib/kaal/internal/active_record/definition_registry.rb
123
+ - lib/kaal/internal/active_record/dispatch_record.rb
124
+ - lib/kaal/internal/active_record/dispatch_registry.rb
125
+ - lib/kaal/internal/active_record/lock_record.rb
126
+ - lib/kaal/internal/active_record/migration_templates.rb
127
+ - lib/kaal/internal/active_record/mysql_backend.rb
128
+ - lib/kaal/internal/active_record/postgres_backend.rb
129
+ - lib/kaal/internal/sequel.rb
130
+ - lib/kaal/internal/sequel/database_backend.rb
131
+ - lib/kaal/internal/sequel/mysql_backend.rb
132
+ - lib/kaal/internal/sequel/postgres_backend.rb
133
+ - lib/kaal/persistence/database.rb
134
+ - lib/kaal/persistence/migration_templates.rb
111
135
  - lib/kaal/register_conflict_support.rb
112
136
  - lib/kaal/registry.rb
113
137
  - lib/kaal/runtime.rb
@@ -123,6 +147,7 @@ files:
123
147
  - lib/kaal/scheduler_file/loader.rb
124
148
  - lib/kaal/scheduler_file/payload_loader.rb
125
149
  - lib/kaal/scheduler_file/placeholder_support.rb
150
+ - lib/kaal/sequel_support.rb
126
151
  - lib/kaal/support/hash_tools.rb
127
152
  - lib/kaal/utils.rb
128
153
  - lib/kaal/utils/cron_humanizer.rb