mankai-jobs-sql 0.1.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.
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Mankai
6
+ module Jobs
7
+ module SQL
8
+ # Recurring tasks added at runtime, without a deploy. Schedulers with
9
+ # `dynamic_recurring_tasks: true` read them on every reload.
10
+ #
11
+ # @api private
12
+ # @since 0.1.0
13
+ class RecurringTasks
14
+ # @api private
15
+ # @since 0.1.0
16
+ def initialize(database:)
17
+ @database = database
18
+ end
19
+
20
+ # @param task [Recurring::Task] a validated task (see Recurring.build)
21
+ # @raise [ConfigurationError] if a task with the same key exists
22
+ #
23
+ # @api private
24
+ # @since 0.1.0
25
+ def add(task)
26
+ current = @database.now
27
+ table.insert(
28
+ key: task.key, job: task.job, schedule: task.schedule, args: JSON.generate(Serializer.dump(task.args)),
29
+ queue: task.queue, priority: task.priority, description: task.description,
30
+ created_at: current, updated_at: current
31
+ )
32
+ true
33
+ rescue Sequel::UniqueConstraintViolation
34
+ raise ConfigurationError, "a recurring task #{task.key.inspect} is already scheduled"
35
+ end
36
+
37
+ # @return [Boolean] whether a task was removed
38
+ #
39
+ # @api private
40
+ # @since 0.1.0
41
+ def remove(key)
42
+ table.where(key: key.to_s).delete.positive?
43
+ end
44
+
45
+ # @return [Hash{String => Hash}] the stored definitions, for Recurring.build
46
+ #
47
+ # @api private
48
+ # @since 0.1.0
49
+ def definitions
50
+ table.order(:key).all.to_h do |row|
51
+ [row[:key], {job: row[:job], schedule: row[:schedule], args: JSON.parse(row[:args]),
52
+ queue: row[:queue], priority: row[:priority], description: row[:description]}.compact]
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ # @api private
59
+ # @since 0.1.0
60
+ def table
61
+ @database.table(:recurring_tasks)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mankai
4
+ module Jobs
5
+ module SQL
6
+ # Concurrency slots: one row per concurrency key, whose value counts free slots.
7
+ #
8
+ # Every slot expires after the job's concurrency duration, so a lost release (a crashed
9
+ # process) cannot block a key forever; the dispatcher deletes expired rows.
10
+ #
11
+ # @api private
12
+ # @since 0.1.0
13
+ class Semaphores
14
+ # @api private
15
+ # @since 0.1.0
16
+ def initialize(db:, table:, clock:)
17
+ @db = db
18
+ @table = table
19
+ @clock = clock
20
+ end
21
+
22
+ # Takes a slot. Call inside a transaction.
23
+ #
24
+ # @return [Integer, nil] the semaphore id the slot was taken from, or nil if none is free
25
+ #
26
+ # @api private
27
+ # @since 0.1.0
28
+ def acquire(key:, limit:, duration:)
29
+ now = @clock.call
30
+ expires_at = now + duration
31
+
32
+ unless (row = dataset.where(key: key).for_update.first)
33
+ create(key: key, value: limit, expires_at: expires_at, created_at: now, updated_at: now)
34
+ row = dataset.where(key: key).for_update.first
35
+ end
36
+
37
+ row && decrement(row[:id], expires_at, now) ? row[:id] : nil
38
+ end
39
+
40
+ # Gives a slot back to the semaphore it was taken from. A no-op if that row expired and was
41
+ # deleted: a semaphore re-created for the same key belongs to other jobs.
42
+ #
43
+ # @api private
44
+ # @since 0.1.0
45
+ def release(id:, limit:, duration:)
46
+ now = @clock.call
47
+ dataset.where(id: id).where(Sequel[:value] < limit)
48
+ .update(value: Sequel[:value] + 1, expires_at: now + duration, updated_at: now)
49
+ end
50
+
51
+ # @return [Integer] how many expired semaphores were deleted
52
+ #
53
+ # @api private
54
+ # @since 0.1.0
55
+ def delete_expired(batch_size: 500)
56
+ now = @clock.call
57
+ total = 0
58
+ loop do
59
+ ids = dataset.where(Sequel[:expires_at] < now).limit(batch_size).select_map(:id)
60
+ break if ids.empty?
61
+
62
+ total += dataset.where(id: ids).delete
63
+ end
64
+ total
65
+ end
66
+
67
+ private
68
+
69
+ # @api private
70
+ # @since 0.1.0
71
+ def dataset
72
+ @db[@table]
73
+ end
74
+
75
+ # @api private
76
+ # @since 0.1.0
77
+ def decrement(id, expires_at, now)
78
+ dataset.where(id: id).where(Sequel[:value] > 0)
79
+ .update(value: Sequel[:value] - 1, expires_at: expires_at, updated_at: now)
80
+ .positive?
81
+ end
82
+
83
+ # Creates the key's row unless another transaction already has, without failing.
84
+ #
85
+ # An atomic "insert if absent" takes the row's exclusive lock directly. A plain insert that
86
+ # hits the duplicate would leave MySQL/MariaDB holding a shared lock on the row, and two
87
+ # transactions upgrading those shared locks to FOR UPDATE deadlock each other.
88
+ #
89
+ # @api private
90
+ # @since 0.1.0
91
+ def create(row)
92
+ case @db.database_type
93
+ when :mysql # and MariaDB
94
+ dataset.on_duplicate_key_update(id: Sequel[:id]).insert(row)
95
+ when :postgres
96
+ dataset.insert_conflict(target: :key).insert(row)
97
+ when :sqlite
98
+ dataset.insert_conflict(:ignore).insert(row)
99
+ else
100
+ # A savepoint keeps a unique violation from aborting the caller's transaction.
101
+ begin
102
+ @db.transaction(savepoint: true) { dataset.insert(row) }
103
+ rescue Sequel::UniqueConstraintViolation
104
+ nil
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end