Skip to content
Snippets Groups Projects
Verified Commit 1a49e8f9 authored by Erick Hitter's avatar Erick Hitter
Browse files

Utility to disconnect from lock and clear its caches

parent 316b1078
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,7 @@ app.use( morgan( DEBUG ? 'dev' : 'combined' ) ); ...@@ -26,7 +26,7 @@ app.use( morgan( DEBUG ? 'dev' : 'combined' ) );
* UTILITIES * UTILITIES
*/ */
// Default return arguments // Default return arguments
var ret = { var ret = {
'status': -1, 'status': -1,
'ret': '', 'ret': '',
...@@ -47,7 +47,7 @@ function statusStringtoInt( status ) { ...@@ -47,7 +47,7 @@ function statusStringtoInt( status ) {
} }
// Clear cached routes // Clear cached routes
function clear_caches( lockName ) { function clearCaches( lockName ) {
apicache.clear( '/api/status/' + lockName ); apicache.clear( '/api/status/' + lockName );
apicache.clear( '/api/lock/' + lockName ); apicache.clear( '/api/lock/' + lockName );
apicache.clear( '/api/unlock/' + lockName ); apicache.clear( '/api/unlock/' + lockName );
...@@ -55,6 +55,12 @@ function clear_caches( lockName ) { ...@@ -55,6 +55,12 @@ function clear_caches( lockName ) {
return true; return true;
} }
// Reset lock connection
function disconnectAndClear( lockName ) {
lock.disconnect();
clearCaches( lockName );
}
/** /**
* ROUTES * ROUTES
*/ */
...@@ -64,7 +70,7 @@ app.get( '/api/status/:lock_name', cache( '15 seconds' ), function( req, res ) { ...@@ -64,7 +70,7 @@ app.get( '/api/status/:lock_name', cache( '15 seconds' ), function( req, res ) {
// Parse allowed request arguments // Parse allowed request arguments
var lock = app.get( 'lock' + req.params.lock_name ); var lock = app.get( 'lock' + req.params.lock_name );
if ( ! lock ) { if ( ! lock ) {
clear_caches( req.params.lock_name ); clearCaches( req.params.lock_name );
res.sendStatus( 400 ); res.sendStatus( 400 );
return; return;
} }
...@@ -84,8 +90,7 @@ app.get( '/api/status/:lock_name', cache( '15 seconds' ), function( req, res ) { ...@@ -84,8 +90,7 @@ app.get( '/api/status/:lock_name', cache( '15 seconds' ), function( req, res ) {
// Perform requested action // Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) { lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err ); console.error( err );
lock.disconnect(); disconnectAndClear( req.params.lock_name );
clear_caches( req.params.lock_name );
res.sendStatus( 500 ); res.sendStatus( 500 );
} ); } );
} ); } );
...@@ -103,7 +108,7 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', cache( '5 seconds' ), func ...@@ -103,7 +108,7 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', cache( '5 seconds' ), func
var lockName = req.params.lock_name, var lockName = req.params.lock_name,
lock = app.get( 'lock' + lockName ); lock = app.get( 'lock' + lockName );
if ( ! lock ) { if ( ! lock ) {
clear_caches( lockName ); clearCaches( lockName );
res.sendStatus( 400 ); res.sendStatus( 400 );
return; return;
} }
...@@ -130,16 +135,14 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', cache( '5 seconds' ), func ...@@ -130,16 +135,14 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', cache( '5 seconds' ), func
ret.msg = "No change made. Lock was already '" + status + "'."; ret.msg = "No change made. Lock was already '" + status + "'.";
} }
lock.disconnect(); disconnectAndClear( lockName );
clear_caches( lockName );
res.json( ret ); res.json( ret );
} ); } );
// Perform requested action // Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) { lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err ); console.error( err );
lock.disconnect(); disconnectAndClear( lockName );
clear_caches( lockName );
res.sendStatus( 500 ); res.sendStatus( 500 );
} ); } );
} ); } );
...@@ -149,13 +152,12 @@ app.get( '/api/disconnect/:lock_name', function( req, res ) { ...@@ -149,13 +152,12 @@ app.get( '/api/disconnect/:lock_name', function( req, res ) {
// Parse allowed request arguments // Parse allowed request arguments
var lock = app.get( 'lock' + req.params.lock_name ); var lock = app.get( 'lock' + req.params.lock_name );
if ( ! lock ) { if ( ! lock ) {
clear_caches( req.params.lock_name ); clearCaches( req.params.lock_name );
res.sendStatus( 400 ); res.sendStatus( 400 );
return; return;
} }
lock.disconnect(); disconnectAndClear( req.params.lock_name );
clear_caches( req.params.lock_name );
res.sendStatus( 204 ); res.sendStatus( 204 );
} ); } );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment