Module basic module

module process

Process processing module, used to manage the resources of the current process

Reference method:

1
var process = require('process');

process events

The process module object isEventEmitterInstances can respond to process-level events by registering event listeners.

beforeExit event

When the fibjs task is empty and no additional work has been added, the event beforeExitwill be triggered

1
process.on('beforeExit', exitCode => {});

Normally, if no additional work is added to the task queue, the fibjs process will end. But if beforeExita new task is started in the callback function of the event-bound listener, such as starting a fiber, then the fibjs process will continue to run.

process.exitCodePassed as the only parameter value to beforeExitthe event listener's callback function. If the process is about to terminate for explicit reasons, such as directly callingprocess.exitOr throw an uncaught exception and beforeExitthe event will not be triggered.

exit event

When fibjs exits, the event exitwill be triggered. Once all exitlisteners bound to the event have completed execution, the process will terminate.

1
process.on('exit', exitCode => {});

exitThe callback function of the event listener has only one input parameter. The value of this parameter can beprocess.exitCodeattribute value, or callprocess.exitThe value passed in to the method exitCode.

Signal event

When the fibjs process receives a signal, a signal event will be triggered. Currently supported signals are SIGINT and SIGTERM. Each event name is expressed in uppercase letters of the signal name (for example, the event 'SIGINT' corresponds to the signal SIGINT).

Signal events are different from other process events. Signal events are preempted. When a signal occurs, no matter what the currentioOperations, whether JavaScript operations, will trigger the corresponding event as soon as possible. For example, you can use the following code to interrupt the current application and output the running status:

1 2 3 4 5 6
var coroutine = require('coroutine'); process.on('SIGINT', () => { coroutine.fibers.forEach(f => console.error("Fiber %d:\n%s", f.id, f.stack)); process.exit(); });

The signal names and their meanings are as follows:

  • SIGINT: When running in the terminal, it can be supported by all platforms and can usually be triggered by CTRL+C.
  • SIGTERM: This signal is triggered when the process is killed. Not supported under Windows.

static function

umask

Change the current umask, Windows does not support this method

1
static Integer process.umask(Integer mask);

Call parameters:

  • mask: Integer, specify new mask

Return results:

  • Integer, return to the previous mask

Change the current umask, Windows does not support this method

1
static Integer process.umask(String mask);

Call parameters:

  • mask: String, specifies the new mask, string type octal (eg: "0664")

Return results:

  • Integer, return to the previous mask

Returns the current umask. Windows does not support this method.

1
static Integer process.umask();

Return results:

  • Integer, returns the current mask value

hrtime

Returns the system's high-precision time. This time has nothing to do with the current time and is only used for high-precision timing.

1
static Array process.hrtime(Array diff = []);

Call parameters:

  • diff: Array, initial time used for comparison

Return results:

  • Array, returns the timing time in the format [seconds, nanoseconds]

exit

Exit the current process and return exitCode as the process result

1
static process.exit();

Exit the current process and return the result

1
static process.exit(Integer code);

Call parameters:

  • code: Integer, returns the process result

cwd

Returns the current working path of the operating system

1
static String process.cwd();

Return results:

  • String, returns the current system path

dlopen

Dynamically loading C++ Addons

1 2 3
static process.dlopen(Object module, String filename, Integer flags = 1);

Call parameters:

  • module: Object, specifies the module to be loaded
  • filename: String, specifies the module file name to be loaded
  • flags: Integer, specifies the way to load the module, the default is 1

chdir

Modify the current working path of the operating system

1
static process.chdir(String directory);

Call parameters:

  • directory: String, specify the new path of the setting

uptime

Query the running time of the running environment, in seconds

1
static Number process.uptime();

Return results:

  • Number, returns a numerical value representing time

cpuUsage

Query the time spent by the current process in user and system code, the value is a microsecond value (millionths of a second)

1
static Object process.cpuUsage(Object previousValue = {});

Call parameters:

  • previousValue: Object, specifies the time of the last query

Return results:

  • Object, returns a report containing the time

The memory report produces results similar to the following:

1 2 3 4
{ "user": 132379, "system": 50507 }

in:

  • user returns the time the process spent in user code
  • system returns the time the process spent in system code

memoryUsage

Query the memory usage report of the current process

1
static Object process.memoryUsage();

Return results:

  • Object, returns a report containing memory

The memory report produces results similar to the following:

