file-based-healthcheck 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '008743fbd28e50d9e75a5d1266ded9bd9bf4a7abb19ffeea607eec8f055f83e3'
4
+ data.tar.gz: fccf8c78aa4edb171e1047a82f7a632319df87ca75c0f393406d05798ab0ef0a
5
+ SHA512:
6
+ metadata.gz: 70a97b3dea0b9b503cde1113bd34b7b5d191dd76a504367f69ea83c79f939449bc2b96b56a59416e2221fa49e2142db3675fe1620f99ba0165225bd2180841b9
7
+ data.tar.gz: b6f0753e57d11ca76d7cf9b29c5e206496612e6089bf8004a750a71d3e7a638c887a6b1b08130e108b3dbb6a0109bda44c234b607b9f674b875f4dffe6d14766
@@ -0,0 +1,13 @@
1
+ version: 2.1
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: ruby:2.7.2
6
+ steps:
7
+ - checkout
8
+ - run:
9
+ name: Run the default task
10
+ command: |
11
+ gem install bundler -v 2.2.11
12
+ bundle install
13
+ bundle exec rake
@@ -0,0 +1,19 @@
1
+ name: CI
2
+ on: [pull_request]
3
+ jobs:
4
+ rspec:
5
+ strategy:
6
+ fail-fast: false
7
+ matrix:
8
+ include:
9
+ - { ruby: '2.7' }
10
+ - { ruby: '3.0' }
11
+ - { ruby: '3.1' }
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby }}
18
+ bundler-cache: true
19
+ - run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-19
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in file-based-healthcheck.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ file-based-healthcheck (0.1.0)
5
+ activesupport (>= 3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (7.0.4)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ concurrent-ruby (1.1.10)
16
+ diff-lcs (1.5.0)
17
+ i18n (1.12.0)
18
+ concurrent-ruby (~> 1.0)
19
+ minitest (5.16.3)
20
+ rake (13.0.6)
21
+ rspec (3.11.0)
22
+ rspec-core (~> 3.11.0)
23
+ rspec-expectations (~> 3.11.0)
24
+ rspec-mocks (~> 3.11.0)
25
+ rspec-core (3.11.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-expectations (3.11.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-mocks (3.11.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-support (3.11.0)
34
+ tzinfo (2.0.5)
35
+ concurrent-ruby (~> 1.0)
36
+
37
+ PLATFORMS
38
+ x86_64-darwin-18
39
+ x86_64-linux
40
+
41
+ DEPENDENCIES
42
+ file-based-healthcheck!
43
+ rake (~> 13.0)
44
+ rspec (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 2.2.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Karol Galanciak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # FileBasedHealthcheck
2
+
3
+ A gem to use a healthcheck for readiness and liveness probes in Kubernetes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'file-based-healthcheck'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install file-based-healthcheck
20
+
21
+ ## Usage
22
+
23
+ The idea behind this gem is to register heartbeats using a file. Thanks to that and comparing when the last heartbeat, happened you can easily check if the process is running or not.
24
+
25
+ Initialize a new healthcheck:
26
+
27
+ ``` rb
28
+ healthcheck = FileBasedHealthcheck.new(directory: Rails.root.join("tmp"), filename: ENV.fetch("HOSTNAME"), time_threshold: 60)
29
+ ```
30
+
31
+ Register a heartbeat:
32
+
33
+ ``` rb
34
+ healthcheck.touch
35
+ ```
36
+
37
+ To verify if the process is running:
38
+
39
+ ``` rb
40
+ healthcheck.running?
41
+ ```
42
+
43
+ This method is going to return `true` if the file (based on `filename`) was touched in the last `time_threshold` in seconds.
44
+
45
+ To remove file:
46
+
47
+ ``` rb
48
+ healthcheck.remove
49
+ ```
50
+
51
+ It is recommended to implement some sort of wrapper for your processes and use this gem under the hood. Then, you could create a following binary:
52
+
53
+ ``` rb
54
+ #!/usr/bin/env ruby
55
+
56
+ require "bundler/setup"
57
+ require_relative "../lib/my_process"
58
+
59
+ result = MyProcess::HealthCheck.check
60
+ if result.success?
61
+ exit 0
62
+ else
63
+ MyProcess.logger.fatal "[MyProcess] health check failed: #{result.message}"
64
+ exit 1
65
+ end
66
+ ```
67
+
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/file-based-healthcheck.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "file/based/healthcheck"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/file_based_healthcheck/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "file-based-healthcheck"
7
+ spec.version = FileBasedHealthcheck::VERSION
8
+ spec.authors = ["Karol Galanciak"]
9
+ spec.email = ["karol.galanciak@gmail.com", "karol@bookingsync.com", "dev@bookingsync.com"]
10
+
11
+ spec.summary = "A gem to use a healthcheck for readiness and liveness probes in Kubernetes."
12
+ spec.description = "A gem to use a healthcheck for readiness and liveness probes in Kubernetes."
13
+ spec.homepage = "https://github.com/BookingSync/file-based-healthcheck"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/BookingSync/file-based-healthcheck."
21
+ spec.metadata["changelog_uri"] = "https://github.com/BookingSync/file-based-healthcheck/blob/master/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+ spec.add_dependency "activesupport", ">= 3.2"
35
+
36
+ # For more information and examples about making a new gem, checkout our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
@@ -0,0 +1 @@
1
+ requir "file_based_healthcheck"
@@ -0,0 +1,6 @@
1
+ class FileBasedHealthcheck
2
+ VERSION = "0.1.0"
3
+
4
+ module Version
5
+ end
6
+ end
@@ -0,0 +1,34 @@
1
+ require "file_based_healthcheck/version"
2
+ require "fileutils"
3
+ require "active_support/core_ext/time"
4
+
5
+ class FileBasedHealthcheck
6
+ attr_reader :directory, :filename, :time_threshold
7
+ private :directory, :filename, :time_threshold
8
+
9
+ def initialize(directory:, filename:, time_threshold:)
10
+ @directory = directory
11
+ @filename = filename
12
+ @time_threshold = time_threshold
13
+ end
14
+
15
+ def touch
16
+ FileUtils.touch(file_path)
17
+ end
18
+
19
+ def running?
20
+ return false if !File.exists?(file_path)
21
+
22
+ File.mtime(file_path).advance(seconds: time_threshold).utc > Time.now.utc
23
+ end
24
+
25
+ def remove
26
+ File.delete(file_path) if File.exists?(file_path)
27
+ end
28
+
29
+ private
30
+
31
+ def file_path
32
+ File.join(directory, filename)
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file-based-healthcheck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karol Galanciak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ description: A gem to use a healthcheck for readiness and liveness probes in Kubernetes.
28
+ email:
29
+ - karol.galanciak@gmail.com
30
+ - karol@bookingsync.com
31
+ - dev@bookingsync.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".circleci/config.yml"
37
+ - ".github/workflows/ci.yml"
38
+ - ".gitignore"
39
+ - ".rspec"
40
+ - CHANGELOG.md
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - bin/console
47
+ - bin/setup
48
+ - file-based-healthcheck.gemspec
49
+ - lib/file-based-healthcheck.rb
50
+ - lib/file_based_healthcheck.rb
51
+ - lib/file_based_healthcheck/version.rb
52
+ homepage: https://github.com/BookingSync/file-based-healthcheck
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/BookingSync/file-based-healthcheck
57
+ source_code_uri: https://github.com/BookingSync/file-based-healthcheck.
58
+ changelog_uri: https://github.com/BookingSync/file-based-healthcheck/blob/master/CHANGELOG.md
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.4.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.1.4
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: A gem to use a healthcheck for readiness and liveness probes in Kubernetes.
78
+ test_files: []