katalyst-healthcheck 0.2.8 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95661561842df409390e4a798c57b959bcbc39d0803f526fe39c4781b1e87903
4
- data.tar.gz: a022ff2050eb06f96fa641bad74b0a1a0132b09b2e09cd9171654e2ce93f179d
3
+ metadata.gz: c405714831e17ff682cee082363e5e3b9433dd622aae6f125c7d6f8cb9e260ba
4
+ data.tar.gz: 5c72ee6dbe280e60404ae84b02b4f7db11a9aa59d8b313a638b5fa8b64c0d1ba
5
5
  SHA512:
6
- metadata.gz: bc2f21383a0ae5f28f40c072e746e265d481d5a3d99659f2b9dfe4bc2e73e85471a6d3a452f66ce4d3f26aed2eedecd36eab6fc9e4efd1e1936a33a7603a0c91
7
- data.tar.gz: f6ade891f379e6abd9a959f07568e9e230a3d7ddeea83cd8337656bc8594536091a39bdcac3c948e9da42a4c7efeab8f41de074e8d9c7f285772d937bf03e086
6
+ metadata.gz: 5fd33513793e0523ea4e6ff37d5210f0409c1dd6572c9715d97034664164ad1c3f2517bed5b514695b189e59f1170fcd58cb29873e6e03483c46047217acc3f0
7
+ data.tar.gz: b8edd34e5e1c248fedce6067fd6224b50d513b9c4742dc05cdf248c7059222cf00a1aebb4d3a2a7e9b354ffa4b382533977a8325095343bafed5916945ef7ae7
data/README.md CHANGED
@@ -58,7 +58,7 @@ the `Katalyst::Healthcheck::Monitored` concern's `healthy!` and `unhealthy` meth
58
58
  ``` ruby
59
59
  include Katalyst::Healthcheck::Monitored
60
60
 
61
- define_task :my_task, "My task description", interval: 1.day
61
+ define_healthcheck_task :my_task, "My task description", interval: 1.day
62
62
 
63
63
  def do_task
64
64
  ... task code here ...
data/Rakefile CHANGED
@@ -5,8 +5,4 @@ require "rspec/core/rake_task"
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
8
- require "rubocop/rake_task"
9
-
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[spec rubocop]
8
+ task default: %i[spec]
@@ -11,7 +11,7 @@ module Katalyst
11
11
 
12
12
  sidekiq_options retry: false
13
13
 
14
- define_task :sidekiq_health, "Sidekiq background processing", interval: 60
14
+ define_healthcheck_task :sidekiq_health, "Sidekiq background processing", interval: 60
15
15
 
16
16
  def self.call
17
17
  perform_async
@@ -16,8 +16,7 @@ module Katalyst
16
16
  private
17
17
 
18
18
  def build_store(name)
19
- klass = "Katalyst::Healthcheck::Store::#{name.to_s.camelize}".constantize
20
- klass.new
19
+ Object.const_get("Katalyst::Healthcheck::Store::#{name.to_s.capitalize}").new
21
20
  end
22
21
  end
23
22
  end
@@ -3,16 +3,24 @@
3
3
  module Katalyst
4
4
  module Healthcheck
5
5
  module Monitored
6
- extend ActiveSupport::Concern
6
+ def self.included(klass)
7
+ klass.extend(ClassMethods)
8
+ end
7
9
 
8
- module HealthMethods
10
+ module ClassMethods
9
11
  # Define a task to be monitored
10
12
  # @param name [Symbol] The name of the task
11
13
  # @param description [String] A description of the task's function
12
14
  # @param interval [Integer,ActiveSupport::Duration] Expected frequency that this task runs, e.g. 1.day
13
- def define_task(name, description, interval:)
15
+ def define_healthcheck_task(name, description, interval:)
14
16
  defined_healthcheck_tasks[name] = Task.new(name: name, description: description, interval: interval)
15
17
  end
18
+ alias define_task define_healthcheck_task
19
+
20
+ # @return [Hash] Defined tasks keyed by name
21
+ def defined_healthcheck_tasks
22
+ @defined_healthcheck_tasks ||= {}
23
+ end
16
24
 
17
25
  # Mark a task as healthy
18
26
  # @param name [Symbol] The name of the task
@@ -41,22 +49,17 @@ module Katalyst
41
49
  end
42
50
  end
43
51
 
44
- class_methods do
45
- include HealthMethods
46
-
47
- attr_accessor :healthcheck_task_definitions
48
-
49
- # @return [Hash] Defined tasks keyed by name
50
- def defined_healthcheck_tasks
51
- self.healthcheck_task_definitions ||= {}
52
- end
52
+ # Mark a task as healthy
53
+ # @param name [Symbol] The name of the task
54
+ def healthy!(name)
55
+ self.class.healthy! name
53
56
  end
54
57
 
55
- include HealthMethods
56
-
57
- # @return [Hash] Defined tasks keyed by name
58
- def defined_healthcheck_tasks
59
- self.class.healthcheck_task_definitions ||= {}
58
+ # Mark a task as unhealthy
59
+ # @param name [Symbol] The name of the task
60
+ # @param error [String] Optional error message
61
+ def unhealthy!(name, error = nil)
62
+ self.class.unhealthy! name, error
60
63
  end
61
64
  end
62
65
  end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ # Inspired by ActiveModel::Attributes, but with support for Rails 3+
6
+ module Katalyst
7
+ module Healthcheck
8
+ module Store
9
+ module Attributes
10
+ def self.included(klass)
11
+ klass.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+ @@attributes = {}
16
+
17
+ def attribute(name, type = :string)
18
+ name = name.to_s
19
+ serializer = case type
20
+ when :integer
21
+ IntegerAttribute
22
+ when :datetime
23
+ DateTimeAttribute
24
+ else
25
+ Attribute
26
+ end
27
+ define_method(name) { @@attributes[name].read(@attributes) }
28
+ define_method("#{name}=") { |value| @@attributes[name].write(@attributes, value) }
29
+ @@attributes[name] = serializer.new(name)
30
+ end
31
+ end
32
+
33
+ def initialize(attributes)
34
+ @attributes = {}
35
+ self.attributes = attributes
36
+ end
37
+
38
+ def attributes
39
+ @attributes.keys.map { |k| [k, public_send(k)] }.to_h
40
+ end
41
+
42
+ def attributes=(attributes)
43
+ attributes.each do |k, v|
44
+ public_send("#{k}=", v)
45
+ end
46
+ attributes
47
+ end
48
+ end
49
+
50
+ class Attribute
51
+ attr_reader :key
52
+
53
+ def initialize(key)
54
+ @key = key
55
+ end
56
+
57
+ def read(attributes)
58
+ deserialize(attributes[key]) if attributes.has_key?(key)
59
+ end
60
+
61
+ def write(attributes, value)
62
+ if value.nil?
63
+ attributes.delete(key)
64
+ elsif value.is_a?(String) # support already serialized values
65
+ attributes[key] = value
66
+ else
67
+ attributes[key] = serialize(value)
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def deserialize(value)
74
+ value
75
+ end
76
+
77
+ def serialize(value)
78
+ value.to_s
79
+ end
80
+ end
81
+
82
+ class IntegerAttribute < Attribute
83
+ private
84
+
85
+ def deserialize(value)
86
+ Integer(value)
87
+ end
88
+ end
89
+
90
+ class DateTimeAttribute < Attribute
91
+ private
92
+
93
+ def serialize(value)
94
+ value.iso8601
95
+ end
96
+
97
+ def deserialize(value)
98
+ DateTime.parse(value)
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -20,8 +20,8 @@ module Katalyst
20
20
  # @return [String] Redis URL defined in rails config
21
21
  def rails_redis_url
22
22
  redis_config = rails_redis_config || {}
23
- host = redis_config[:host] || DEFAULT_HOST
24
- port = redis_config[:port] || DEFAULT_PORT
23
+ host = redis_config["host"] || DEFAULT_HOST
24
+ port = redis_config["port"] || DEFAULT_PORT
25
25
  "redis://#{host}:#{port}"
26
26
  end
27
27
 
@@ -62,7 +62,7 @@ module Katalyst
62
62
  if task_state.nil?
63
63
  task_data.delete(name)
64
64
  else
65
- task_data[name] = serialize(task_state)
65
+ task_data[name] = task_state
66
66
  end
67
67
  client.set(cache_key, JSON.generate(data))
68
68
  end
@@ -76,18 +76,6 @@ module Katalyst
76
76
 
77
77
  private
78
78
 
79
- # @param task_state [Hash]
80
- def serialize(task_state)
81
- task_state.transform_values do |value|
82
- case value
83
- when ActiveSupport::TimeWithZone, DateTime
84
- value.strftime("%d/%m/%Y %H:%M:%S %z")
85
- else
86
- value
87
- end
88
- end
89
- end
90
-
91
79
  def cache_key
92
80
  options.cache_key || DEFAULT_CACHE_KEY
93
81
  end
@@ -104,7 +92,7 @@ module Katalyst
104
92
  end
105
93
 
106
94
  def lock_manager
107
- raise "redis url is required" if options.url.blank?
95
+ raise "redis url is required" unless options.url
108
96
 
109
97
  @lock_manager ||= ::Redlock::Client.new([options.url])
110
98
  end
@@ -4,8 +4,7 @@ module Katalyst
4
4
  module Healthcheck
5
5
  # Represents a background task that runs periodically in an application
6
6
  class Task
7
- include ActiveModel::Model
8
- include ActiveModel::Attributes
7
+ include Store::Attributes
9
8
 
10
9
  TASK_GRACE_PERIOD = 10 * 60 # 10 minutes
11
10
 
@@ -65,11 +64,11 @@ module Katalyst
65
64
 
66
65
  # @return [Boolean] true if this background task is running on schedule
67
66
  def on_schedule?
68
- next_time.nil? || next_time + TASK_GRACE_PERIOD > Time.current
67
+ next_time.nil? || next_time + TASK_GRACE_PERIOD > DateTime.now
69
68
  end
70
69
 
71
70
  def next_time
72
- return nil if interval.blank?
71
+ return nil if interval.nil?
73
72
 
74
73
  (last_time || created_at) + interval
75
74
  end
@@ -77,7 +76,7 @@ module Katalyst
77
76
  # Mark this task as healthy and save state
78
77
  # @return [Task] This task
79
78
  def healthy!
80
- self.last_time = Time.current
79
+ self.last_time = DateTime.now
81
80
  self.error = nil
82
81
  self.status = :ok
83
82
 
@@ -87,7 +86,7 @@ module Katalyst
87
86
  # Mark this task as unhealthy and save state
88
87
  # @return [Task] This task
89
88
  def unhealthy!(error = nil)
90
- self.error = error.presence || "Fail"
89
+ self.error = error || "Fail"
91
90
  self.status = :fail
92
91
 
93
92
  save
@@ -96,8 +95,8 @@ module Katalyst
96
95
  # Save task state
97
96
  # @return [Task] This task
98
97
  def save
99
- self.created_at ||= Time.now
100
- self.updated_at = Time.now
98
+ self.created_at ||= DateTime.now
99
+ self.updated_at = DateTime.now
101
100
  store.update(name, attributes)
102
101
  self
103
102
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Katalyst
4
4
  module Healthcheck
5
- VERSION = "0.2.8"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support"
4
- require "active_model"
5
3
  require "redlock"
6
4
 
7
5
  require "katalyst/healthcheck/version"
8
6
  require "katalyst/healthcheck/railtie" if defined?(Rails)
9
7
  require "katalyst/healthcheck/config"
8
+ require "katalyst/healthcheck/store/attributes"
10
9
  require "katalyst/healthcheck/store/memory"
11
10
  require "katalyst/healthcheck/store/redis"
12
11
  require "katalyst/healthcheck/task"
metadata CHANGED
@@ -1,57 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-healthcheck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-06 00:00:00.000000000 Z
11
+ date: 2022-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activemodel
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '6'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '6'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: redis
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - ">="
32
18
  - !ruby/object:Gem::Version
33
- version: 3.3.5
19
+ version: '3'
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
24
  - - ">="
39
25
  - !ruby/object:Gem::Version
40
- version: 3.3.5
26
+ version: '3'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: redlock
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - ">="
46
32
  - !ruby/object:Gem::Version
47
- version: 1.2.2
33
+ version: '1.2'
48
34
  type: :runtime
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - ">="
53
39
  - !ruby/object:Gem::Version
54
- version: 1.2.2
40
+ version: '1.2'
55
41
  description:
56
42
  email:
57
43
  - admin@katalyst.com.au
@@ -68,6 +54,7 @@ files:
68
54
  - lib/katalyst/healthcheck/monitored.rb
69
55
  - lib/katalyst/healthcheck/railtie.rb
70
56
  - lib/katalyst/healthcheck/route.rb
57
+ - lib/katalyst/healthcheck/store/attributes.rb
71
58
  - lib/katalyst/healthcheck/store/memory.rb
72
59
  - lib/katalyst/healthcheck/store/redis.rb
73
60
  - lib/katalyst/healthcheck/task.rb
@@ -90,14 +77,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
77
  requirements:
91
78
  - - ">="
92
79
  - !ruby/object:Gem::Version
93
- version: '2.7'
80
+ version: '2.3'
94
81
  required_rubygems_version: !ruby/object:Gem::Requirement
95
82
  requirements:
96
83
  - - ">="
97
84
  - !ruby/object:Gem::Version
98
85
  version: '0'
99
86
  requirements: []
100
- rubygems_version: 3.1.6
87
+ rubygems_version: 3.3.15
101
88
  signing_key:
102
89
  specification_version: 4
103
90
  summary: Health check routes and functions