ChildProcessWorker.d.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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, 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 _fakeStream;
  34. private _stdout;
  35. private _stderr;
  36. private _exitPromise;
  37. private _resolveExitPromise;
  38. constructor(options: WorkerOptions);
  39. initialize(): void;
  40. private _shutdown;
  41. private _onMessage;
  42. private _onExit;
  43. send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd): void;
  44. waitForExit(): Promise<void>;
  45. forceExit(): void;
  46. getWorkerId(): number;
  47. getStdout(): NodeJS.ReadableStream | null;
  48. getStderr(): NodeJS.ReadableStream | null;
  49. private _getFakeStream;
  50. }