Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.4.0 #14

Merged
merged 45 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f0b84ee
on signal, run hook instead of sending a notice
Akamaozu Dec 11, 2018
71085f0
switch supe modules dependent on citizen-signal notice to hooks
Akamaozu Dec 11, 2018
742e35c
fix: map noticeboard args to signal's
Akamaozu Dec 11, 2018
41cd2bc
refactor notice inbox-no-longer-empty to hook
Akamaozu Dec 11, 2018
4e0d131
log failure to handle mail-related signal
Akamaozu Dec 11, 2018
66ab5da
minor code cleanup
Akamaozu Dec 11, 2018
da7856e
fix: properly prevent require-directory from working recursively
Akamaozu Dec 11, 2018
17c785a
reduce code duplication in mail module
Akamaozu Dec 11, 2018
e183de2
create hooks for stdout and stderr output
Akamaozu Dec 12, 2018
0a0c574
minor code cleanup
Akamaozu Dec 12, 2018
59085a6
minor code cleanup
Akamaozu Dec 12, 2018
4c30979
moved signal setup to citizen-registration hook
Akamaozu Dec 12, 2018
a24d5dc
create citizen-registered hook
Akamaozu Dec 12, 2018
c6de43d
rename hook creation files to improve clarity
Akamaozu Dec 12, 2018
4a83fef
create notice for citizen-registered lifecycle event
Akamaozu Dec 12, 2018
df55cc2
converted excessive-crash and auto-restarted notices to hooks
Akamaozu Dec 12, 2018
5a00a05
convert citizen-started notice to hook
Akamaozu Dec 12, 2018
e87ccc8
notify on citizen-started hook
Akamaozu Dec 12, 2018
56a15fb
convert citizen-stopped notice to hook
Akamaozu Dec 12, 2018
18b9636
update stop tests to use hooks
Akamaozu Dec 12, 2018
a9e3ea9
convert tests using notices for extensions to hooks
Akamaozu Dec 12, 2018
380a6fe
move mail requeue on citizen exit behavior from notice to hook
Akamaozu Dec 12, 2018
7cdc128
detect mail by plugging into citizen-ipc hook instead of creating ext…
Akamaozu Dec 12, 2018
659da35
detect signal by plugging into citizen-ipc hook instead of creating e…
Akamaozu Dec 12, 2018
460acdc
fix: properly add signal sender to envelope
Akamaozu Dec 12, 2018
4196c8e
switch citizen-deregistered from notice to hook
Akamaozu Dec 12, 2018
111768c
switch deregistration from citizen-stop notice to hook
Akamaozu Dec 12, 2018
a683bc6
remove citizen-registered notice
Akamaozu Dec 12, 2018
2b23767
log citizen lifecycle events to stdout
Akamaozu Dec 12, 2018
461cd60
pipe citizen stdout and stderr to supervisor
Akamaozu Dec 12, 2018
91b3a4e
Merge branches 'log-citizen-lifecycle' and 'log-citizen-output' into …
Akamaozu Dec 12, 2018
05f3bda
make test citizen interval-logger a supe citizen
Akamaozu Dec 12, 2018
e6ef2e3
switch child_process.kill references in tests to supervisor.stop
Akamaozu Dec 13, 2018
a0660e0
minor code cleanup
Akamaozu Dec 13, 2018
a651be5
minor code cleanup
Akamaozu Dec 13, 2018
2b31f29
rename citizen module references from supe to citizen
Akamaozu Dec 14, 2018
ed51155
create supervisor-ipc hook for citizens
Akamaozu Dec 14, 2018
ed2a0f1
citizen detects signals sent from supervisor via ipc
Akamaozu Dec 14, 2018
9ce59fd
switched citizen shutdown signal handler to supervisor-signal hook
Akamaozu Dec 14, 2018
07dfd77
switch citizen modules to hook system
Akamaozu Dec 14, 2018
98f9878
update tests to use new mail strategy
Akamaozu Dec 14, 2018
37b29ac
create hook for unhandled mail
Akamaozu Dec 14, 2018
4614971
update test citizen to handle only routed mail
Akamaozu Dec 14, 2018
7e39c2f
minor code cleanup
Akamaozu Dec 14, 2018
8d47f95
version 0.4.0
Akamaozu Dec 14, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/modules/citizen/mail/detect-mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = function( citizen, config ){

citizen.hook.add( 'supervisor-ipc', 'detect-mail', function( details ){
if( ! details || ! details.hasOwnProperty( 'data' ) ) return;

var envelope = details.data;

if( Object.prototype.toString.call( envelope ) !== '[object Object]' ) return;
if( ! envelope.hasOwnProperty( 'type' ) ) return;
if( envelope.type !== 'mail' ) return;

var mail_handled = false;

citizen.hook.run( 'citizen-mail', envelope, ack_current_mail );

if( ! mail_handled ){
citizen.hook.run( 'unhandled-citizen-mail', envelope );
ack_current_mail();
}

citizen.hook.end();

function ack_current_mail(){
mail_handled = true;

citizen.signal.send( 'ACK-CURRENT-MAIL' );
citizen.hook.end();
}
});
}
6 changes: 6 additions & 0 deletions lib/modules/citizen/mail/log-unhandled-mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function( citizen ){

citizen.hook.add( 'unhandled-citizen-mail', 'log-unhandled-mail', function( envelope ){
console.log({ action: 'log-unhandled-mail', envelope: envelope });
});
}
23 changes: 1 addition & 22 deletions lib/modules/citizen/mail/receive-mail.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
var current_receive_callback,
setup_listener = false;

