Skip to content

Commit a9843e0

Browse files
PierreDemaillyRossb0b
authored andcommitted
feat: add environment variables (#137)
* feat: add environment variables
1 parent 7c2a0a2 commit a9843e0

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ if (isDeleteOperation(event.operation)) {
132132
}
133133
```
134134

135+
### Environment Variables
136+
137+
> [!IMPORTANT]
138+
> Some options takes the lead over environment variables. For instance with: `new Incomer({ dispatcherInactivityOptions: { maxPingInterval: 900_000 }})` the max ping interval will be `900_000` even if `MYUNISOFT_INCOMER_MAX_PING_INTERVAL` variable is set.
139+
140+
| variable | description | default |
141+
| --- | --- | --- |
142+
| `MYUNISOFT_INCOMER_INIT_TIMEOUT` | Incomer initialisation timeout | `3_500` |
143+
| `MYUNISOFT_EVENTS_INIT_EXTERNAL` | Weither Incomer should initialise an external Dispatcher | `false` |
144+
| `MYUNISOFT_EVENTS_SILENT_LOGGER` | Disable logs | `false` |
145+
| `MYUNISOFT_INCOMER_MAX_PING_INTERVAL` | Maximum ping interval | `60_000` |
146+
| `MYUNISOFT_INCOMER_PUBLISH_INTERVAL` | Publish interval | `60_000` |
147+
| `MYUNISOFT_INCOMER_IS_DISPATCHER` | Weither Incomer is a Dispatcher | `false` |
148+
| `MYUNISOFT_DISPATCHER_IDLE_TIME` | Interval threshold when Dispatcher become idle | `600_000` |
149+
| `MYUNISOFT_DISPATCHER_CHECK_LAST_ACTIVITY_INTERVAL` | Dispatcher checking last activity interval | `120_000` |
150+
| `MYUNISOFT_DISPATCHER_BACKUP_TRANSACTION_STORE_NAME` | Default name for backup transaction store | `backup` |
151+
| `MYUNISOFT_DISPATCHER_INIT_TIMEOUT` | Dispatcher initialisation timeout | `3_500` |
152+
| `MYUNISOFT_DISPATCHER_PING_INTERVAL` | Dispatcher ping interval | `3_500` |
153+
135154
### API
136155

137156
#### Dispatcher & Incomer class

src/class/eventManagement/dispatcher.class.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ import { TransactionHandler } from "./dispatcher/transaction-handler.class";
4242

4343
// CONSTANTS
4444
const ajv = new Ajv();
45-
const kIdleTime = 60_000 * 10;
46-
const kCheckLastActivityInterval = 60_000 * 2;
47-
const kCheckRelatedTransactionInterval = 60_000 * 3;
48-
const kBackupTransactionStoreName = "backup";
49-
const kSilentLogger = process.env.MYUNISOFT_EVENTS_SILENT_LOGGER || false;
50-
export const PING_INTERVAL = 60_000 * 5;
45+
const kIdleTime = Number(process.env.MYUNISOFT_DISPATCHER_IDLE_TIME ?? 60_000 * 10);
46+
const kCheckLastActivityInterval = Number(process.env.MYUNISOFT_DISPATCHER_CHECK_LAST_ACTIVITY_INTERVAL ?? 60_000 * 2);
47+
const kCheckRelatedTransactionInterval = Number(process.env.MYUNISOFT_DISPATCHER_RESOLVE_TRANSACTION_INTERVAL ?? 60_000 * 3);
48+
const kBackupTransactionStoreName = String(process.env.MYUNISOFT_DISPATCHER_BACKUP_TRANSACTION_STORE_NAME ?? "backup");
49+
const kSilentLogger = Boolean(process.env.MYUNISOFT_EVENTS_SILENT_LOGGER || false);
50+
const kMaxInitTimeout = Number(process.env.MYUNISOFT_DISPATCHER_INIT_TIMEOUT ?? 3_500);
51+
export const PING_INTERVAL = Number(process.env.MYUNISOFT_DISPATCHER_PING_INTERVAL ?? 60_000 * 5);
5152

5253
export type DispatcherOptions<T extends GenericEvent = GenericEvent> = {
5354
/* Prefix for the channel name, commonly used to distinguish envs */
@@ -139,7 +140,7 @@ export class Dispatcher<T extends GenericEvent = GenericEvent> extends EventEmit
139140
private minTimeout = 0;
140141
// Arbitrary value according to fastify default pluginTimeout
141142
// Max timeout is 8_000, but u may init both an Dispatcher & an Incomer
142-
private maxTimeout = 3_500;
143+
private maxTimeout = kMaxInitTimeout;
143144

144145
private eventsValidationFn: Map<string, ValidateFunction<Record<string, any>> | CustomEventsValidationFunctions>;
145146
private validationCbFn: (event: T) => void = null;

src/class/eventManagement/incomer.class.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "@myunisoft/redis";
1111
import { Logger, pino } from "pino";
1212
import { P, match } from "ts-pattern";
13+
import { ValidateFunction } from "ajv";
1314

1415
// Import Internal Dependencies
1516
import {
@@ -39,14 +40,16 @@ import {
3940
defaultStandardLog
4041
} from "../../utils/index";
4142
import { Externals } from "./externals.class";
42-
import { ValidateFunction } from "ajv";
4343

4444
// CONSTANTS
4545
// Arbitrary value according to fastify default pluginTimeout
4646
// Max timeout is 8_000, but u may init both an Dispatcher & an Incomer
47-
const kDefaultStartTime = 3_500;
48-
const kExternalInit = process.env.MYUNISOFT_EVENTS_INIT_EXTERNAL || false;
49-
const kSilentLogger = process.env.MYUNISOFT_EVENTS_SILENT_LOGGER || false;
47+
const kDefaultStartTime = Number(process.env.MYUNISOFT_INCOMER_INIT_TIMEOUT ?? 3_500);
48+
const kExternalInit = Boolean(process.env.MYUNISOFT_EVENTS_INIT_EXTERNAL ?? false);
49+
const kSilentLogger = Boolean(process.env.MYUNISOFT_EVENTS_SILENT_LOGGER ?? false);
50+
const kMaxPingInterval = Number(process.env.MYUNISOFT_INCOMER_MAX_PING_INTERVAL ?? 60_000);
51+
const kPublishInterval = Number(process.env.MYUNISOFT_INCOMER_PUBLISH_INTERVAL ?? 60_000);
52+
const kIsDispatcherInstance = Boolean(process.env.MYUNISOFT_INCOMER_IS_DISPATCHER ?? false);
5053

5154
type DispatcherChannelEvents = { name: "approvement" };
5255
type IncomerChannelEvents<
@@ -101,7 +104,7 @@ export class Incomer <
101104
public baseUUID = randomUUID();
102105

103106
private prefixedName: string;
104-
private isDispatcherInstance?: boolean;
107+
private isDispatcherInstance: boolean;
105108
private registerTransactionId: string | null;
106109
private eventsCast: EventCast[];
107110
private eventsSubscribe: EventSubscribe[];
@@ -132,8 +135,11 @@ export class Incomer <
132135
this.prefixedName = `${this.prefix ? `${this.prefix}-` : ""}`;
133136
this.dispatcherChannelName = this.prefixedName + channels.dispatcher;
134137
this.standardLogFn = options.standardLog ?? defaultStandardLog;
135-
this.publishInterval = options.dispatcherInactivityOptions?.publishInterval ?? 60_000;
136-
this.maxPingInterval = options.dispatcherInactivityOptions?.maxPingInterval ?? 60_000;
138+
this.publishInterval = options.dispatcherInactivityOptions?.publishInterval ?? kPublishInterval;
139+
this.maxPingInterval = options.dispatcherInactivityOptions?.maxPingInterval ?? kMaxPingInterval;
140+
if (this.isDispatcherInstance === undefined) {
141+
this.isDispatcherInstance = kIsDispatcherInstance;
142+
}
137143

138144
if (options.eventsValidation) {
139145
this.eventsValidationFn = options.eventsValidation.eventsValidationFn;
@@ -169,7 +175,6 @@ export class Incomer <
169175
private async checkDispatcherState() {
170176
if (this.lastPingDate + this.maxPingInterval < Date.now()) {
171177
this.dispatcherIsAlive = false;
172-
this.isDispatcherInstance = false;
173178

174179
return;
175180
}

0 commit comments

Comments
 (0)