Skip to content

sdi: Add support for 'sendMainPid' and 'extendTimeout' shared object functions #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/sdi.q
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@
.sdi.cfg.soFunctionMap[`getInterval]: 1;
.sdi.cfg.soFunctionMap[`sendWatchdog]: 1;
.sdi.cfg.soFunctionMap[`sendStatus]: 1;
.sdi.cfg.soFunctionMap[`sendMainPid]: 1;
.sdi.cfg.soFunctionMap[`extendTimeout]: 1;

/ The list of supported OS types for 'systemd' integration
.sdi.cfg.systemdSupportedOs:`l`v;


/ Logs the 'systemd' state transitions based on the functions called in this library
/ @see .sdi.i.sendReady
/ @see .sdi.sendStopping
.sdi.state:`state xkey flip `state`transitionAt!"SP"$\:();


.sdi.init:{
if[not .os.type in .sdi.cfg.systemdSupportedOs;
.log.if.info "systemd is not supported on the current OS [ OS: ",string[.os.type]," ]";
:(::);
];

.sdi.i.loadNativeFunctions[];

`.sdi.state upsert (`starting; .time.now[]);
};


Expand All @@ -47,6 +57,8 @@
/ @see .sdi.so.sendStopping
.sdi.sendStopping:{
.log.if.debug "Notifying systemd that kdb application is stopping";
`.sdi.state upsert (`stopping; .time.now[]);

.sdi.so.sendStopping[];
};

Expand All @@ -58,6 +70,30 @@
.sdi.so.sendStatus status;
};

/ Sends the main PID of the application to systemd
/ @param pid (Integer) Null to send the current pid ('.z.i') or a PID if a different 'primary' process
/ @see .sdi.so.sendMainPid
.sdi.sendMainPid:{[pid]
$[null pid;
pid:.z.i;
not .type.isInteger pid;
'"IllegalArgumentException"
];

.log.if.debug ("Sending main PID to systemd [ PID: {} ]"; pid);
.sdi.so.sendMainPid pid;
};

.sdi.extendTimeout:{[extension]
if[not .type.isTimespan extension;
'"IllegalArgumentException";
];

currentState:last[0!.sdi.state]`state;

.log.if.info ("Extending systemd timeout [ Current State: {} ] [ Extension: {} ]"; currentState; extension);
.sdi.so.extendTimeout currentState;
};

/ Binds the stopping notification to the process.exit event
/ @see .sdi.sendStopping
Expand All @@ -83,6 +119,8 @@
/ @see .sdi.so.sendReady
.sdi.i.sendReady:{
.log.if.info "Notifying systemd that kdb application is ready";
`.sdi.state upsert (`ready; .time.now[]);

.sdi.so.sendReady[];
};

Expand Down