ChildProcessWorker.d.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /// <reference types="node" />
  2. /**
  3. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. /// <reference types="node" />
  9. import { ChildMessage, OnEnd, OnStart, WorkerInterface, WorkerOptions } from '../types';
  10. /**
  11. * This class wraps the child process and provides a nice interface to
  12. * communicate with. It takes care of:
  13. *
  14. * - Re-spawning the process if it dies.
  15. * - Queues calls while the worker is busy.
  16. * - Re-sends the requests if the worker blew up.
  17. *
  18. * The reason for queueing them here (since childProcess.send also has an
  19. * internal queue) is because the worker could be doing asynchronous work, and
  20. * this would lead to the child process to read its receiving buffer and start a
  21. * second call. By queueing calls here, we don't send the next call to the
  22. * children until we receive the result of the previous one.
  23. *
  24. * As soon as a request starts to be processed by a worker, its "processed"
  25. * field is changed to "true", so that other workers which might encounter the
  26. * same call skip it.
  27. */
  28. export default class ChildProcessWorker implements WorkerInterface {
  29. private _child;
  30. private _options;
  31. private _request;
  32. private _retries;
  33. private _onProcessEnd;
  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): 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. }