Advertisement
yurystanev

Untitled

Nov 8th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   async restore() {
  2.     debug('restore');
  3.     try {
  4.       if (fs.existsSync(process.env.PERSISTENCE_FILE)) {
  5.         debug('File exists at ', process.env.PERSISTENCE_FILE);
  6.         // tasks = {}
  7.         this.tasks = JSON.parse(fs.readFileSync(process.env.PERSISTENCE_FILE));
  8.         debug('persistance file is Loaded\n', 'TASKS ARRAY\n', this.tasks);
  9.         // task = {
  10.         //   id: TASK,
  11.         //   id: TASK
  12.         // }
  13.       } else debug('No persistance file at', process.env.PERSISTENCE_FILE);
  14.     } catch (err) {
  15.       console.error(err);
  16.       process.exit(2);
  17.     }
  18.  
  19.     for (const id in this.tasks) {
  20.       if (this.tasks.hasOwnProperty(id)) {
  21.         // rehydrate
  22.         debug('Before Rehydration');
  23.         this.tasks[id] = new TASK(this.tasks[id].taskData, () => {
  24.           // replicate the done
  25.         }, this.tasks[id]);
  26.         debug('After Rehydration');
  27.  
  28.         // * the bellow is equivalent of -> this._handler = new DOCKER(this); etc.
  29.         this.tasks[id].initHandler();
  30.         debug('HANDLER -> ', this.tasks[id]._handler);
  31.  
  32.         try {
  33.           await this.tasks[id]._handler.reloadState();
  34.           debug('Reattached to Task' /* this.tasks[id]._handler.container.id */);
  35.         } catch (err) {
  36.           console.log(err);
  37.           // TODO restart job
  38.         }
  39.       }
  40.     }
  41.  
  42.     // TODO: Uncomment save() callback
  43.     this.saveTimer = setInterval(() => {
  44.       debug('Calling save()');
  45.       for (const id in this.tasks) {
  46.         if (moment().isAfter(this.tasks[id].finishTime)) clearInterval(this.saveTimer);
  47.       }
  48.       this.save();
  49.     }, 1000);
  50.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement