mongo-keepalive 1.0.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 +7 -0
- data/lib/keepalive.rb +99 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 28d074e5002228d1b11a3ed4bd8f337693ce0fcb29d35e0fd0348843bffaa5a4
|
|
4
|
+
data.tar.gz: e95137f53edb576bb1b4bcc953a75c0863f6e46669912ba962a06d5cab3bbc81
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c05c2f202e951c5902df82e4ccc5ee3bba2aacc2dfd024dfedbfe26545e888251162aa6e3acee20cee4e678fa2d98a79983529c8aa1047e209abb2e6ea6c8b09
|
|
7
|
+
data.tar.gz: ec86a1e185684e704dae11142d57384d59ddcf7c3586b649a55a23e2279490c515a7f4f65498118880f56eefd1f90c67c8c75f17b219baf9bf31bab7005de0da
|
data/lib/keepalive.rb
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "logger"
|
|
4
|
+
require "mongo"
|
|
5
|
+
|
|
6
|
+
# Keep MongoDB Atlas free-tier clusters alive by periodically sending a ping command.
|
|
7
|
+
module MongoKeepAlive
|
|
8
|
+
DEFAULT_INTERVAL = "12h"
|
|
9
|
+
MAX_RETRIES = 3
|
|
10
|
+
RETRY_DELAY = 5
|
|
11
|
+
|
|
12
|
+
# Control handle returned by {.start_keep_alive}.
|
|
13
|
+
class Handle
|
|
14
|
+
def initialize(client, thread)
|
|
15
|
+
@client = client
|
|
16
|
+
@thread = thread
|
|
17
|
+
@stopped = false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Stop the keep-alive loop and close the connection.
|
|
21
|
+
def stop
|
|
22
|
+
@stopped = true
|
|
23
|
+
@thread.kill
|
|
24
|
+
@client.close
|
|
25
|
+
MongoKeepAlive.logger.info("[mongo-keepalive] Stopped and disconnected.")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def stopped?
|
|
29
|
+
@stopped
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class << self
|
|
34
|
+
# @return [Logger]
|
|
35
|
+
def logger
|
|
36
|
+
@logger ||= Logger.new($stdout, progname: "mongo-keepalive")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
attr_writer :logger
|
|
40
|
+
|
|
41
|
+
# Start the keep-alive loop in a background thread.
|
|
42
|
+
#
|
|
43
|
+
# @param uri [String] MongoDB connection string.
|
|
44
|
+
# @param interval [String] Ping interval, e.g. "12h", "30m", "60s".
|
|
45
|
+
# @return [Handle] A handle with a {Handle#stop} method.
|
|
46
|
+
def start_keep_alive(uri:, interval: DEFAULT_INTERVAL)
|
|
47
|
+
raise ArgumentError, "A MongoDB connection URI is required." if uri.nil? || uri.empty?
|
|
48
|
+
|
|
49
|
+
interval_s = parse_interval(interval)
|
|
50
|
+
logger.info("[mongo-keepalive] Starting with interval #{interval} (#{interval_s} s)")
|
|
51
|
+
|
|
52
|
+
client = Mongo::Client.new(uri)
|
|
53
|
+
db = client.use("admin").database
|
|
54
|
+
|
|
55
|
+
# Initial ping
|
|
56
|
+
ping_with_retry(db)
|
|
57
|
+
|
|
58
|
+
thread = Thread.new do
|
|
59
|
+
loop do
|
|
60
|
+
sleep(interval_s)
|
|
61
|
+
ping_with_retry(db)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
Handle.new(client, thread)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def parse_interval(interval)
|
|
71
|
+
match = interval.strip.match(/\A(\d+)\s*([hms])\z/i)
|
|
72
|
+
raise ArgumentError, "Invalid interval \"#{interval}\". Use e.g. \"12h\", \"30m\", or \"60s\"." unless match
|
|
73
|
+
|
|
74
|
+
value = match[1].to_i
|
|
75
|
+
unit = match[2].downcase
|
|
76
|
+
|
|
77
|
+
case unit
|
|
78
|
+
when "h" then value * 3600
|
|
79
|
+
when "m" then value * 60
|
|
80
|
+
when "s" then value
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def ping_with_retry(db)
|
|
85
|
+
MAX_RETRIES.times do |i|
|
|
86
|
+
attempt = i + 1
|
|
87
|
+
begin
|
|
88
|
+
result = db.command(ping: 1)
|
|
89
|
+
logger.info("[mongo-keepalive] Ping successful: #{result.documents.first}")
|
|
90
|
+
return
|
|
91
|
+
rescue Mongo::Error => e
|
|
92
|
+
logger.warn("[mongo-keepalive] Ping attempt #{attempt}/#{MAX_RETRIES} failed: #{e.message}")
|
|
93
|
+
sleep(RETRY_DELAY) if attempt < MAX_RETRIES
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
logger.error("[mongo-keepalive] All #{MAX_RETRIES} ping attempts failed. Will retry at next interval.")
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mongo-keepalive
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- mongo-keepalive contributors
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-04 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: mongo
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.20'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.20'
|
|
27
|
+
description: Periodically sends a ping command to prevent MongoDB Atlas free-tier
|
|
28
|
+
clusters from becoming inactive.
|
|
29
|
+
email:
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- lib/keepalive.rb
|
|
35
|
+
homepage: https://github.com/YadavSourabhGH/mongo-keepalive
|
|
36
|
+
licenses:
|
|
37
|
+
- MIT
|
|
38
|
+
metadata: {}
|
|
39
|
+
post_install_message:
|
|
40
|
+
rdoc_options: []
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
requirements: []
|
|
54
|
+
rubygems_version: 3.0.3.1
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: Keep MongoDB Atlas free-tier clusters alive
|
|
58
|
+
test_files: []
|