sidekiq-cronitor 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +41 -15
- data/lib/sidekiq/cronitor/version.rb +1 -1
- data/lib/sidekiq/cronitor.rb +28 -47
- 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: cab38f6cd486339c23ce89d495d8fd320536ad475385c77f2f28f08ddc78a47b
|
4
|
+
data.tar.gz: ecee9682b2caf0b6becfa83354a5be5275c192f57ab09e1c2da2368ce2aa85f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcbd75ca1e900e2a409d281c2fbad43e6fd76e6c03778c2b87df92cf50160573e96fd437a06f5db1b95e6c7e7c53ec9e3af9dfdce486bef9da517b7f08c07a25
|
7
|
+
data.tar.gz: 9b61ae50868b37b951c444090180ab4b8e0ce680185c61291f6eb16ae71a4138de89cf97d785a330c58948917cc287578526a9b2cb3c78a2b5a2fa779c4b6d8f
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# Sidekiq Cronitor
|
2
2
|
|
3
|
-
|
3
|
+
[Cronitor](https://cronitor.io/) provides dead simple monitoring for cron jobs, daemons, queue workers, websites, APIs, and anything else that can send or receive an HTTP request. The Cronitor Sidekiq library provides a drop in integration for monitoring any Sidekiq Job.
|
4
|
+
|
5
|
+
|
6
|
+
#### NOTE: Version 3.0.0 changes the integration method from 2.x.x. This is a breaking change. You now add the middleware in the Sidekiq initializer. You can opt of out telemetry for specific jobs with a sidekiq_options (see below)
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -19,39 +22,62 @@ bundle
|
|
19
22
|
|
20
23
|
## Usage
|
21
24
|
|
22
|
-
|
25
|
+
Configure `sidekiq-cronitor` with an [API Key](https://cronitor.io/docs/api-overview) from [your settings](https://cronitor.io/settings). You can use ENV variables to configure Cronitor:
|
23
26
|
|
24
27
|
```sh
|
25
|
-
export CRONITOR_API_KEY='
|
28
|
+
export CRONITOR_API_KEY='api_key_123'
|
29
|
+
export CRONITOR_ENVIRONMENT='development' #default: 'production'
|
30
|
+
|
26
31
|
bundle exec sidekiq
|
27
32
|
```
|
28
33
|
|
29
|
-
|
34
|
+
Or declare the API key directly on the Cronitor module from within your application (e.g. the Sidekiq initializer).
|
30
35
|
|
31
36
|
```ruby
|
32
|
-
|
33
|
-
|
34
|
-
|
37
|
+
require 'cronitor'
|
38
|
+
Cronitor.api_key = 'api_key_123'
|
39
|
+
Cronitor.environment = 'development' #default: 'production'
|
40
|
+
```
|
35
41
|
|
36
|
-
|
37
|
-
|
42
|
+
|
43
|
+
To monitor jobs insert the server middleware (most people do this in the Sidekiq initializer)
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
Sidekiq.configure_server do |config|
|
47
|
+
config.server_middleware do |chain|
|
48
|
+
chain.add Sidekiq::Cronitor::ServerMiddleware
|
38
49
|
end
|
39
50
|
end
|
40
51
|
```
|
41
52
|
|
42
|
-
By default this will look for an existing monitor named after your worker, `MyWorker` in the case above, and pings that. Otherwise it will try to create a new monitor with the worker's name, which you can configure rules for at a later time via your Cronitor dashboard.
|
43
53
|
|
44
|
-
|
54
|
+
When this job is invoked, Cronitor will send telemetry pings with a `key` matching the name of your job class (`MyJob` in the example below). If no monitor exists it will create one on the first event. You can configure rules at a later time via the Cronitor dashboard, API, or [YAML config](https://github.com/cronitorio/cronitor-ruby#configuring-monitors) file.
|
55
|
+
|
56
|
+
If you want to specify the Cronitor key directly (not required) you can do so using `sidekiq_options`:
|
45
57
|
|
46
58
|
```ruby
|
47
|
-
class
|
48
|
-
include Sidekiq::
|
49
|
-
include Sidekiq::Cronitor
|
59
|
+
class MyJob
|
60
|
+
include Sidekiq::Job
|
50
61
|
|
62
|
+
# note that 'key' should be set with a symbol as the key in the hash
|
51
63
|
sidekiq_options cronitor: { key: 'abc123' }
|
52
64
|
|
53
65
|
def perform
|
54
|
-
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
|
71
|
+
To disable Cronitor for a specific job you can set the following option:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
class MyJob
|
75
|
+
include Sidekiq::MyJob
|
76
|
+
|
77
|
+
# note that 'disabled' should be set with a symbol as the key in the hash
|
78
|
+
sidekiq_options cronitor: { disabled: true }
|
79
|
+
|
80
|
+
def perform
|
55
81
|
end
|
56
82
|
end
|
57
83
|
```
|
data/lib/sidekiq/cronitor.rb
CHANGED
@@ -4,74 +4,55 @@ require 'cronitor'
|
|
4
4
|
require 'sidekiq/cronitor/version'
|
5
5
|
|
6
6
|
module Sidekiq::Cronitor
|
7
|
-
|
8
|
-
unless base.ancestors.include?(Sidekiq::Worker)
|
9
|
-
raise ArgumentError, 'Sidekiq::Cronitor can only be included in a Sidekiq::Worker'
|
10
|
-
end
|
11
|
-
|
12
|
-
base.extend(ClassMethods)
|
13
|
-
|
14
|
-
# Automatically add sidekiq middleware when we're first included
|
15
|
-
Sidekiq.configure_server do |config|
|
16
|
-
unless config.server_middleware.exists?(Sidekiq::Cronitor::Middleware)
|
17
|
-
config.server_middleware.add(Sidekiq::Cronitor::Middleware)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def cronitor
|
23
|
-
self.class.cronitor
|
24
|
-
end
|
25
|
-
|
26
|
-
module ClassMethods
|
27
|
-
def cronitor
|
28
|
-
return @cronitor if defined?(@cronitor)
|
29
|
-
|
30
|
-
opts = sidekiq_options.fetch('cronitor', {})
|
31
|
-
key = opts.symbolize_keys.fetch(:key, name)
|
32
|
-
|
33
|
-
Sidekiq.logger.debug("[cronitor] initializing monitor: worker=#{name} key=#{key}")
|
34
|
-
|
35
|
-
begin
|
36
|
-
@cronitor = Cronitor::Monitor.new(key)
|
37
|
-
rescue Cronitor::Error => e
|
38
|
-
Sidekiq.logger.error("[cronitor] failed to initialize monitor: worker=#{name} error=#{e.message}")
|
39
|
-
|
40
|
-
@cronitor = nil
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
class Middleware
|
7
|
+
class ServerMiddleware
|
46
8
|
def call(worker, message, queue)
|
47
9
|
ping(worker: worker, state: 'run')
|
48
10
|
|
49
|
-
yield
|
11
|
+
result = yield
|
50
12
|
rescue => e
|
51
13
|
ping(worker: worker, state: 'fail', message: e.to_s)
|
52
14
|
|
53
15
|
raise e
|
54
16
|
else
|
55
17
|
ping(worker: worker, state: 'complete')
|
18
|
+
result # to be consistent with client middleware, return results of yield
|
56
19
|
end
|
57
20
|
|
58
21
|
private
|
22
|
+
def cronitor(worker)
|
23
|
+
Cronitor::Monitor.new(job_key(worker))
|
24
|
+
end
|
25
|
+
|
26
|
+
def cronitor_disabled?(worker)
|
27
|
+
options(worker).fetch(:disabled, false)
|
28
|
+
end
|
29
|
+
|
30
|
+
def job_key(worker)
|
31
|
+
options(worker).fetch(:key, worker.class.name)
|
32
|
+
end
|
33
|
+
|
34
|
+
def options(worker)
|
35
|
+
opts = worker.class.sidekiq_options.fetch('cronitor', {})
|
36
|
+
# symbolize_keys is a rails helper, so only use it if it's defined
|
37
|
+
opts = opts.symbolize_keys if opts.respond_to?(:symbolize_keys)
|
38
|
+
opts
|
39
|
+
end
|
59
40
|
|
60
41
|
def ping(worker:, state:, message: nil)
|
61
|
-
return unless
|
42
|
+
return unless should_ping?(worker)
|
62
43
|
|
63
|
-
Sidekiq.logger.debug("[cronitor] ping: worker=#{worker
|
44
|
+
Sidekiq.logger.debug("[cronitor] ping: worker=#{job_key(worker)} state=#{state} message=#{message}")
|
64
45
|
|
65
|
-
worker.
|
46
|
+
cronitor(worker).ping(state: state)
|
66
47
|
rescue Cronitor::Error => e
|
67
|
-
Sidekiq.logger.error("[cronitor] error during ping: worker=#{worker
|
48
|
+
Sidekiq.logger.error("[cronitor] error during ping: worker=#{job_key(worker)} error=#{e.message}")
|
68
49
|
rescue => e
|
69
|
-
Sidekiq.logger.error("[cronitor] unexpected error: worker=#{worker
|
50
|
+
Sidekiq.logger.error("[cronitor] unexpected error: worker=#{job_key(worker)} error=#{e.message}")
|
70
51
|
Sidekiq.logger.error(e.backtrace.first)
|
71
52
|
end
|
72
53
|
|
73
|
-
def
|
74
|
-
worker.
|
54
|
+
def should_ping?(worker)
|
55
|
+
!cronitor(worker).api_key.nil? && !cronitor_disabled?(worker)
|
75
56
|
end
|
76
57
|
end
|
77
58
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-cronitor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zeke Gabrielse
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-03-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sidekiq
|