async-background 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81377ce73dc827a65ac5710dcc5343a2c2d97d80800489f50c8c334a71cc5f76
4
- data.tar.gz: 34f372d35ec9b98b88c3009268c2b4427e8d4211c6534d2f0ec9099be922ac5c
3
+ metadata.gz: f3335a0dca391341d47e79016f80305cc492d04198b076e415e6fffa737a616a
4
+ data.tar.gz: 9b0db37886a03f75508c8c01d76f3bde461c5cd4dbc78cbec02f934480712163
5
5
  SHA512:
6
- metadata.gz: cdda5f36c0dcbcabc97b3d580af897ea466c382358848faa436b04d0bfe61e18e16e1743246848f57546ba4908c3876f83f314983b97caf419381757cb1849c7
7
- data.tar.gz: 227eb8648f9a611e866766246b63c288a1d15dc2e7447c8323f11c1d1c7bae5ceebab215ec7922901cb1bb66c4c701bf4d2fcf1dadfcd18b1def693559490d2c
6
+ metadata.gz: 7c8e916287e2eb99a0741d6842f82beb763dc866de76679af19fb3e10e44e05cf4e37d0644f978d244b1068c22021e178869d757bba834fb934ef365adcc32c7
7
+ data.tar.gz: 40ac44f16981597d2c9c3488480188b3125a3a2b5f5362715ced5e34694e9fade82dd103820affe360655d24f5e4876a86409a455aee29c913d2e5808e5c7005
@@ -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, permitted_classes: [Symbol])
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
- job_class = class_name.safe_constantize
104
- raise ConfigError, "[#{name}] unknown class: #{class_name}" unless job_class
105
- raise ConfigError, "[#{name}] #{class_name} must implement #perform" unless job_class.method_defined?(:perform)
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') { "#{entry.name}: #{e.class} #{e.message}" }
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Async
4
4
  module Background
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
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.0
4
+ version: 0.2.0
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-15 00:00:00.000000000 Z
11
+ date: 2026-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async