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

Exponential backoff implementation #105

Closed
wants to merge 4 commits into from
Closed
Changes from 3 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
45 changes: 32 additions & 13 deletions src/Supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class Supervisor {
var self = this;

while (true) {
// console.log("jdebug enter loop");
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
// await sleep(4000);
// get ssh connector from pool
var ssh: SSH;
if (job.maintainerInstance.connector.config.is_community_account) {
Expand All @@ -136,21 +139,35 @@ class Supervisor {
ssh = connectionPool[job.id].ssh;
}

var exit = false;
var wait = 2;

// connect ssh & run
try {
if (!ssh.connection.isConnected())
await ssh.connection.connect(ssh.config);
await ssh.connection.execCommand("echo"); // test connection
if (job.maintainerInstance.isInit) {
await job.maintainerInstance.maintain();
} else {
await job.maintainerInstance.init();
while (!exit && !ssh.connection.isConnected()) {
if (wait > 100) {
console.log("jdebug bad");
exit = true;
continue;
}
try {
console.log("jdebug ok");
await sleep(wait * 1000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await sleep will block the execution of the job as they are executed sequentially. A more appropriate method might be to wrap the createMaintianerWorker at line 113 this.createMaintainerWorker(job); within a Promise, use setTimeout as the timer, and interrupt the execution using reject:

   new Promise((resolve, reject) => {
        console.log("Function starts");

        setTimeout(() => {
            console.log("setTimeout callback runs");
            reject('Interrupted by setTimeout');
        }, 1000);

        // Simulate some work
        for (let i = 0; i < 5; i++) {
            console.log(`Processing ${i}`);
        }

        resolve('Completed successfully');
    });

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you can use this to emit a failure event:

          self.emitter.registerEvents(
            job,
            "JOB_REGISTERED",
            `job [${job.id}] is registered with the supervisor, waiting for initialization`
          );

if (!ssh.connection.isConnected()) {
await ssh.connection.connect(ssh.config);
}
await ssh.connection.execCommand("echo"); // test connection
if (job.maintainerInstance.isInit) {
await job.maintainerInstance.maintain();
} else {
await job.maintainerInstance.init();
}
wait = wait * wait;
console.log("jdebug success");
} catch (e) {
wait = wait * wait;
if (config.is_testing) console.error(e.stack);
}
} catch (e) {
if (config.is_testing) console.error(e.stack);
continue;
}

// emit events & logs
var events = job.maintainerInstance.dumpEvents();
var logs = job.maintainerInstance.dumpLogs();
Expand All @@ -175,7 +192,8 @@ class Supervisor {
}

// ending conditions
if (job.maintainerInstance.isEnd) {
if (job.maintainerInstance.isEnd || exit) {
console.log("jdebug exit");
// exit or deflag ssh pool
if (job.maintainerInstance.connector.config.is_community_account) {
connectionPool[job.hpc].counter--;
Expand Down Expand Up @@ -252,3 +270,4 @@ class Supervisor {
}

export default Supervisor;

Loading