-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprotection-manager.js
123 lines (110 loc) · 4.82 KB
/
protection-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import got from 'got';
import { EventEmitter } from 'node:events';
// This class manages the task protection state. It implements basic
// rate limiting, and emits events to let you know when it changes state.
// you can await the acquire() and release() methods if you want to ensure
// that task protection has reached the desired state before moving on.
export default class ProtectionManager extends EventEmitter {
/**
* @constructor
* @param {*} protectionSettings
* @param {*} protectionSettings.desiredProtectionDurationInMins - How long in minutes to protect the process on calling the acquire method
* @param {*} protectionSettings.maintainProtectionPercentage - Number between 0 and 100 that expresses percentage of desired protection to maintain if release is called early
* @param {*} protectionSettings.refreshProtectionPercentage - Number between 0 and 100 that expresses percentage of desired protection duration to let pass before doing an early refresh
* @param {*} protectionSettings.protectionAdjustIntervalInMs - How frequently in ms to attempt/verify state matches desire
*/
constructor(protectionSettings) {
super();
this.desiredProtectionDurationInMins = protectionSettings.desiredProtectionDurationInMins;
this.protectionAdjustIntervalInMs = protectionSettings.protectionAdjustIntervalInMs;
this.maintainProtectionPercentage = protectionSettings.maintainProtectionPercentage;
this.refreshProtectionPercentage = protectionSettings.refreshProtectionPercentage;
this.ECS_AGENT_URI = process.env.ECS_AGENT_URI;
if (!this.ECS_AGENT_URI) {
throw new Error('ECS_AGENT_URI environment variable must be set. This is set automatically in an ECS task environment');
}
this.desiredState = 'unprotected';
this.currentState = 'unprotected';
this.lastStateChange = new Date().getTime();
this.interval = setInterval(this.attemptAdjustProtection.bind(this), protectionSettings.protectionAdjustIntervalInMs);
}
attemptAdjustProtection = async function () {
if (this.currentState == 'unprotected' &&
this.desiredState == 'unprotected') {
// Already unprotected so nothing to do right now.
this.emit(this.currentState);
return;
}
var now = new Date().getTime();
var timeSinceLastChange = now - this.lastStateChange;
var timeUntilProtectRefresh = this.desiredProtectionDurationInMins * 60 * 1000 * (this.refreshProtectionPercentage / 100);
var timeUntilProtectRelease = this.desiredProtectionDurationInMins * 60 * 1000 * (this.maintainProtectionPercentage / 100);
if (this.currentState == 'protected' &&
this.desiredState == 'protected' &&
timeSinceLastChange < timeUntilProtectRefresh) {
// We are already protected and haven't yet reached 80% of the acquired protection duration
// so no need to do an early refresh.
this.emit(this.currentState);
return;
}
if (this.currentState == 'protected' &&
this.desiredState == 'unprotected' &&
timeSinceLastChange < timeUntilProtectRelease) {
// We are currently protected and not enough duration has passed since we became protected
// so don't actually release the protection yet, maintain it for now.
this.emit(this.currentState);
return;
}
var ecsAgentParams;
if (this.desiredState == 'unprotected') {
ecsAgentParams = {
ProtectionEnabled: false
};
} else if (this.desiredState == 'protected') {
ecsAgentParams = {
ProtectionEnabled: true,
ExpiresInMinutes: this.desiredProtectionDurationInMins
};
}
try {
await got(`${this.ECS_AGENT_URI}/task-protection/v1/state`, {
method: 'PUT',
json: ecsAgentParams
});
} catch (e) {
return this.emit('rejected', e);
}
this.lastStateChange = new Date().getTime();
this.currentState = this.desiredState;
this.emit(this.currentState);
}
/**
* Set the desired state to protected and wait for protection to be successfully acquired
*/
acquire = async function () {
var self = this;
this.desiredState = 'protected';
return new Promise(function (resolve, reject) {
self.once('protected', resolve);
self.attemptAdjustProtection(); // Immediate attempt to make an adjustment
});
}
/**
* Set the desired state to unprotected and wait for protection to be successfully released
*/
release = async function () {
var self = this;
this.desiredState = 'unprotected';
return new Promise(function (resolve, reject) {
self.once('unprotected', resolve);
self.attemptAdjustProtection(); // Immediate attempt to make an adjustment
});
}
/**
* When it is time to stop the process this clears
* the interval so that it no longer keeps the event loop alive.
*/
close = function () {
clearInterval(this.interval);
}
}