-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from NetsBlox/matlab-timeout
Keep matlab cluster "warm" (prevent worker release)
- Loading branch information
Showing
2 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const { setTimeout, setInterval, clearInterval } = require("../../timers"); | ||
const seconds = 1000; | ||
const minutes = 60 * seconds; | ||
|
||
/** | ||
* This is made to "keep the matlab cluster warm" and prevent worker nodes from being | ||
* released back to the cluster. This class takes an action and, once started, call it | ||
* on a given interval for a specified duration. | ||
* | ||
* Essentially, it is a stateful setInterval w/ an end duration that restarts on each call | ||
* | ||
* Please use responsibly. | ||
*/ | ||
class KeepWarm { | ||
constructor(action, interval = 10 * seconds) { | ||
this.action = action; | ||
this.interval = interval; | ||
this.currentInterval = null; | ||
} | ||
|
||
async keepWarm(duration = 15 * minutes) { | ||
this.stop(); | ||
const intervalId = setInterval(this.action, this.interval); | ||
this.currentInterval = intervalId; | ||
setTimeout(() => { | ||
if (this.currentInterval === intervalId) { | ||
this.stop(); | ||
} | ||
}, duration); | ||
} | ||
|
||
stop() { | ||
if (this.currentInterval) { | ||
clearInterval(this.currentInterval); | ||
this.currentInterval = null; | ||
} | ||
} | ||
|
||
isStillWarm() { | ||
return !!this.currentInterval; | ||
} | ||
} | ||
|
||
module.exports = KeepWarm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters