-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.ts
54 lines (48 loc) · 1.5 KB
/
tasks.ts
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
import { Handler } from "aws-lambda";
import TasksData from "./src/model/tasksData";
import callLambda from "./src/lambda/lambda";
import Tasks from "./src/table/tasks";
import History from "./src/table/history";
const TASKS_TABLE = process.env.TASKS_TABLE!;
export const check: Handler = async () =>
//event: any,
//context: Context,
//callback: Callback
{
try {
const table = new Tasks(TASKS_TABLE);
const data: TasksData[] = await table.scan();
const count: number = data.length;
if (count > 0) {
console.log("count", count, "items:", data);
let i: number;
const time = Date.now();
for (i = 0; i < count; i++) {
console.log("item", i, ":", data[i]);
if (time > data[i].startTime) {
if (data[i].startTime + 24 * 60 * 60 * 1000 < time) {
console.error("Removing stuck task", data[i]);
await table.remove(data[i].id);
}
console.log("Executing");
await callLambda(
data[i].task,
data[i].taskdata
? JSON.stringify({
id: data[i].id,
data: data[i].taskdata,
})
: JSON.stringify({ id: data[i].id })
);
} else {
console.log("Waiting");
}
}
}
await History.clean();
return 200;
} catch (error) {
console.error("catch", (<any>error).toString());
return 200;
}
};