@balena/open-balena-api 31.2.9 → 31.2.10-build-service-install-task-write-tx-03dec042707480b4af9ba29641165b625dedbce5-1
Sign up to get free protection for your applications and to get access to all the features.
@@ -29,92 +29,108 @@ tasks.addTaskHandler('create_service_installs', async (options) => {
|
|
29
29
|
};
|
30
30
|
}
|
31
31
|
}, schema);
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
$expand: {
|
40
|
-
image: {
|
41
|
-
$select: 'is_a_build_of__service',
|
42
|
-
},
|
43
|
-
},
|
44
|
-
},
|
45
|
-
},
|
46
|
-
};
|
47
|
-
const targetServicesByDevice = new Map((await api.resin.get({
|
48
|
-
resource: 'device',
|
49
|
-
passthrough: { req: permissions.rootRead },
|
50
|
-
options: {
|
32
|
+
async function getTargetDevicesAndServices(devices) {
|
33
|
+
// We use a write transaction to avoid a race condition that a high
|
34
|
+
// replication lag can cause, in which case we read stale data.
|
35
|
+
// Eg moving a device could lead to recreating the same service installs.
|
36
|
+
// TODO: Consider artificially delaying the task from running as an alternative.
|
37
|
+
return await sbvrUtils.db.transaction(async (tx) => {
|
38
|
+
const releaseExpand = {
|
51
39
|
$select: 'id',
|
52
40
|
$expand: {
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
id: { $in: devices },
|
59
|
-
},
|
60
|
-
},
|
61
|
-
})).map((device) => {
|
62
|
-
const deviceServiceIds = [
|
63
|
-
...device.should_be_running__release,
|
64
|
-
...device.should_be_managed_by__release,
|
65
|
-
...device.should_be_operated_by__release,
|
66
|
-
].flatMap((release) => release.contains__image.flatMap((image) => image.image.map((img) => img.is_a_build_of__service.__id)));
|
67
|
-
return [device.id, [...new Set(deviceServiceIds)]];
|
68
|
-
}));
|
69
|
-
const serviceIds = [...new Set([...targetServicesByDevice.values()].flat())];
|
70
|
-
const missingServiceFilters = serviceIds.map((serviceId) => ({
|
71
|
-
$not: {
|
72
|
-
service_install: {
|
73
|
-
$any: {
|
74
|
-
$alias: 'si',
|
75
|
-
$expr: {
|
76
|
-
si: {
|
77
|
-
installs__service: serviceId,
|
41
|
+
contains__image: {
|
42
|
+
$select: 'id',
|
43
|
+
$expand: {
|
44
|
+
image: {
|
45
|
+
$select: 'is_a_build_of__service',
|
78
46
|
},
|
79
47
|
},
|
80
48
|
},
|
81
49
|
},
|
82
|
-
}
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
50
|
+
};
|
51
|
+
const targetServicesByDevice = new Map((await api.resin.get({
|
52
|
+
resource: 'device',
|
53
|
+
passthrough: { req: permissions.rootRead, tx },
|
54
|
+
options: {
|
55
|
+
$select: 'id',
|
56
|
+
$expand: {
|
57
|
+
should_be_running__release: releaseExpand,
|
58
|
+
should_be_managed_by__release: releaseExpand,
|
59
|
+
should_be_operated_by__release: releaseExpand,
|
60
|
+
},
|
61
|
+
$filter: {
|
62
|
+
id: { $in: devices },
|
63
|
+
},
|
64
|
+
},
|
65
|
+
})).map((device) => {
|
66
|
+
const deviceServiceIds = [
|
67
|
+
...device.should_be_running__release,
|
68
|
+
...device.should_be_managed_by__release,
|
69
|
+
...device.should_be_operated_by__release,
|
70
|
+
].flatMap((release) => release.contains__image.flatMap((image) => image.image.map((img) => img.is_a_build_of__service.__id)));
|
71
|
+
return [device.id, [...new Set(deviceServiceIds)]];
|
72
|
+
}));
|
73
|
+
const serviceIds = [
|
74
|
+
...new Set([...targetServicesByDevice.values()].flat()),
|
75
|
+
];
|
76
|
+
if (serviceIds.length === 0) {
|
77
|
+
console.info('[service-install-task] No service installs to create');
|
78
|
+
return;
|
79
|
+
}
|
80
|
+
const missingServiceFilters = serviceIds.map((serviceId) => ({
|
81
|
+
$not: {
|
94
82
|
service_install: {
|
95
|
-
$
|
96
|
-
|
97
|
-
|
83
|
+
$any: {
|
84
|
+
$alias: 'si',
|
85
|
+
$expr: {
|
86
|
+
si: {
|
87
|
+
installs__service: serviceId,
|
88
|
+
},
|
89
|
+
},
|
98
90
|
},
|
99
91
|
},
|
100
92
|
},
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
93
|
+
}));
|
94
|
+
const devicesToAddServiceInstalls = (await api.resin.get({
|
95
|
+
resource: 'device',
|
96
|
+
passthrough: { req: permissions.rootRead, tx },
|
97
|
+
options: {
|
98
|
+
$select: 'id',
|
99
|
+
$expand: {
|
100
|
+
service_install: {
|
101
|
+
$select: 'installs__service',
|
102
|
+
$filter: {
|
103
|
+
installs__service: { $in: serviceIds },
|
104
|
+
},
|
105
|
+
},
|
106
|
+
},
|
107
|
+
$filter: {
|
108
|
+
id: { $in: devices },
|
109
|
+
...(missingServiceFilters.length === 1
|
110
|
+
? missingServiceFilters[0]
|
111
|
+
: {
|
112
|
+
$or: missingServiceFilters,
|
113
|
+
}),
|
114
|
+
},
|
108
115
|
},
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
};
|
116
|
+
})).map((device) => {
|
117
|
+
// Transform the device to the simpler object we will need later which is smaller
|
118
|
+
// and will allow the larger version to be garbage collected sooner
|
119
|
+
return {
|
120
|
+
id: device.id,
|
121
|
+
serviceInstalls: device.service_install.map((si) => si.installs__service.__id),
|
122
|
+
};
|
123
|
+
});
|
124
|
+
return { targetServicesByDevice, devicesToAddServiceInstalls };
|
117
125
|
});
|
126
|
+
}
|
127
|
+
const createServiceInstalls = async ({ devices, }) => {
|
128
|
+
const startTime = Date.now();
|
129
|
+
const deviceAndServiceInfo = await getTargetDevicesAndServices(devices);
|
130
|
+
if (deviceAndServiceInfo == null) {
|
131
|
+
return 0;
|
132
|
+
}
|
133
|
+
const { targetServicesByDevice, devicesToAddServiceInstalls } = deviceAndServiceInfo;
|
118
134
|
// This is already batched at one level, does it make sense to batch it again?
|
119
135
|
const remainingDevices = new Set(devices);
|
120
136
|
let totalSiCreated = 0;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"service-installs.js","sourceRoot":"","sources":["../../../../src/features/ci-cd/tasks/service-installs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,CAAC,MAAM,QAAQ,CAAC;AACvB,OAAO,EACN,wBAAwB,EACxB,8CAA8C,GAC9C,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,0BAA0B,EAAE,MAAM,wCAAwC,CAAC;AAEpF,MAAM,MAAM,GAAG;IACd,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACX,OAAO,EAAE;YACR,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SACzB;KACD;IACD,QAAQ,EAAE,CAAC,SAAS,CAAC;CACrB,CAAC;AAEF,MAAM,EAAE,GAAG,EAAE,GAAG,SAAS,CAAC;AAM1B,KAAK,CAAC,cAAc,CACnB,yBAAyB,EACzB,KAAK,EAAE,OAAO,EAAE,EAAE;IACjB,IAAI,CAAC;QACJ,MAAM,cAAc,GAAG,MAAM,qBAAqB,CACjD,OAAO,CAAC,MAAyC,CACjD,CAAC;QACF,OAAO,CAAC,IAAI,CACX,kCAAkC,cAAc,mBAAmB,CACnE,CAAC;QACF,OAAO;YACN,MAAM,EAAE,WAAW;SACnB,CAAC;IACH,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACZ,OAAO,CAAC,KAAK,CACZ,2DAA2D,CAAC,EAAE,CAC9D,CAAC;QACF,OAAO;YACN,KAAK,EAAE,GAAG,CAAC,EAAE;YACb,MAAM,EAAE,QAAQ;SAChB,CAAC;IACH,CAAC;AACF,CAAC,EACD,MAAM,CACN,CAAC;AAEF,
|
1
|
+
{"version":3,"file":"service-installs.js","sourceRoot":"","sources":["../../../../src/features/ci-cd/tasks/service-installs.ts"],"names":[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1
1
|
{
|
2
2
|
"name": "@balena/open-balena-api",
|
3
3
|
"description": "Internet of things, Made Simple",
|
4
|
-
"version": "31.2.
|
4
|
+
"version": "31.2.10-build-service-install-task-write-tx-03dec042707480b4af9ba29641165b625dedbce5-1",
|
5
5
|
"license": "AGPL-3.0",
|
6
6
|
"repository": {
|
7
7
|
"type": "git",
|
@@ -166,6 +166,6 @@
|
|
166
166
|
"loader": "ts-node/esm/transpile-only"
|
167
167
|
},
|
168
168
|
"versionist": {
|
169
|
-
"publishedAt": "2024-11-
|
169
|
+
"publishedAt": "2024-11-22T12:09:47.356Z"
|
170
170
|
}
|
171
171
|
}
|