cosmonats 0.8.0 → 0.10.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 +4 -4
- data/README.md +73 -22
- data/lib/cosmo/active_job/adapter.rb +2 -0
- data/lib/cosmo/active_job/executor.rb +7 -0
- data/lib/cosmo/active_job/options.rb +6 -0
- data/lib/cosmo/api/batch.rb +26 -0
- data/lib/cosmo/api/counter.rb +52 -4
- data/lib/cosmo/api/cron/entry.rb +34 -20
- data/lib/cosmo/api/cron.rb +24 -10
- data/lib/cosmo/api/job.rb +15 -1
- data/lib/cosmo/api/kv.rb +141 -12
- data/lib/cosmo/api/registry.rb +47 -6
- data/lib/cosmo/api/stats/jobs.rb +84 -0
- data/lib/cosmo/api/stats/metrics.rb +20 -9
- data/lib/cosmo/api/stats/processes.rb +27 -11
- data/lib/cosmo/api/stats/totals.rb +1 -0
- data/lib/cosmo/api/stats.rb +58 -7
- data/lib/cosmo/api/stream.rb +73 -24
- data/lib/cosmo/batch/callback.rb +5 -0
- data/lib/cosmo/batch/dispatcher.rb +4 -4
- data/lib/cosmo/batch.rb +34 -11
- data/lib/cosmo/cli.rb +6 -0
- data/lib/cosmo/client.rb +217 -23
- data/lib/cosmo/config/cosmo.yml +1 -0
- data/lib/cosmo/config/settings.rb +4 -0
- data/lib/cosmo/config.rb +58 -10
- data/lib/cosmo/engine.rb +28 -1
- data/lib/cosmo/heartbeat.rb +12 -3
- data/lib/cosmo/http_server/handler.rb +5 -0
- data/lib/cosmo/http_server/health.rb +2 -0
- data/lib/cosmo/http_server/ping.rb +2 -0
- data/lib/cosmo/http_server.rb +8 -0
- data/lib/cosmo/job/data.rb +12 -1
- data/lib/cosmo/job/enqueuer.rb +100 -1
- data/lib/cosmo/job/limit.rb +5 -1
- data/lib/cosmo/job/processor.rb +56 -21
- data/lib/cosmo/job/setter.rb +53 -0
- data/lib/cosmo/job.rb +71 -2
- data/lib/cosmo/logger.rb +37 -0
- data/lib/cosmo/middleware/chain.rb +12 -0
- data/lib/cosmo/middleware/limit.rb +12 -3
- data/lib/cosmo/middleware/metrics.rb +6 -4
- data/lib/cosmo/middleware/sentry.rb +6 -4
- data/lib/cosmo/middleware/totals.rb +5 -11
- data/lib/cosmo/middleware.rb +0 -1
- data/lib/cosmo/processor.rb +60 -3
- data/lib/cosmo/publisher.rb +8 -0
- data/lib/cosmo/services.rb +7 -2
- data/lib/cosmo/stream/message.rb +5 -0
- data/lib/cosmo/stream/processor.rb +7 -0
- data/lib/cosmo/stream/serializer.rb +4 -0
- data/lib/cosmo/stream.rb +35 -1
- data/lib/cosmo/utils/duration.rb +3 -0
- data/lib/cosmo/utils/hash.rb +12 -0
- data/lib/cosmo/utils/json.rb +8 -0
- data/lib/cosmo/utils/signal.rb +9 -0
- data/lib/cosmo/utils/stopwatch.rb +1 -0
- data/lib/cosmo/utils/string.rb +4 -0
- data/lib/cosmo/utils/thread_pool.rb +8 -0
- data/lib/cosmo/utils/ttl_cache.rb +10 -0
- data/lib/cosmo/utils.rb +0 -1
- data/lib/cosmo/version.rb +1 -1
- data/lib/cosmo/web/assets/app.css +273 -629
- data/lib/cosmo/web/assets/app.js +158 -0
- data/lib/cosmo/web/assets/fonts/OFL.txt +92 -0
- data/lib/cosmo/web/assets/fonts/geist-mono.woff2 +0 -0
- data/lib/cosmo/web/assets/fonts/geist.woff2 +0 -0
- data/lib/cosmo/web/assets/logo.svg +10 -0
- data/lib/cosmo/web/cache.rb +77 -0
- data/lib/cosmo/web/chart.rb +63 -39
- data/lib/cosmo/web/circuit.rb +56 -0
- data/lib/cosmo/web/context.rb +6 -4
- data/lib/cosmo/web/controllers/application.rb +42 -0
- data/lib/cosmo/web/controllers/batches.rb +8 -1
- data/lib/cosmo/web/controllers/crons.rb +24 -3
- data/lib/cosmo/web/controllers/jobs.rb +88 -68
- data/lib/cosmo/web/controllers/metrics.rb +6 -0
- data/lib/cosmo/web/controllers/overview.rb +88 -0
- data/lib/cosmo/web/controllers/processes.rb +36 -0
- data/lib/cosmo/web/controllers/streams.rb +38 -28
- data/lib/cosmo/web/helpers/application.rb +101 -0
- data/lib/cosmo/web/renderer.rb +36 -6
- data/lib/cosmo/web/throughput.rb +104 -0
- data/lib/cosmo/web/views/batches/_table.erb +42 -54
- data/lib/cosmo/web/views/batches/index.erb +3 -10
- data/lib/cosmo/web/views/crons/_table.erb +36 -55
- data/lib/cosmo/web/views/crons/index.erb +2 -9
- data/lib/cosmo/web/views/jobs/_args.erb +5 -0
- data/lib/cosmo/web/views/jobs/_busy.erb +25 -121
- data/lib/cosmo/web/views/jobs/_dead.erb +42 -82
- data/lib/cosmo/web/views/jobs/_enqueued.erb +33 -113
- data/lib/cosmo/web/views/jobs/_panel.erb +34 -0
- data/lib/cosmo/web/views/jobs/_queue_row.erb +18 -0
- data/lib/cosmo/web/views/jobs/_queues.erb +20 -0
- data/lib/cosmo/web/views/jobs/_scheduled.erb +20 -54
- data/lib/cosmo/web/views/jobs/index.erb +4 -17
- data/lib/cosmo/web/views/layout.erb +73 -22
- data/lib/cosmo/web/views/metrics/_chart.erb +37 -32
- data/lib/cosmo/web/views/metrics/_panel.erb +3 -3
- data/lib/cosmo/web/views/metrics/_table.erb +24 -40
- data/lib/cosmo/web/views/metrics/index.erb +8 -16
- data/lib/cosmo/web/views/overview/_badges.erb +3 -0
- data/lib/cosmo/web/views/overview/_kpis.erb +19 -0
- data/lib/cosmo/web/views/overview/_running.erb +18 -0
- data/lib/cosmo/web/views/overview/_throughput.erb +50 -0
- data/lib/cosmo/web/views/overview/_workers.erb +25 -0
- data/lib/cosmo/web/views/overview/index.erb +28 -0
- data/lib/cosmo/web/views/processes/_table.erb +67 -0
- data/lib/cosmo/web/views/processes/index.erb +1 -0
- data/lib/cosmo/web/views/shared/_pager.erb +22 -0
- data/lib/cosmo/web/views/shared/unavailable.erb +7 -0
- data/lib/cosmo/web/views/streams/_info.erb +29 -86
- data/lib/cosmo/web/views/streams/_list.erb +7 -0
- data/lib/cosmo/web/views/streams/_pause_banner.erb +8 -16
- data/lib/cosmo/web/views/streams/_stream.erb +53 -0
- data/lib/cosmo/web/views/streams/index.erb +3 -11
- data/lib/cosmo/web/views/streams/info.erb +3 -11
- data/lib/cosmo/web.rb +73 -35
- data/lib/cosmo.rb +33 -0
- data/sig/cosmo/active_job/adapter.rbs +3 -3
- data/sig/cosmo/active_job/options.rbs +4 -2
- data/sig/cosmo/api/batch.rbs +5 -3
- data/sig/cosmo/api/counter.rbs +16 -6
- data/sig/cosmo/api/cron/entry.rbs +6 -3
- data/sig/cosmo/api/cron.rbs +10 -4
- data/sig/cosmo/api/kv.rbs +21 -4
- data/sig/cosmo/api/registry.rbs +10 -0
- data/sig/cosmo/api/stats/jobs.rbs +34 -0
- data/sig/cosmo/api/stats/metrics.rbs +4 -1
- data/sig/cosmo/api/stats/processes.rbs +9 -1
- data/sig/cosmo/api/stats.rbs +12 -2
- data/sig/cosmo/api/stream.rbs +12 -5
- data/sig/cosmo/batch/callback.rbs +1 -1
- data/sig/cosmo/batch/dispatcher.rbs +1 -1
- data/sig/cosmo/batch.rbs +5 -5
- data/sig/cosmo/cli.rbs +2 -0
- data/sig/cosmo/client.rbs +24 -4
- data/sig/cosmo/config/settings.rbs +6 -0
- data/sig/cosmo/config.rbs +5 -5
- data/sig/cosmo/engine.rbs +5 -0
- data/sig/cosmo/heartbeat.rbs +2 -0
- data/sig/cosmo/job/data.rbs +3 -1
- data/sig/cosmo/job/enqueuer.rbs +12 -0
- data/sig/cosmo/job/processor.rbs +18 -3
- data/sig/cosmo/job/setter.rbs +20 -0
- data/sig/cosmo/job.rbs +10 -4
- data/sig/cosmo/logger.rbs +5 -3
- data/sig/cosmo/middleware.rbs +0 -8
- data/sig/cosmo/processor.rbs +14 -2
- data/sig/cosmo/publisher.rbs +1 -0
- data/sig/cosmo/stream/processor.rbs +4 -0
- data/sig/cosmo/stream.rbs +4 -3
- data/sig/cosmo/utils/hash.rbs +2 -1
- data/sig/cosmo/utils/signal.rbs +5 -5
- data/sig/cosmo/utils/string.rbs +2 -2
- data/sig/cosmo/utils/thread_pool.rbs +1 -1
- data/sig/cosmo/web/cache.rbs +35 -0
- data/sig/cosmo/web/chart.rbs +48 -0
- data/sig/cosmo/web/circuit.rbs +29 -0
- data/sig/cosmo/web/context.rbs +15 -0
- data/sig/cosmo/web/controllers/application.rbs +32 -0
- data/sig/cosmo/web/controllers/batches.rbs +11 -0
- data/sig/cosmo/web/controllers/crons.rbs +23 -0
- data/sig/cosmo/web/controllers/jobs.rbs +47 -0
- data/sig/cosmo/web/controllers/metrics.rbs +21 -0
- data/sig/cosmo/web/controllers/overview.rbs +31 -0
- data/sig/cosmo/web/controllers/processes.rbs +13 -0
- data/sig/cosmo/web/controllers/streams.rbs +27 -0
- data/sig/cosmo/web/helpers/application.rbs +61 -0
- data/sig/cosmo/web/renderer.rbs +33 -0
- data/sig/cosmo/web/throughput.rbs +45 -0
- data/sig/cosmo/web.rbs +25 -0
- data/sig/cosmo.rbs +19 -0
- metadata +53 -23
- data/lib/cosmo/api/stats/busy.rb +0 -63
- data/lib/cosmo/middleware/busy.rb +0 -15
- data/lib/cosmo/utils/overrides.rb +0 -206
- data/lib/cosmo/utils/warnings.rb +0 -17
- data/lib/cosmo/web/controllers/actions.rb +0 -16
- data/lib/cosmo/web/views/actions/index.erb +0 -7
- data/lib/cosmo/web/views/jobs/_processes.erb +0 -145
- data/lib/cosmo/web/views/jobs/_stats.erb +0 -77
- data/lib/cosmo/web/views/jobs/_tabs.erb +0 -14
- data/lib/cosmo/web/views/jobs/busy.erb +0 -18
- data/lib/cosmo/web/views/jobs/dead.erb +0 -18
- data/lib/cosmo/web/views/jobs/enqueued.erb +0 -18
- data/lib/cosmo/web/views/jobs/scheduled.erb +0 -18
- data/lib/cosmo/web/views/streams/_stream_row.erb +0 -42
- data/lib/cosmo/web/views/streams/_table.erb +0 -25
- data/sig/cosmo/api/stats/busy.rbs +0 -31
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
module Cosmo
|
|
4
4
|
module Stream
|
|
5
5
|
class Processor < ::Cosmo::Processor
|
|
6
|
+
RELEASE_DELAY = "10s" # How long NATS holds a message released by a timed out shutdown before redelivering it (override via COSMO_STREAMS_RELEASE_DELAY)
|
|
7
|
+
|
|
6
8
|
# @return [Array<String>] names of the stream classes being processed
|
|
7
9
|
def subscriptions
|
|
8
10
|
@consumers.map { |(_, config, _)| config[:class].name }
|
|
@@ -10,6 +12,11 @@ module Cosmo
|
|
|
10
12
|
|
|
11
13
|
private
|
|
12
14
|
|
|
15
|
+
def abandon(message)
|
|
16
|
+
delay = Utils::Duration.parse(ENV.fetch("COSMO_STREAMS_RELEASE_DELAY", RELEASE_DELAY))
|
|
17
|
+
message.nak(delay: Config.to_ns(delay))
|
|
18
|
+
end
|
|
19
|
+
|
|
13
20
|
def setup
|
|
14
21
|
@configs ||= []
|
|
15
22
|
@configs = static_config + dynamic_config
|
|
@@ -7,10 +7,14 @@ module Cosmo
|
|
|
7
7
|
module Serializer
|
|
8
8
|
module_function
|
|
9
9
|
|
|
10
|
+
# @param data [Object]
|
|
11
|
+
# @return [String, nil] JSON, or nil when +data+ cannot be dumped
|
|
10
12
|
def serialize(data)
|
|
11
13
|
Utils::Json.dump(data)
|
|
12
14
|
end
|
|
13
15
|
|
|
16
|
+
# @param payload [String]
|
|
17
|
+
# @return [Object, nil] the parsed JSON with string keys, or nil when it is malformed
|
|
14
18
|
def deserialize(payload)
|
|
15
19
|
Utils::Json.parse(payload, symbolize_names: false)
|
|
16
20
|
end
|
data/lib/cosmo/stream.rb
CHANGED
|
@@ -7,28 +7,50 @@ require "cosmo/stream/serializer"
|
|
|
7
7
|
|
|
8
8
|
module Cosmo
|
|
9
9
|
module Stream
|
|
10
|
+
# @param base [Class] the stream class including Stream
|
|
11
|
+
# @return [void]
|
|
10
12
|
def self.included(base)
|
|
11
13
|
base.extend(ClassMethods)
|
|
12
14
|
end
|
|
13
15
|
|
|
14
16
|
module ClassMethods
|
|
17
|
+
# Registers the class as a stream processor and merges the given options over its defaults.
|
|
18
|
+
#
|
|
19
|
+
# @param stream [Symbol, String, nil] NATS stream to consume
|
|
20
|
+
# @param consumer_name [String, nil] durable consumer name
|
|
21
|
+
# @param batch_size [Integer, nil] messages fetched per batch
|
|
22
|
+
# @param fetch_timeout [Numeric, nil] seconds to wait for a batch
|
|
23
|
+
# @param start_position [Symbol, String, Time, nil] where a new consumer starts delivering from
|
|
24
|
+
# @param consumer [Hash, nil] consumer config, e.g. +subjects:+
|
|
25
|
+
# @param publisher [Hash, nil] publisher config: +subject:+ and +serializer:+
|
|
26
|
+
# @return [Hash{Symbol => Object}] the class's merged options
|
|
15
27
|
def options(stream: nil, consumer_name: nil, batch_size: nil, fetch_timeout: nil, start_position: nil, consumer: nil, publisher: nil)
|
|
16
28
|
register
|
|
17
29
|
default_options.merge!({ stream:, consumer_name:, batch_size:, fetch_timeout:, start_position:, consumer:, publisher: }.compact)
|
|
18
30
|
end
|
|
19
31
|
alias cosmo_options options
|
|
20
32
|
|
|
33
|
+
# @param data [Object] serialized with the publisher's serializer
|
|
34
|
+
# @param subject [String, nil] defaults to the publisher's subject
|
|
35
|
+
# @param options [Hash] passed on to the JetStream publish
|
|
36
|
+
# @return [NATS::JetStream::PubAck]
|
|
21
37
|
def publish(data, subject: nil, **options)
|
|
22
38
|
stream = default_options[:stream]
|
|
23
39
|
subject ||= default_options.dig(:publisher, :subject)
|
|
24
40
|
Publisher.publish(subject, data, stream: stream, serializer: default_options.dig(:publisher, :serializer), **options)
|
|
25
41
|
end
|
|
26
42
|
|
|
43
|
+
# Options inherited from the superclass stream, or the built-in defaults.
|
|
44
|
+
#
|
|
45
|
+
# @return [Hash{Symbol => Object}]
|
|
27
46
|
def default_options
|
|
28
47
|
@default_options ||= Utils::Hash.dup(superclass.respond_to?(:default_options) ? superclass.default_options : Data::DEFAULTS)
|
|
29
48
|
end
|
|
30
49
|
|
|
31
|
-
|
|
50
|
+
# Adds the class to the processed streams and derives its stream, consumer and subjects from its name.
|
|
51
|
+
#
|
|
52
|
+
# @return [void]
|
|
53
|
+
def register
|
|
32
54
|
Config.internal[:streams] ||= []
|
|
33
55
|
Config.internal[:streams] << self
|
|
34
56
|
|
|
@@ -47,6 +69,10 @@ module Cosmo
|
|
|
47
69
|
end
|
|
48
70
|
end
|
|
49
71
|
|
|
72
|
+
# Calls {#process_one} for each message, exposing it through {#message}.
|
|
73
|
+
#
|
|
74
|
+
# @param messages [Array<Message>]
|
|
75
|
+
# @return [void]
|
|
50
76
|
def process(messages)
|
|
51
77
|
messages.each do |message|
|
|
52
78
|
Thread.current[:cosmo_message] = message
|
|
@@ -58,18 +84,26 @@ module Cosmo
|
|
|
58
84
|
alias process_many process
|
|
59
85
|
alias process_batch process
|
|
60
86
|
|
|
87
|
+
# @return [void]
|
|
88
|
+
# @raise [NotImplementedError] unless the stream class overrides it or {#process}
|
|
61
89
|
def process_one
|
|
62
90
|
raise NotImplementedError, "#{self.class}#process_one must be implemented"
|
|
63
91
|
end
|
|
64
92
|
|
|
93
|
+
# @return [::Logger]
|
|
65
94
|
def logger
|
|
66
95
|
Logger.instance
|
|
67
96
|
end
|
|
68
97
|
|
|
98
|
+
# @return [Message, nil] the message being processed by {#process_one}
|
|
69
99
|
def message
|
|
70
100
|
Thread.current[:cosmo_message]
|
|
71
101
|
end
|
|
72
102
|
|
|
103
|
+
# @param data [Object] serialized with the publisher's serializer
|
|
104
|
+
# @param subject [String, nil] defaults to the publisher's subject
|
|
105
|
+
# @param options [Hash] passed on to the JetStream publish
|
|
106
|
+
# @return [NATS::JetStream::PubAck]
|
|
73
107
|
def publish(data, subject, **options)
|
|
74
108
|
self.class.publish(data, subject:, **options)
|
|
75
109
|
end
|
data/lib/cosmo/utils/duration.rb
CHANGED
|
@@ -18,6 +18,9 @@ module Cosmo
|
|
|
18
18
|
|
|
19
19
|
module_function
|
|
20
20
|
|
|
21
|
+
# @param value [Numeric, String] seconds, or a duration such as "20s", "1h", "7d"
|
|
22
|
+
# @return [Numeric] seconds
|
|
23
|
+
# @raise [ArgumentError] when +value+ is not a valid duration
|
|
21
24
|
def parse(value)
|
|
22
25
|
return value if value.is_a?(Numeric)
|
|
23
26
|
|
data/lib/cosmo/utils/hash.rb
CHANGED
|
@@ -5,6 +5,11 @@ module Cosmo
|
|
|
5
5
|
module Hash
|
|
6
6
|
module_function
|
|
7
7
|
|
|
8
|
+
# Symbolizes hash keys in place, recursing into nested hashes and arrays.
|
|
9
|
+
#
|
|
10
|
+
# @param obj [Object]
|
|
11
|
+
# @return [Object] +obj+, mutated
|
|
12
|
+
# @raise [ArgumentError] when a key cannot be converted to a symbol
|
|
8
13
|
def symbolize_keys!(obj)
|
|
9
14
|
case obj
|
|
10
15
|
when ::Hash
|
|
@@ -23,6 +28,10 @@ module Cosmo
|
|
|
23
28
|
end
|
|
24
29
|
end
|
|
25
30
|
|
|
31
|
+
# Stringifies hash keys, recursing into nested hashes and arrays.
|
|
32
|
+
#
|
|
33
|
+
# @param obj [Object]
|
|
34
|
+
# @return [Object] a copy of +obj+
|
|
26
35
|
def stringify_keys(obj)
|
|
27
36
|
case obj
|
|
28
37
|
when ::Hash
|
|
@@ -46,6 +55,9 @@ module Cosmo
|
|
|
46
55
|
end
|
|
47
56
|
|
|
48
57
|
# deep dup
|
|
58
|
+
#
|
|
59
|
+
# @param hash [Object] a Marshal-able value
|
|
60
|
+
# @return [Object] a deep copy of +hash+
|
|
49
61
|
def dup(hash)
|
|
50
62
|
Marshal.load(Marshal.dump(hash))
|
|
51
63
|
end
|
data/lib/cosmo/utils/json.rb
CHANGED
|
@@ -7,12 +7,20 @@ module Cosmo
|
|
|
7
7
|
module Json
|
|
8
8
|
module_function
|
|
9
9
|
|
|
10
|
+
# @param value [String]
|
|
11
|
+
# @param default [Object] returned when +value+ is not valid JSON
|
|
12
|
+
# @param symbolize_names [Boolean]
|
|
13
|
+
# @param options [Hash] passed on to +JSON.parse+
|
|
14
|
+
# @return [Object]
|
|
10
15
|
def parse(value, default: nil, symbolize_names: true, **options)
|
|
11
16
|
JSON.parse(value, **options, symbolize_names:)
|
|
12
17
|
rescue TypeError, JSON::ParserError
|
|
13
18
|
default
|
|
14
19
|
end
|
|
15
20
|
|
|
21
|
+
# @param value [Object]
|
|
22
|
+
# @param default [Object] returned when +value+ cannot be generated
|
|
23
|
+
# @return [String, Object]
|
|
16
24
|
def dump(value, default: nil)
|
|
17
25
|
::JSON.generate(value)
|
|
18
26
|
rescue TypeError, JSON::NestingError
|
data/lib/cosmo/utils/signal.rb
CHANGED
|
@@ -3,19 +3,28 @@
|
|
|
3
3
|
module Cosmo
|
|
4
4
|
module Utils
|
|
5
5
|
class Signal
|
|
6
|
+
# Traps the given signals, see {#initialize}.
|
|
7
|
+
#
|
|
8
|
+
# @return [Signal]
|
|
6
9
|
def self.trap(...)
|
|
7
10
|
new(...)
|
|
8
11
|
end
|
|
9
12
|
|
|
13
|
+
# @param signals [Array<Symbol, String>] signals to trap
|
|
10
14
|
def initialize(*signals)
|
|
11
15
|
@queue = Queue.new
|
|
12
16
|
signals.each { |s| ::Signal.trap(s) { push(s) } }
|
|
13
17
|
end
|
|
14
18
|
|
|
19
|
+
# Blocks until a trapped signal arrives.
|
|
20
|
+
#
|
|
21
|
+
# @return [Symbol, String] the signal
|
|
15
22
|
def wait
|
|
16
23
|
@queue.pop # Wait indefinitely for a signal
|
|
17
24
|
end
|
|
18
25
|
|
|
26
|
+
# @param signal [Symbol, String]
|
|
27
|
+
# @return [void]
|
|
19
28
|
def push(signal)
|
|
20
29
|
@queue.push(signal)
|
|
21
30
|
end
|
data/lib/cosmo/utils/string.rb
CHANGED
|
@@ -5,6 +5,8 @@ module Cosmo
|
|
|
5
5
|
module String
|
|
6
6
|
module_function
|
|
7
7
|
|
|
8
|
+
# @param value [String, Symbol, Class] e.g. "Cosmo::SendEmailJob"
|
|
9
|
+
# @return [String] e.g. "cosmo-send_email_job"
|
|
8
10
|
def underscore(value)
|
|
9
11
|
value
|
|
10
12
|
.to_s
|
|
@@ -14,6 +16,8 @@ module Cosmo
|
|
|
14
16
|
.downcase
|
|
15
17
|
end
|
|
16
18
|
|
|
19
|
+
# @param value [String] a constant name
|
|
20
|
+
# @return [Object, nil] the constant, or nil when it is not defined
|
|
17
21
|
def safe_constantize(value)
|
|
18
22
|
Object.const_get(value)
|
|
19
23
|
rescue NameError
|
|
@@ -14,6 +14,7 @@ module Cosmo
|
|
|
14
14
|
|
|
15
15
|
delegate %i[shutdown wait_for_termination] => :@pool
|
|
16
16
|
|
|
17
|
+
# @param concurrency [Integer] number of threads
|
|
17
18
|
def initialize(concurrency)
|
|
18
19
|
@mutex = Thread::Mutex.new
|
|
19
20
|
@concurrency = concurrency
|
|
@@ -22,10 +23,17 @@ module Cosmo
|
|
|
22
23
|
@pool = Concurrent::FixedThreadPool.new(concurrency)
|
|
23
24
|
end
|
|
24
25
|
|
|
26
|
+
# Blocks until no task is running.
|
|
27
|
+
#
|
|
28
|
+
# @return [void]
|
|
25
29
|
def wait_idle
|
|
26
30
|
@mutex.synchronize { @cond.wait(@mutex) until @available == @concurrency }
|
|
27
31
|
end
|
|
28
32
|
|
|
33
|
+
# Runs the block on a pool thread, blocking until one is free.
|
|
34
|
+
#
|
|
35
|
+
# @yield the task
|
|
36
|
+
# @return [void]
|
|
29
37
|
def post
|
|
30
38
|
@mutex.synchronize do
|
|
31
39
|
@cond.wait(@mutex) while @available <= 0
|
|
@@ -7,17 +7,27 @@ module Cosmo
|
|
|
7
7
|
@store = {}
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
+
# @param key [Object]
|
|
11
|
+
# @param value [Object]
|
|
12
|
+
# @param ttl [Numeric, nil] seconds until the value expires, nil keeps it forever
|
|
13
|
+
# @return [Object] +value+
|
|
10
14
|
def set(key, value, ttl: nil)
|
|
11
15
|
@store[key] = [value, ttl ? Time.now + ttl : nil]
|
|
12
16
|
value
|
|
13
17
|
end
|
|
14
18
|
|
|
19
|
+
# @param key [Object]
|
|
20
|
+
# @return [Object, nil] the value, or nil when it is missing or expired
|
|
15
21
|
def get(key)
|
|
16
22
|
return unless key?(key)
|
|
17
23
|
|
|
18
24
|
@store[key].first
|
|
19
25
|
end
|
|
20
26
|
|
|
27
|
+
# @param key [Object]
|
|
28
|
+
# @param ttl [Numeric, nil] seconds until a computed value expires
|
|
29
|
+
# @yieldreturn [Object] the value, computed only when no fresh copy is cached
|
|
30
|
+
# @return [Object]
|
|
21
31
|
def fetch(key, ttl: nil)
|
|
22
32
|
return get(key) if key?(key)
|
|
23
33
|
|
data/lib/cosmo/utils.rb
CHANGED
data/lib/cosmo/version.rb
CHANGED