@balena/pinejs 16.2.0-build-joshbwlng-tasks-009b08b1f157611c22f2425c25f905d5a59aaabe-1 → 16.2.0-build-joshbwlng-tasks-7478568100de4cf0789edd97865827cae9041667-1
Sign up to get free protection for your applications and to get access to all the features.
- package/.versionbot/CHANGELOG.yml +1883 -3
- package/CHANGELOG.md +717 -1
- package/out/database-layer/db.js.map +1 -1
- package/out/sbvr-api/sbvr-utils.d.ts +1 -1
- package/out/sbvr-api/sbvr-utils.js.map +1 -1
- package/out/tasks/index.d.ts +1 -1
- package/out/tasks/index.js +1 -27
- package/out/tasks/index.js.map +1 -1
- package/package.json +37 -37
- package/src/database-layer/db.ts +0 -1
- package/src/sbvr-api/sbvr-utils.ts +1 -1
- package/src/tasks/index.ts +4 -60
package/src/tasks/index.ts
CHANGED
@@ -5,7 +5,7 @@ import type * as Db from '../database-layer/db';
|
|
5
5
|
import { BadRequestError } from '../sbvr-api/errors';
|
6
6
|
import { addPureHook } from '../sbvr-api/hooks';
|
7
7
|
import { PinejsClient } from '../sbvr-api/sbvr-utils';
|
8
|
-
import * as sbvrUtils from '../sbvr-api/sbvr-utils';
|
8
|
+
import type * as sbvrUtils from '../sbvr-api/sbvr-utils';
|
9
9
|
import { ajv, apiRoot, channel } from './common';
|
10
10
|
import type { TaskHandler } from './types';
|
11
11
|
import { Worker } from './worker';
|
@@ -48,9 +48,8 @@ async function createTrigger(tx: Db.Tx): Promise<void> {
|
|
48
48
|
`);
|
49
49
|
}
|
50
50
|
|
51
|
-
//
|
51
|
+
// Partial index for polling
|
52
52
|
async function createIndexes(tx: Db.Tx): Promise<void> {
|
53
|
-
// Partial index for polling
|
54
53
|
await tx.executeSql(`
|
55
54
|
CREATE INDEX IF NOT EXISTS idx_task_poll ON task USING btree (
|
56
55
|
"is executed by-handler",
|
@@ -59,56 +58,6 @@ async function createIndexes(tx: Db.Tx): Promise<void> {
|
|
59
58
|
"id" ASC
|
60
59
|
) WHERE status = 'queued';
|
61
60
|
`);
|
62
|
-
|
63
|
-
// Partial index used to adjust priority based on actor
|
64
|
-
await tx.executeSql(`
|
65
|
-
CREATE INDEX IF NOT EXISTS idx_task_queued ON task (
|
66
|
-
"id",
|
67
|
-
"is created by-actor"
|
68
|
-
) WHERE status = 'queued';
|
69
|
-
`);
|
70
|
-
}
|
71
|
-
|
72
|
-
// Check if a task should be given priority
|
73
|
-
// This is used to balance the load across actors
|
74
|
-
// by giving priority to actors with less tasks in the queue
|
75
|
-
// when another actor is crossing a percentage threshold of queued tasks
|
76
|
-
// This is to prevent a single actor from hogging the queue
|
77
|
-
async function adjustPriority(
|
78
|
-
actor: number,
|
79
|
-
priority: number,
|
80
|
-
): Promise<number> {
|
81
|
-
const result = await (sbvrUtils.db.executeSql(
|
82
|
-
`SELECT "is created by-actor", COUNT("id") AS task_count
|
83
|
-
FROM (
|
84
|
-
SELECT "id", "is created by-actor"
|
85
|
-
FROM task
|
86
|
-
WHERE "status" = 'queued'
|
87
|
-
ORDER BY "id" DESC
|
88
|
-
LIMIT 100
|
89
|
-
) AS recent_tasks
|
90
|
-
GROUP BY "is created by-actor"
|
91
|
-
ORDER BY task_count DESC;
|
92
|
-
`,
|
93
|
-
) as Promise<{
|
94
|
-
rowsAffected: number;
|
95
|
-
rows: Array<{
|
96
|
-
'is created by-actor': number;
|
97
|
-
task_count: string;
|
98
|
-
}>;
|
99
|
-
}>);
|
100
|
-
|
101
|
-
// Increase the priority of this task if another actor is filling up the queue
|
102
|
-
if (
|
103
|
-
result.rows.some(
|
104
|
-
(row) =>
|
105
|
-
parseInt(row.task_count, 10) >= 49 &&
|
106
|
-
row['is created by-actor'] !== actor,
|
107
|
-
)
|
108
|
-
) {
|
109
|
-
return priority + 1;
|
110
|
-
}
|
111
|
-
return priority;
|
112
61
|
}
|
113
62
|
|
114
63
|
let worker: Worker | null = null;
|
@@ -144,14 +93,9 @@ export async function setup(db: Db.Database, tx: Db.Tx): Promise<void> {
|
|
144
93
|
// Set defaults
|
145
94
|
request.values.status = 'queued';
|
146
95
|
request.values.attempt_count = 0;
|
147
|
-
request.values.priority ??= 1;
|
148
96
|
request.values.attempt_limit ??= 1;
|
149
|
-
|
150
|
-
|
151
|
-
request.values.priority = await adjustPriority(
|
152
|
-
request.values.is_created_by__actor,
|
153
|
-
request.values.priority,
|
154
|
-
);
|
97
|
+
// TODO: Implement a balancer to better enqueue tasks based on actor usage
|
98
|
+
request.values.priority ??= 1;
|
155
99
|
|
156
100
|
// Set scheduled start time using cron expression if provided
|
157
101
|
if (
|