|
| 1 | +/* ************************************************************************ |
| 2 | +
|
| 3 | + osparc - the simcore frontend |
| 4 | +
|
| 5 | + https://osparc.io |
| 6 | +
|
| 7 | + Copyright: |
| 8 | + 2025 IT'IS Foundation, https://itis.swiss |
| 9 | +
|
| 10 | + License: |
| 11 | + MIT: https://opensource.org/licenses/MIT |
| 12 | +
|
| 13 | + Authors: |
| 14 | + * Odei Maiz (odeimaiz) |
| 15 | +
|
| 16 | +************************************************************************ */ |
| 17 | + |
| 18 | +qx.Class.define("osparc.store.Jobs", { |
| 19 | + extend: qx.core.Object, |
| 20 | + type: "singleton", |
| 21 | + |
| 22 | + properties: { |
| 23 | + jobs: { |
| 24 | + check: "Array", |
| 25 | + init: [], |
| 26 | + nullable: true, |
| 27 | + event: "changeJobs" |
| 28 | + } |
| 29 | + }, |
| 30 | + |
| 31 | + members: { |
| 32 | + fetchJobs: function() { |
| 33 | + return osparc.data.Resources.get("jobs") |
| 34 | + .then(jobsData => { |
| 35 | + jobsData.forEach(jobData => { |
| 36 | + const interval = 1000; |
| 37 | + this.addJob(jobData, interval); |
| 38 | + }); |
| 39 | + }) |
| 40 | + .catch(err => console.error(err)); |
| 41 | + }, |
| 42 | + |
| 43 | + addJob: function(jobData, interval = 1000) { |
| 44 | + const jobs = this.getJobs(); |
| 45 | + const index = jobs.findIndex(t => t.getJobId() === jobData["job_id"]); |
| 46 | + if (index === -1) { |
| 47 | + const job = new osparc.data.Job(jobData, interval); |
| 48 | + jobs.push(job); |
| 49 | + return job; |
| 50 | + } |
| 51 | + return null; |
| 52 | + }, |
| 53 | + |
| 54 | + createJob: function(fetchPromise, interval) { |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + fetchPromise |
| 57 | + .then(jobData => { |
| 58 | + if ("status_href" in jobData) { |
| 59 | + const job = this.addJob(jobData, interval); |
| 60 | + resolve(job); |
| 61 | + } else { |
| 62 | + throw Error("Status missing"); |
| 63 | + } |
| 64 | + }) |
| 65 | + .catch(err => reject(err)); |
| 66 | + }); |
| 67 | + }, |
| 68 | + |
| 69 | + removeJobs: function() { |
| 70 | + const jobs = this.getJobs(); |
| 71 | + jobs.forEach(job => job.dispose()); |
| 72 | + }, |
| 73 | + } |
| 74 | +}); |
0 commit comments