Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain hooks multithreaded execution on the SERVER page #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion content/en/SERVER.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,31 @@ const file = application.resources.get(`/fileName.exe`);

## Start

You can create `start` hook by putting `start.js` file to `application/domain`, `application/db`, or `application/lib`:
You can create `start` hook by putting `start.js` file to `application/domain`, `application/db`, `application/lib` or any inner nested folders there:

```js
async () => {
console.log('Code to be executed after start');
};
```

Note that due to multithreaded nature of Metarhia application server, every `start` or `stop` hook by default will be executed in every worker thread established based on configuration. Sometimes it's necessary to run some hook or part of it's logic only at specific thread or except some threads, e.g. in task threads but not threads with opened network port for API. To check the current executing thread there is exposed property `application.worker.id` available from any place of application. It contains value in format `W1`, where the number is counted index of specific worker thread. Probably you had already familiar with workers IDs by looking into application log where every record has explicit mark at what thread it happened. Using `application.worker.id` you may place simple guard or conditional blocks inside of `start` and `stop` hooks, for example:

```js
if (application.worker.id !== 'W1') return;
// The following code will run in the first thread only
```

```js
if (application.worker.id === 'W2') {
// Run this code in the second thread only
}
if (application.worker.id === 'W4') {
// Run that different code in the 4th thread only
}
// All the following code will still be executed in every thread
```

## Graceful shutdown

Application server will stop after:
Expand Down