Skip to content

Commit

Permalink
database: add periodic test query
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Jan 18, 2018
1 parent 56c5f8e commit 6c4140d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/smc-hub/postgres-base.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class exports.PostgreSQL extends EventEmitter # emits a 'connect' event whene
@_debug = opts.debug
@_timeout_ms = opts.timeout_ms
@_ensure_exists = opts.ensure_exists
@_init_test_query()
dbg = @_dbg("constructor") # must be after setting @_debug above
dbg(opts)
i = opts.host.indexOf(':')
Expand Down Expand Up @@ -115,6 +116,7 @@ class exports.PostgreSQL extends EventEmitter # emits a 'connect' event whene
close: =>
if @_state == 'closed'
return # nothing to do
@_close_test_query()
@_state = 'closed'
@emit('close')
@removeAllListeners()
Expand All @@ -123,6 +125,29 @@ class exports.PostgreSQL extends EventEmitter # emits a 'connect' event whene
@_client.end()
delete @_client

###
If @_timeout_ms is set, then we periodically do a simple test query,
to ensure that the database connection is working and responding to queries.
If the query below times out, then the connection will get recreated.
###
_do_test_query: =>
dbg = @_dbg('test_query')
dbg('starting')
@_query
query : 'SELECT NOW()'
cb : (err, result) =>
dbg("finished", err, result)

_init_test_query: =>
if not @_timeout_ms
return
@_test_query = setInterval(@_do_test_query, @_timeout_ms)

_close_test_query: =>
if @_test_query?
clearInterval(@_test_query)
delete @_test_query

engine: -> 'postgresql'

connect: (opts) =>
Expand Down

0 comments on commit 6c4140d

Please sign in to comment.