module.exports = function( citizen, config ){

if( !setup_listener ){

process.on( 'message', function( envelope ){

if( !envelope.type || envelope.type !== 'mail' ) return;
if( typeof current_receive_callback !== 'function' ) return;

current_receive_callback( envelope, function(){ citizen.signal.send( 'ACK-CURRENT-MAIL' ); } );
});

setup_listener = true;
}

citizen.mail.receive = function receive_mail( receive_callback ){

if( !current_receive_callback && ( !receive_callback || typeof receive_callback !== 'function' ) ) throw new Error( 'mail.receive requires a function to pass received messages to' );

if( receive_callback && typeof receive_callback === 'function' ) current_receive_callback = receive_callback;

citizen.mail.receive = function receive_mail(){
citizen.signal.send( 'READY-TO-RECEIVE-MAIL' );
}
}
3 changes: 1 addition & 2 deletions lib/modules/citizen/mail/send-mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ var merge = require('merge-objects');
module.exports = function( citizen, config ){

citizen.mail.send = function send_mail( settings, msg ){

if( !msg ){
if( ! msg ){
msg = settings;
settings = {};
}
Expand Down
15 changes: 15 additions & 0 deletions lib/modules/citizen/signal/detect-signal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function( citizen, config ){

citizen.hook.add( 'supervisor-ipc', 'detect-signal', function( details ){
if( ! details || ! details.hasOwnProperty( 'data' ) ) return;

var envelope = details.data;

if( Object.prototype.toString.call( envelope ) !== '[object Object]' ) return;
if( ! envelope.hasOwnProperty( 'type' ) ) return;
if( envelope.type !== 'signal' ) return;

citizen.hook.run( 'supervisor-signal', envelope );
citizen.hook.end();
});
}
32 changes: 0 additions & 32 deletions lib/modules/citizen/signal/receive-watch-ignore-signals.js

This file was deleted.

4 changes: 1 addition & 3 deletions lib/modules/citizen/signal/send-signal.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
module.exports = function( citizen, config ){

citizen.signal.send = function send_signal( signal, data ){

if( !signal ) return;
if( ! signal ) return;
if( typeof signal !== 'string' ) throw new Error( 'signal must be a string' );

var envelope = { type: 'signal', signal: signal };

if( data ) envelope.data = data;

process.send( envelope );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function( citizen ){

process.on( 'message', function( msg ){
citizen.hook.run( 'supervisor-ipc', { data: msg });
});
}
4 changes: 2 additions & 2 deletions lib/modules/citizen/supervision/get-citizen-name.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function( supe ){
supe.get_name = get_name;
module.exports = function( citizen ){
citizen.get_name = get_name;

function get_name(){
return process.env.SUPE_CITIZEN_NAME;
Expand Down
12 changes: 9 additions & 3 deletions lib/modules/citizen/supervision/handle-shutdown-signal.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
var shutdown_initiated = false,
shutdown_hook_running = false,
delays = 0;

module.exports = function( citizen, config ){

citizen.signal.watch( 'SUPE-SHUTDOWN', 'supe-initiate-shutdown', function(){
citizen.hook.add( 'supervisor-signal', 'handle-shutdown-request', function( envelope ){
if( ! envelope || ! envelope.signal || envelope.signal !== 'SUPE-SHUTDOWN' ) return;
if( shutdown_initiated ) return;
else shutdown_initiated = true;

shutdown_initiated = true;
shutdown_hook_running = true;

citizen.hook.run( 'shutdown', delay_shutdown );

shutdown_hook_running = false;
if( ! delays ) return process.exit();
});

Expand All @@ -18,6 +24,6 @@ module.exports = function( citizen, config ){

function clear_delay(){
delays -= 1;
if( ! delays ) process.exit();
if( ! shutdown_hook_running && ! delays ) process.exit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module.exports = function( citizen, noticeboard, config ){
citizen.noticeboard.ignore = ignore_wrapper;

function ignore_wrapper( notice, watcher ){

noticeboard.ignore( notice, watcher );

if( noticeboard.watchers[ notice ] && noticeboard.watchers[ notice ].length > 0 ) return;

citizen.signal.send( 'NOTICEBOARD-REQUEST', { type: 'ignore', notice: notice });
else citizen.signal.send( 'NOTICEBOARD-REQUEST', { type: 'ignore', notice: notice });
}
}
Original file line number Diff line number Diff line change
@@ -1,64 +1,43 @@
module.exports = function( citizen, noticeboard, config ){

process.on( 'message', function( envelope ){
citizen.hook.add( 'supervisor-signal', 'handle-noticeboard-responses', function( envelope ){
if( ! envelope || ! envelope.signal || envelope.signal !== 'NOTICEBOARD-RESPONSE' ) return;

if( !envelope || !envelope.type || envelope.type !== 'signal' ) return;
if( envelope.signal.indexOf( 'NOTICEBOARD' ) !== 0 ) return;
var data = envelope.data;
if( ! data.type ) return console.log({ action: 'handle-noticeboard-responses', success: false, error: 'no noticeboard action type found' });

switch( envelope.signal ){
switch( data.type ){

case 'NOTICEBOARD-RESPONSE':
case 'watch':
if( ! data.success ) citizen.noticeboard.pending[ data.notice ].callback( new Error( data.error || 'unidentified error' ) );
else citizen.noticeboard.pending[ data.notice ].callback();

var data = envelope.data;

if( !data.type ) return;

switch( data.type ){

case 'watch':

if( !data.success ) citizen.noticeboard.pending[ data.notice ].callback( new Error( data.error || 'unidentified error' ) );

else citizen.noticeboard.pending[ data.notice ].callback();

delete citizen.noticeboard.pending[ data.notice ];

noticeboard.notify( data.notice + '-pipe-installed' );

break;

case 'ignore':

if( !data.success ) throw new Error( data.error || 'attempt to ignore supervisor notice "' + data.notice + '" failed' );

break;

case 'notify':

if( !data.success ) throw new Error( data.error || 'attempt to send notice "' + data.notice + '" to supervisor failed' );

break;
}
delete citizen.noticeboard.pending[ data.notice ];

noticeboard.notify( data.notice + '-pipe-installed' );
break;

case 'NOTICEBOARD-EVENT':

var data = envelope.data;

if( !data.type ) return;

switch( data.type ){
case 'ignore':
if( ! data.success ) throw new Error( data.error || 'attempt to ignore supervisor notice "' + data.notice + '" failed' );
break;

case 'notice':
case 'notify':
if( ! data.success ) throw new Error( data.error || 'attempt to send notice "' + data.notice + '" to supervisor failed' );
break;
}
});

if( !data.notice ) return;
citizen.hook.add( 'supervisor-signal', 'handle-noticeboard-events', function( envelope ){
if( ! envelope || ! envelope.signal || envelope.signal !== 'NOTICEBOARD-EVENT' ) return;

noticeboard.notify( data.notice, data.msg.notice, 'supe' );
var data = envelope.data;
if( ! data.type ) return console.log({ action: 'handle-noticeboard-events', success: false, error: 'no noticeboard event type found' });

break;
}
switch( data.type ){

case 'notice':
if( ! data.notice ) return console.log({ action: 'handle-noticeboard-events', success: false, error: 'event notice name not found' });
else noticeboard.notify( data.notice, data.msg.notice, 'supe' );
break;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module.exports = function( citizen, noticeboard, config ){
citizen.noticeboard.notify = notify_wrapper;

function notify_wrapper( notice, message, source ){

citizen.signal.send( 'NOTICEBOARD-REQUEST', { type: 'notify', notice: notice, message: message, source: source });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module.exports = function( citizen, noticeboard, config ){
citizen.noticeboard.once = once_wrapper;

function once_wrapper( notice, watcher, callback, opts ){

if( !opts ) opts = {};
if( ! opts ) opts = {};

opts.once = true;

return citizen.noticeboard.watch( notice, watcher, callback, opts );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ module.exports = function( citizen, noticeboard, config ){
var method = opts && opts.once ? 'once' : 'watch';

if( method === 'once' ){

callback = function( real_callback ){

return function( msg ){

real_callback( msg );
citizen.noticeboard.ignore( notice, watcher );
}
Expand All @@ -21,7 +19,6 @@ module.exports = function( citizen, noticeboard, config ){
// if there is an active watcher, pipe is still installed
// -- do a normal watch
if( noticeboard.watchers[ notice ] && noticeboard.watchers[ notice ].length > 0 ){

noticeboard[ method ]( notice, watcher, callback, opts );
}

Expand All @@ -30,7 +27,6 @@ module.exports = function( citizen, noticeboard, config ){
else if( citizen.noticeboard.pending[ notice ] ){

noticeboard.once( notice + '-pipe-installed', 'pipe-to-' + watcher, function(){

noticeboard[ method ]( notice, watcher, callback, opts );
});
}
Expand All @@ -40,16 +36,13 @@ module.exports = function( citizen, noticeboard, config ){
else {

citizen.noticeboard.pending[ notice ] = {

type: 'watch',
notice: notice,
opts: opts,

callback: function( error ){

if( error ) throw error;

noticeboard[ method ]( notice, watcher, callback, opts );
else noticeboard[ method ]( notice, watcher, callback, opts );
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function( supervisor, citizen, envelope ){
citizen.mail.inbox.push( envelope );
supervisor.hook.run( 'mail-added-to-inbox', { citizen: citizen.name });
}
6 changes: 6 additions & 0 deletions lib/modules/supervisor/mail/actions/requeue-current-mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function( supervisor, citizen ){
if( ! citizen.state.current_mail ) return;

citizen.mail.inbox.splice( 0, 0, citizen.state.current_mail );
citizen.state.current_mail = null;
}