async-background 0.1.0 → 0.2.1
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 +4 -4
- data/lib/async/background/runner.rb +28 -7
- data/lib/async/background/version.rb +1 -1
- data/lib/async/background.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 45ddaa826537a0cee25297261dd7e0fdfd1ff23e73aee6dc97c345653765eb3a
|
|
4
|
+
data.tar.gz: e41c05a4e0a8ecfa897f67c1816301bd6884062fa415834f68904fca4ea9d610
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0827be4ae4599f860d8cfd28f74f9f05c624d6b9d4be2cd3104f07317fc0c0c03fbb42ff871b95f247f3a648978dae1101b764d71bce5a09e54cf6ece4649acd'
|
|
7
|
+
data.tar.gz: 3eee9e658de44f335051f54a2cf2cdc1508e4bce407f605908406e6595d45f00eebe78a4cbb7b4caa4df7347ddcaa1a2c4ef65cadd2aee0fdd882dd5ff6c3d97
|
|
@@ -14,10 +14,11 @@ module Async
|
|
|
14
14
|
class Runner
|
|
15
15
|
attr_reader :logger, :semaphore, :heap, :worker_index, :total_workers
|
|
16
16
|
|
|
17
|
-
def initialize(config_path:, job_count: 2, worker_index:, total_workers:)
|
|
18
|
-
@logger = Console.logger
|
|
17
|
+
def initialize(config_path:, job_count: 2, worker_index:, total_workers:, logger: nil)
|
|
18
|
+
@logger = logger || Console.logger
|
|
19
19
|
@worker_index = worker_index
|
|
20
20
|
@total_workers = total_workers
|
|
21
|
+
@running = true
|
|
21
22
|
|
|
22
23
|
logger.info { "Async::Background worker_index=#{worker_index}/#{total_workers}, job_count=#{job_count}" }
|
|
23
24
|
|
|
@@ -34,9 +35,12 @@ module Async
|
|
|
34
35
|
now = monotonic_now
|
|
35
36
|
wait = [entry.next_run_at - now, MIN_SLEEP_TIME].max
|
|
36
37
|
task.sleep wait
|
|
38
|
+
break unless running?
|
|
37
39
|
|
|
38
40
|
now = monotonic_now
|
|
39
41
|
while (top = heap.peek) && top.next_run_at <= now
|
|
42
|
+
break unless running?
|
|
43
|
+
|
|
40
44
|
entry = heap.pop
|
|
41
45
|
|
|
42
46
|
if entry.running
|
|
@@ -54,15 +58,26 @@ module Async
|
|
|
54
58
|
heap.push(entry)
|
|
55
59
|
end
|
|
56
60
|
end
|
|
61
|
+
|
|
62
|
+
semaphore.wait
|
|
57
63
|
end
|
|
58
64
|
end
|
|
59
65
|
|
|
66
|
+
def stop
|
|
67
|
+
@running = false
|
|
68
|
+
logger.info { "Async::Background: stopping gracefully" }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def running?
|
|
72
|
+
@running
|
|
73
|
+
end
|
|
74
|
+
|
|
60
75
|
private
|
|
61
76
|
|
|
62
77
|
def build_heap(config_path)
|
|
63
78
|
raise ConfigError, "Schedule file not found: #{config_path}" unless File.exist?(config_path)
|
|
64
79
|
|
|
65
|
-
raw = YAML.safe_load_file(config_path
|
|
80
|
+
raw = YAML.safe_load_file(config_path)
|
|
66
81
|
raise ConfigError, "Empty schedule: #{config_path}" unless raw&.any?
|
|
67
82
|
|
|
68
83
|
heap = MinHeap.new
|
|
@@ -100,9 +115,13 @@ module Async
|
|
|
100
115
|
class_name = config&.dig('class').to_s.strip
|
|
101
116
|
raise ConfigError, "[#{name}] missing class" if class_name.empty?
|
|
102
117
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
118
|
+
begin
|
|
119
|
+
job_class = Object.const_get(class_name)
|
|
120
|
+
rescue NameError
|
|
121
|
+
raise ConfigError, "[#{name}] unknown class: #{class_name}"
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
raise ConfigError, "[#{name}] #{class_name} must implement .perform_now" unless job_class.respond_to?(:perform_now)
|
|
106
125
|
|
|
107
126
|
interval = config['every']&.then { |v|
|
|
108
127
|
int = v.to_i
|
|
@@ -137,7 +156,9 @@ module Async
|
|
|
137
156
|
rescue ::Async::TimeoutError
|
|
138
157
|
logger.error('Async::Background') { "#{entry.name}: timed out after #{entry.timeout}s" }
|
|
139
158
|
rescue => e
|
|
140
|
-
logger.error('Async::Background') {
|
|
159
|
+
logger.error('Async::Background') {
|
|
160
|
+
"#{entry.name}: #{e.class} #{e.message}\n#{e.backtrace.join("\n")}"
|
|
161
|
+
}
|
|
141
162
|
end
|
|
142
163
|
end
|
|
143
164
|
end
|
data/lib/async/background.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: async-background
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Roman Hajdarov
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-03-
|
|
11
|
+
date: 2026-03-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: async
|