async-background 0.2.3 → 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 +4 -4
- data/lib/async/background/runner.rb +39 -5
- data/lib/async/background/version.rb +1 -1
- data/lib/async/background.rb +0 -8
- metadata +6 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b412d31294adfc8d7a2586f31f42f47cd0a004e992016615dff81fc1828b7e90
|
|
4
|
+
data.tar.gz: 3d629e5272bd5f819e8fe2f3ed4507e73b720ce13c71c06433f1d7f3f9e9f563
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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|
|
|
31
|
-
|
|
32
|
+
setup_signal_handlers
|
|
33
|
+
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
data/lib/async/background.rb
CHANGED
|
@@ -6,14 +6,6 @@ require 'console'
|
|
|
6
6
|
require 'fugit'
|
|
7
7
|
|
|
8
8
|
require_relative 'background/version'
|
|
9
|
-
|
|
10
|
-
# Warn if using an unstable version
|
|
11
|
-
unless ['0.1.0', '0.2.2', '0.2.3'].include?(Async::Background::VERSION)
|
|
12
|
-
warn "\n⚠️ WARNING: Async::Background v#{Async::Background::VERSION} is not a stable version!"
|
|
13
|
-
warn "⚠️ Stable versions are: 0.1.0, 0.2.2, and 0.2.3"
|
|
14
|
-
warn "⚠️ For production use, pin to one of these versions in your Gemfile\n\n"
|
|
15
|
-
end
|
|
16
|
-
|
|
17
9
|
require_relative 'background/min_heap'
|
|
18
10
|
require_relative 'background/entry'
|
|
19
11
|
require_relative 'background/runner'
|
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
|
+
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-
|
|
11
|
+
date: 2026-03-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: async
|
|
@@ -80,11 +80,10 @@ dependencies:
|
|
|
80
80
|
- - "~>"
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
82
|
version: '3.12'
|
|
83
|
-
description:
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
application."
|
|
83
|
+
description: A production-grade lightweight scheduler built on top of Async. Single
|
|
84
|
+
event loop with min-heap timer, skip-overlapping execution, jitter, monotonic clock
|
|
85
|
+
intervals, semaphore concurrency control, and deterministic worker sharding. Designed
|
|
86
|
+
for Falcon but works with any Async-based application.
|
|
88
87
|
email:
|
|
89
88
|
- romnhajdarov@gmail.com
|
|
90
89
|
executables: []
|