1 2 3 4 5
{ "rss": 8622080, "heapTotal": 4083456, "heapUsed": 1621800 }

in:

  • rss returns the current physical memory size occupied by the process
  • heapTotal returns the v8 engine heap memory size
  • heapUsed returns the heap memory size being used by the v8 engine

nextTick

start a fiber

1 2
static process.nextTick(Function func, ...args);

Call parameters:

  • func: Function, specify the function executed by the fiber
  • args: ..., a variadic sequence of arguments that will be passed to the function within the fiber

binding

Get the internal module with the specified name

1
static Value process.binding(String name);

Call parameters:

  • name: String, specifies the internal module name to be queried

Return results:

  • Value, returns the specified internal module

getgid

Query the group id of the current process

1
static Integer process.getgid();

Return results:

  • Integer, returns the group id of the current process

getuid

Query the user ID of the current process

1
static Integer process.getuid();

Return results:

  • Integer, returns the user id of the current process

setgid

Set the group id of the current process

1
static process.setgid(Integer id);

Call parameters:

  • id: Integer, specify the group id to be set

setuid

Set the user id of the current process

1
static process.setuid(Integer id);

Call parameters:

  • id: Integer, specify the user ID to be set

emitWarning

Issue custom or application-specific process warnings. These events can be listened to by adding a handler to the 'warning' event

1 2
static process.emitWarning(Value warning, Object options);

Call parameters:

  • warning: Value, specifies the warning to be issued
  • options: Object, options to specify warnings

    Options include the following:

1 2 3 4 5
{ "type": "Warning", // specifies the name of the type of warning issued. Default value: 'Warning' "code": "", // specify the unique identifier of the warning instance issued "detail": "" // specify additional text for warnings }

How to use it:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
const { emitWarning } = require('process'); // Emit a warning with a code and additional detail. emitWarning('Something happened!', { code: 'MY_WARNING', detail: 'This is some additional information', }); process.on('warning', (warning) => { console.warn(warning.name); // 'Warning' console.warn(warning.message); // 'Something happened!' console.warn(warning.code); // 'MY_WARNING' console.warn(warning.stack); // Stack trace console.warn(warning.detail); // 'This is some additional information' });

Issue custom or application-specific process warnings. These events can be listened to by adding a handler to the 'warning' event

1 2 3
static process.emitWarning(Value warning, String type = "Warning", String code = "");

Call parameters:

  • warning: Value, specifies the warning to be issued
  • type: String, specifies the name of the type of warning to be issued. Default: 'Warning'
  • code: String, specifies the unique identifier of the warning instance issued

disconnect

Close the ipc pipe to the parent process

1
static process.disconnect();

send

Send a message to the parent process

1
static process.send(Value msg);

Call parameters:

  • msg: Value, specifies the message to send

static properties

argv

Array, returns the command line parameters of the current process

1
static readonly Array process.argv;

execArgv

Array, returns the special command line parameters of the current process. These parameters are used by fibjs to set the running environment.

1
static readonly Array process.execArgv;

version

String, returns the fibjs version string

1
static readonly String process.version;

versions

Object, returns the version information of fibjs and components

1
static readonly Object process.versions;

execPath

String, query the full path of the currently running execution file

1
static readonly String process.execPath;

env

Object, query the environment variables of the current process

1
static readonly Object process.env;

arch

String, query the current CPU environment, possible results are 'amd64', 'arm', 'arm64', 'ia32'

1
static readonly String process.arch;

platform

String, query the current platform name, possible results are 'darwin', 'freebsd', 'linux', or 'win32'

1
static readonly String process.platform;

pid

Integer, read the id of the process pointed to by the current object

1
static readonly Integer process.pid;

ppid

Integer, read the id of the parent process pointed to by the current object

1
static readonly Integer process.ppid;

stdin

Stream, query the standard input object of the current process, inttyZhongweiTTYInputStream, otherwiseStream

1
static readonly Stream process.stdin;

stdout

Stream, query the standard output object of the current process, inttyZhongweiTTYOutputStream, otherwiseStream

1
static readonly Stream process.stdout;

stderr

Stream, query the current process standard error output object, inttyZhongweiTTYOutputStream, otherwiseStream

1
static readonly Stream process.stderr;

exitCode

Integer, query and set the exit code of the current process

1
static Integer process.exitCode;

connected

Boolean, query whether the pipe with the parent process is connected normally

1
static readonly Boolean process.connected;