async-background 0.2.4 → 0.2.5

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: 49376bad9260678399dcf092b9d9f10bb86d834793937b00d2b32beac9b40a92
4
- data.tar.gz: 1af4b44f91b2e06b6eee54d4916236c57ca70f0e61219aad4560b47ea5c5397d
3
+ metadata.gz: b412d31294adfc8d7a2586f31f42f47cd0a004e992016615dff81fc1828b7e90
4
+ data.tar.gz: 3d629e5272bd5f819e8fe2f3ed4507e73b720ce13c71c06433f1d7f3f9e9f563
5
5
  SHA512:
6
- metadata.gz: 8b0dfde6d86f7198912c0aa106f2abb96fd6e7ff88052a0005c8c49cf7171ce16ad2ba9845f106ae5afa7b623d9c4846082be015e3dfb00482680b172fe20944
7
- data.tar.gz: 2bf30304fb376f90631df8dc1e3ac5d369653095a36dc686485457412fad3025b834ee71b0fe17cf92ae743c4f30c0f9492e296341153cc17299ac8a33ce3fe3
6
+ metadata.gz: 9eab99f4c978281eea0689a96dfc45b356e82991fc177bf57ab919f688c397d6361f348fc0451c20b45207c124f15106e9b4bd6e637866103830fd64937266a0
7
+ data.tar.gz: 15b3e8677ec777ae4395d87fbfca81ce9b79fa67d3e2c1b425cbcdac17c12df5f382550a13593f1d71518e0f6ead48e31dd5e474c99634aedc7bea6fa4afe9ba
@@ -12,13 +12,14 @@ module Async
12
12
  MAX_JITTER = 5
13
13
 
14
14
  class Runner
15
- attr_reader :logger, :semaphore, :heap, :worker_index, :total_workers
15
+ attr_reader :logger, :semaphore, :heap, :worker_index, :total_workers, :shutdown
16
16
 
17
17
  def initialize(config_path:, job_count: 2, worker_index:, total_workers:)
18
18
  @logger = Console.logger
19
19
  @worker_index = worker_index
20
20
  @total_workers = total_workers
21
21
  @running = true
22
+ @shutdown = ::Async::Condition.new
22
23
 
23
24
  logger.info { "Async::Background worker_index=#{worker_index}/#{total_workers}, job_count=#{job_count}" }
24
25
 
@@ -28,13 +29,15 @@ module Async
28
29
 
29
30
  def run
30
31
  Async do |task|
32
+ setup_signal_handlers
33
+
31
34
  loop do
32
35
  entry = heap.peek
33
36
  break unless entry
34
37
 
35
38
  now = monotonic_now
36
39
  wait = [entry.next_run_at - now, MIN_SLEEP_TIME].max
37
- task.sleep wait
40
+ wait_with_shutdown(task, wait)
38
41
  break unless running?
39
42
 
40
43
  now = monotonic_now
@@ -47,7 +50,7 @@ module Async
47
50
  logger.warn('Async::Background') { "#{entry.name}: skipped, previous run still active" }
48
51
  else
49
52
  entry.running = true
50
- semaphore.async(parent: task) do
53
+ semaphore.async do
51
54
  run_job(task, entry)
52
55
  ensure
53
56
  entry.running = false
@@ -59,13 +62,16 @@ module Async
59
62
  end
60
63
  end
61
64
 
62
- semaphore.wait
65
+ semaphore.acquire {}
63
66
  end
64
67
  end
65
68
 
66
69
  def stop
70
+ return unless @running
71
+
67
72
  @running = false
68
73
  logger.info { "Async::Background: stopping gracefully" }
74
+ shutdown.signal
69
75
  end
70
76
 
71
77
  def running?
@@ -74,6 +80,34 @@ module Async
74
80
 
75
81
  private
76
82
 
83
+ def setup_signal_handlers
84
+ @signal_r, @signal_w = IO.pipe
85
+
86
+ %w[INT TERM].each do |signal|
87
+ Signal.trap(signal) do
88
+ @running = false
89
+ @signal_w.write_nonblock('.') rescue nil
90
+ end
91
+ end
92
+ end
93
+
94
+ def wait_with_shutdown(task, duration)
95
+ timer = task.async(transient: true) do
96
+ task.sleep duration
97
+ shutdown.signal
98
+ end
99
+
100
+ watcher = task.async(transient: true) do
101
+ @signal_r.wait_readable
102
+ @signal_r.read_nonblock(256) rescue nil
103
+ shutdown.signal
104
+ end
105
+
106
+ shutdown.wait
107
+ timer.stop
108
+ watcher.stop
109
+ end
110
+
77
111
  def build_heap(config_path)
78
112
  raise ConfigError, "Schedule file not found: #{config_path}" unless File.exist?(config_path)
79
113
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Async
4
4
  module Background
5
- VERSION = '0.2.4'
5
+ VERSION = '0.2.5'
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.2.4
4
+ version: 0.2.5
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-16 00:00:00.000000000 Z
11
+ date: 2026-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async