-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.js
65 lines (57 loc) · 1.75 KB
/
job.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
console.log("job worker created", Deno.env.get("env"));
const runJob = async (path) => {
console.log("running job");
const { default: api } = await import(`${path}_app/db/api/get.js`);
const { date, setDate } = await import(
`${path}_app/job/api/services/index.js`
);
const res = await api(
new Request(
`http://hello.com?col=job&indexName=date&index=${date.getToday()}`,
{
headers: {
referer: "oneohone.xyz",
},
},
),
);
if (res.status === 200) {
const activeJobs = res.document_data.filter((data) =>
data.retry <= 3 && data.duration > 0
);
console.log(activeJobs, "jobs for today");
activeJobs.forEach(async (element) => {
element.headers.body = JSON.stringify(element.payload);
const updateJobSpec = {
duration: --element.duration,
date: setDate(element.date.split("/")).addDays(
element.interval - element.retry,
),
};
await fetch(element.uri, element.headers).catch((err) => {
updateJobSpec.duration = element.duration;
updateJobSpec.retry = ++element.retry;
updateJobSpec.date = setDate(element.date.split("/")).addDays(1);
console.log("job failed");
});
const { default: api } = await import(`${path}_app/db/api/patch.js`);
await api(
new Request(`http://hello.com?col=job&id=${element.id}`, {
headers: {
referer: "oneohone.xyz",
},
}),
{ ...updateJobSpec },
);
});
}
};
setInterval(() => {
runJob("sauveur.cloud/src/");
console.log("running the jobs for today", new Date().toTimeString());
}, 86400000);
if (Deno.env.get("env")) {
setInterval(async () => {
runJob("sauveur.cloud/src/");
}, 864000);
}