mongo-keepalive (Ruby)

Gem Version License: MIT

Prevent MongoDB Atlas free-tier clusters from becoming inactive by periodically sending a ping command.

MongoDB Atlas pauses free-tier (M0) clusters after 60 days of inactivity. This library keeps your cluster alive by running db.adminCommand({ ping: 1 }) on a configurable interval (default: every 12 hours).

Installation

Add this line to your application's Gemfile:

gem 'mongo-keepalive'

And then execute:

bundle install

Or install it yourself as:

gem install mongo-keepalive

Usage

require 'mongo_keepalive'

# Start keep-alive with default 12-hour interval
handle = MongoKeepAlive.start_keep_alive(
  uri: 'mongodb+srv://user:[email protected]/db'
)

# Or specify custom interval
handle = MongoKeepAlive.start_keep_alive(
  uri: 'mongodb+srv://user:[email protected]/db',
  interval: '6h'
)

# Graceful shutdown
handle.stop

Features

  • ๐ŸŽฏ Simple API - One method call to start
  • ๐Ÿ”„ Automatic retry - 3 attempts with 5-second delays
  • ๐Ÿ“ Logging - Built-in logger with timestamps
  • ๐Ÿงต Background thread - Non-blocking execution
  • โ™ป๏ธ Graceful shutdown - Clean stop mechanism

Configuration

Parameter Type Default Description
uri String - MongoDB connection string (required)
interval String "12h" Ping interval (e.g. "6h", "30m")

Interval Format

  • "30m" โ†’ 30 minutes
  • "6h" โ†’ 6 hours
  • "12h" โ†’ 12 hours (default)

Requirements

  • Ruby 3.0 or later
  • mongo gem ~> 2.20

License

This project is licensed under the MIT License.