{"id":13878200,"url":"https://github.com/discourse/rails_failover","last_synced_at":"2025-04-04T17:08:28.779Z","repository":{"id":39107284,"uuid":"265478676","full_name":"discourse/rails_failover","owner":"discourse","description":null,"archived":false,"fork":false,"pushed_at":"2023-09-05T00:49:07.000Z","size":652,"stargazers_count":108,"open_issues_count":0,"forks_count":3,"subscribers_count":24,"default_branch":"main","last_synced_at":"2024-05-09T09:26:22.233Z","etag":null,"topics":["rubygem"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/discourse.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-20T06:57:35.000Z","updated_at":"2024-05-29T10:57:30.143Z","dependencies_parsed_at":"2024-05-29T10:57:27.874Z","dependency_job_id":"f9d7c1e8-e217-4f70-9d22-2331cff72257","html_url":"https://github.com/discourse/rails_failover","commit_stats":{"total_commits":127,"total_committers":9,"mean_commits":14.11111111111111,"dds":0.4330708661417323,"last_synced_commit":"9e85522062b3abf6717a8d1278235812d9803892"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/discourse%2Frails_failover","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/discourse%2Frails_failover/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/discourse%2Frails_failover/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/discourse%2Frails_failover/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/discourse","download_url":"https://codeload.github.com/discourse/rails_failover/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247217184,"owners_count":20903009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rubygem"],"created_at":"2024-08-06T08:01:42.531Z","updated_at":"2025-04-04T17:08:28.760Z","avatar_url":"https://github.com/discourse.png","language":"Ruby","readme":"# RailsFailover\n\nAutomatic failover and recovery for primary/replica setup for:\n\n1. Redis\n2. ActiveRecord (PostgreSQL/MySQL Adapters)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rails_failover', require: false\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install rails_failover\n\n## Usage\n\n### ActiveRecord\n\nIn `config/application.rb` add `require 'rails_failover/active_record'` after `require \"active_record/railtie\"`.\n\nIn your database configuration, simply add `replica_host` and `replica_port` to your database configuration.\n\n```yml\nproduction:\n  host: \u003cprimary db server host\u003e\n  port: \u003cprimary db server port\u003e\n  replica_host: \u003creplica db server host\u003e\n  replica_port: \u003creplica db server port\u003e\n```\n\nThe gem will automatically create a role (using `ActiveRecord.reading_role`) on\nthe default `ActiveRecord` connection handler.\n\n#### Failover/Fallback Hooks\n\n```ruby\nRailsFailover::ActiveRecord.on_failover do\n  # Enable readonly mode\nend\n\nRailsFailover::ActiveRecord.on_fallback do\n  # Disable readonly mode\nend\n```\n\n#### Multiple connection handlers\n\n```yml\n# config/database.yml\n\nproduction:\n  primary:\n    host: \u003cprimary db server host\u003e\n    port: \u003cprimary db server port\u003e\n    replica_host: \u003creplica db server host\u003e\n    replica_port: \u003creplica db server port\u003e\n  second_database_writing:\n    host: \u003cprimary db server host\u003e\n    port: \u003cprimary db server port\u003e\n    replica_host: \u003creplica db server host\u003e\n    replica_port: \u003creplica db server port\u003e\n\n# In your ActiveRecord base model or model.\n\nconnects_to database: { writing: :primary, second_database_writing: :second_database_writing }\n```\n\n### Redis\n\nAdd `require 'rails_failover/redis'` before creating a `Redis` instance.\n\n```ruby\n# Redis/RedisClient 4.x\nRedis.new(\n  host: \"127.0.0.1\",\n  port: 6379,\n  replica_host: \"127.0.0.1\",\n  replica_port: 6380,\n  connector: RailsFailover::Redis::Connector,\n)\n\n# Redis/RedisClient 5.x\nRedis.new(\n  host: \"127.0.0.1\",\n  port: 6379,\n  client_implementation: RailsFailover::Redis::Client,\n  custom: {\n    replica_host: \"127.0.0.1\",\n    replica_port: 6380,\n  }\n)\n```\n\nCallbacks can be registered when the primary connection is down and when it is up.\n\n```ruby\nRailsFailover::Redis.on_failover_callback do\n  # Switch site to read-only mode\nend\n\nRailsFailover::Redis.on_fallback_callback do\n  # Switch site out of read-only mode\nend\n```\n\n\u003e ⚠️ If you’re using Sidekiq, don’t provide it with the replica configuration as it won’t work. RailsFailover works with a replica in read-only mode, meaning Sidekiq wouldn’t work properly anyway as it needs to write to Redis.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n### Testing\n\n#### ActiveRecord\n\nThe ActiveRecord failover tests are run against a dummy Rails server. Run the following commands to run the test:\n\n1. `make setup_pg`\n3. `bin/rspec active_record`. You may also run the tests with more unicorn workers by adding the `UNICORN_WORKERS` env variable.\n\n#### Redis\n\n`bin/rspec redis`\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/discourse/rails_failover. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/discourse/rails_failover/blob/master/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the RailsFailover project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/discourse/rails_failover/blob/master/CODE_OF_CONDUCT.md).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiscourse%2Frails_failover","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiscourse%2Frails_failover","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiscourse%2Frails_failover/lists"}