ChildProcessWorker.d.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. /// <reference types="node" />
  8. import { ChildMessage, OnCustomMessage, OnEnd, OnStart, WorkerInterface, WorkerOptions } from '../types';
  9. /**
  10. * This class wraps the child process and provides a nice interface to
  11. * communicate with. It takes care of:
  12. *
  13. * - Re-spawning the process if it dies.
  14. * - Queues calls while the worker is busy.
  15. * - Re-sends the requests if the worker blew up.
  16. *
  17. * The reason for queueing them here (since childProcess.send also has an
  18. * internal queue) is because the worker could be doing asynchronous work, and
  19. * this would lead to the child process to read its receiving buffer and start a
  20. * second call. By queueing calls here, we don't send the next call to the
  21. * children until we receive the result of the previous one.
  22. *
  23. * As soon as a request starts to be processed by a worker, its "processed"
  24. * field is changed to "true", so that other workers which might encounter the
  25. * same call skip it.
  26. */
  27. export default class ChildProcessWorker implements WorkerInterface {
  28. private _child;
  29. private _options;
  30. private _request;
  31. private _retries;
  32. private _onProcessEnd;
  33. private _onCustomMessage;
  34. private _fakeStream;
  35. private _stdout;
  36. private _stderr;
  37. private _exitPromise;
  38. private _resolveExitPromise;
  39. constructor(options: WorkerOptions);
  40. initialize(): void;
  41. private _shutdown;
  42. private _onMessage;
  43. private _onExit;
  44. send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd, onCustomMessage: OnCustomMessage): void;
  45. waitForExit(): Promise<void>;
  46. forceExit(): void;
  47. getWorkerId(): number;
  48. getStdout(): NodeJS.ReadableStream | null;
  49. getStderr(): NodeJS.ReadableStream | null;
  50. private _getFakeStream;
  51. }