Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
306 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module.exports = function( supervisor, citizens, config ){ | ||
|
||
supervisor.deregister = deregister_citizen; | ||
|
||
function perform_deregister_citizen( accept, reject, name ) { | ||
supervisor.hook.run( 'citizen-deregistration', citizens[ name ] ); | ||
|
||
delete citizens[ name ]; | ||
|
||
// deregistration complete | ||
supervisor.noticeboard.notify( 'citizen-deregistered', { name: name }); | ||
supervisor.noticeboard.notify( name + '-deregistered' ); | ||
|
||
return accept(); | ||
} | ||
|
||
function deregister_citizen( name ){ | ||
return new Promise( function ( accept, reject ) { | ||
// if unregistered, reject | ||
if( ! supervisor.is_registered( name ) ) | ||
return reject( new Error( 'no citizen named "' + name + '" has been registed' ) ); | ||
|
||
// if citizen is not running then complete | ||
if( citizens[ name ].ref ) | ||
return supervisor.stop( name ) | ||
.then( () => perform_deregister_citizen( accept, reject, name ) ) | ||
.catch( reject ); | ||
|
||
return perform_deregister_citizen( accept, reject, name ); | ||
}) ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
var merge = require('merge-objects'); | ||
|
||
module.exports = function( supervisor, citizens, config ){ | ||
|
||
supervisor.register = register_citizen; | ||
|
||
function register_citizen( name, file, params ){ | ||
return new Promise( function ( accept, reject ) { | ||
// filter bad input | ||
if( !name ) return reject( new Error( 'requires a name to refer to supervised process' ) ); | ||
if( !file ) return reject( new Error( 'requires path to file to run to supervised process' ) ); | ||
if( typeof name !== 'string' ) return reject( new Error( 'name must be a string' ) ); | ||
if( typeof file !== 'string' ) return reject( new Error( 'path to file must be a string' ) ); | ||
if( !params ) params = {}; | ||
|
||
// filter bad input | ||
if( !name ) throw new Error( 'requires a name to refer to supervised process' ); | ||
if( !file ) throw new Error( 'requires path to file to run to supervised process' ); | ||
if( typeof name !== 'string' ) throw new Error( 'name must be a string' ); | ||
if( typeof file !== 'string' ) throw new Error( 'path to file must be a string' ); | ||
if( !params ) params = {}; | ||
|
||
// ensure citizen name isn't already taken | ||
if( supervisor.is_registered( name ) ){ | ||
// ensure citizen name isn't already taken | ||
if( supervisor.is_registered( name ) ){ | ||
|
||
if( citizens[ name ].file === file ) return citizens[ name ]; | ||
else throw new Error( 'a citizen named "' + name + '" already exists' ); | ||
} | ||
if( citizens[ name ].file === file ) return accept( citizens[ name ] ); | ||
else return reject( new Error( 'a citizen named "' + name + '" already exists' ) ); | ||
} | ||
|
||
// store citizen in map | ||
citizens[ name ] = { | ||
// store citizen in map | ||
citizens[ name ] = { | ||
|
||
name: name, | ||
file: file, | ||
state: {}, | ||
name: name, | ||
file: file, | ||
state: {}, | ||
|
||
config: merge({ retries: config.retries, duration: config.duration }, params ) | ||
}; | ||
config: merge( { retries: config.retries, duration: config.duration }, params ) | ||
}; | ||
|
||
// customize citizen instance | ||
citizens[ name ] = supervisor.hook.run( 'citizen-registration', citizens[ name ] ); | ||
// customize citizen instance | ||
citizens[ name ] = supervisor.hook.run( 'citizen-registration', citizens[ name ] ); | ||
|
||
// registration complete | ||
supervisor.noticeboard.notify( 'citizen-registered', { name: name }); | ||
supervisor.noticeboard.notify( name + '-registered' ); | ||
// registration complete | ||
supervisor.noticeboard.notify( 'citizen-registered', { name: name }); | ||
supervisor.noticeboard.notify( name + '-registered' ); | ||
|
||
return citizens[ name ]; | ||
return accept( citizens[ name ] ); | ||
} ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module.exports = function( supervisor, citizens, config ){ | ||
|
||
supervisor.stop = stop_citizen; | ||
|
||
function stop_citizen( name ){ | ||
return new Promise( function ( accept, reject ) { | ||
// if unregistered, reject | ||
if( ! supervisor.is_registered( name ) ) | ||
return reject( new Error( 'no citizen named "' + name + '" has been registed' ) ); | ||
|
||
var citizen = citizens[ name ]; | ||
|
||
// if citizen is not running then complete | ||
if( ! citizen.ref ) return accept(); | ||
|
||
citizen = supervisor.hook.run( 'citizen-stoping', citizen ); | ||
|
||
citizen.ref.kill('SIGHUP'); | ||
|
||
delete citizen.ref; | ||
|
||
citizens[ name ] = citizen; | ||
|
||
return accept(); | ||
} ); | ||
} | ||
} |
Oops, something went wrong.