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 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
41 changes: 28 additions & 13 deletions src/Supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Supervisor {

async createMaintainerWorker(job: Job) {
var self = this;

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
while (true) {
// get ssh connector from pool
var ssh: SSH;
Expand All @@ -136,21 +136,36 @@ class Supervisor {
ssh = connectionPool[job.id].ssh;
}

var exit = false;
var wait = 0;

// 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) {
exit = true;
self.emitter.registerEvents(
job,
"JOB_FAILED",
`job [${job.id}] failed because the HPC could not connect within the allotted time`
);
continue;
}
} catch (e) {
if (config.is_testing) console.error(e.stack);
continue;
try {
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
} catch (e) {
if (config.is_testing) console.error(e.stack);
}
wait = wait == 0 ? 2 : wait * wait;
}
if (job.maintainerInstance.isInit) {
await job.maintainerInstance.maintain();
} else {
await job.maintainerInstance.init();
}

// emit events & logs
var events = job.maintainerInstance.dumpEvents();
var logs = job.maintainerInstance.dumpLogs();
Expand Down
Loading