activegraph: Optionally wait for database to become available instead of dying immediately on connection failure

I came up with a workaround that allows me to use Rails+Neo4j with Docker Compose. Docker Comopse tries to start everything up in the right order, but usually tries to start the web server before the Neo4j server is ready for connections, and because the Rails application immediately tries to connect to the Neo4j server on initialization, it errors out and dies.

Instead of having to remember to pre-start the Neo4j server, I created a method which tries once a second for 30 seconds (by default) to open a simple connection to the Neo4j server, before allowing initialization to continue. The upside is that docker-compose up works smoothly now, and the web server typically has to wait less than ten seconds for Neo4j to be ready for connections. The downside is that running rake tasks that would not normally even get to the point of trying to connect to Neo4j (e.g., in my case, setting up and starting Neo4j with the provided rake task in Travis CI) have to wait 30 seconds for no reason whatsoever.

Would it be desirable to add an option to wait for a (optionally specified) length of time for the Neo4j server to become available, before erroring out as unable to connect? Or, is there another approach I could take that wouldn’t make everything that goes through my application’s config/application.rb wait for 30 seconds, whether or not Neo4j would even try to connect to the database server? (I’ve thought about emulating how the connection itself happens in Neo4j::Railtie, and programmatically adding an initializer that runs between load_config_initializers and neo4j.start, but haven’t tried implementing it yet.)

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Hey ! Sorry for the long time without answering ! My project with your gem is 2 month old, you can see it here, it was a quick test : [https://github.com/disastrous-charly/breizhcamp-pokerails]

I was using your github branch !

😉 No problem, I pushed so I think it should be working now

Ok, so I’ve been playing with Docker Compose and I’ve decided that I was wrong 😉

I just released version 6.1.6 which has a wait_for_connection config which you can set in config/application.rb or config/environments/*.rb like:

    config.neo4j.wait_for_connection = true

That should keep trying to connect for up to 60 seconds if set with some dots outputted to STDOUT.