main-base.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * Wrapper for sandboxing.
  5. *
  6. */
  7. const child_processor_1 = require("./child-processor");
  8. const enums_1 = require("../enums");
  9. const utils_1 = require("../utils");
  10. exports.default = (send, receiver) => {
  11. const childProcessor = new child_processor_1.ChildProcessor(send, receiver);
  12. receiver === null || receiver === void 0 ? void 0 : receiver.on('message', async (msg) => {
  13. try {
  14. switch (msg.cmd) {
  15. case enums_1.ChildCommand.Init:
  16. await childProcessor.init(msg.value);
  17. break;
  18. case enums_1.ChildCommand.Start:
  19. await childProcessor.start(msg.job, msg === null || msg === void 0 ? void 0 : msg.token);
  20. break;
  21. case enums_1.ChildCommand.Stop:
  22. break;
  23. case enums_1.ChildCommand.Cancel:
  24. childProcessor.cancel(msg.value);
  25. break;
  26. }
  27. }
  28. catch (err) {
  29. console.error('Error handling child message');
  30. }
  31. });
  32. process.on('SIGTERM', () => childProcessor.waitForCurrentJobAndExit());
  33. process.on('SIGINT', () => childProcessor.waitForCurrentJobAndExit());
  34. process.on('uncaughtException', async (err) => {
  35. if (typeof err !== 'object') {
  36. err = new Error((0, utils_1.toString)(err));
  37. }
  38. await send({
  39. cmd: enums_1.ParentCommand.Failed,
  40. value: (0, utils_1.errorToJSON)(err),
  41. });
  42. // An uncaughException leaves this process in a potentially undetermined state so
  43. // we must exit
  44. process.exit();
  45. });
  46. };
  47. //# sourceMappingURL=main-base.js.map