Skip to content

Commit d5f0137

Browse files
Make worker names global && return improve error handling for APIError
1 parent 85a0f71 commit d5f0137

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

src/backend/src/services/worker/WorkerService.js

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class WorkerService extends BaseService {
8989
const es_subdomain = this.services.get('es:subdomain');
9090
const svc_auth = this.services.get("auth");
9191
const svc_notification = this.services.get('notification');
92-
92+
9393
svc_event.on('fs.written.file', async (_key, data, meta) => {
9494
// Code should only run on the same server as the write
9595
if (meta.from_outside) return;
@@ -121,34 +121,34 @@ class WorkerService extends BaseService {
121121
}
122122

123123

124-
svc_notification.notify(
125-
UsernameNotifSelector(actor.type.user.username),
126-
{
127-
source: 'worker',
128-
title: `Deploying CF worker ${workerName}`,
129-
template: 'user-requesting-share',
130-
fields: {
131-
username: actor.type.user.username,
132-
},
133-
}
134-
);
124+
// svc_notification.notify(
125+
// UsernameNotifSelector(actor.type.user.username),
126+
// {
127+
// source: 'worker',
128+
// title: `Deploying CF worker ${workerName}`,
129+
// template: 'user-requesting-share',
130+
// fields: {
131+
// username: actor.type.user.username,
132+
// },
133+
// }
134+
// );
135135
try {
136136
// Create the worker
137137
const cfData = await createWorker((await data.node.get("owner")).type.user, authToken, workerName, preamble + fileData, PREAMBLE_LENGTH);
138138

139139
// Send user the appropriate notification
140140
if (cfData.success) {
141-
svc_notification.notify(
142-
UsernameNotifSelector(actor.type.user.username),
143-
{
144-
source: 'worker',
145-
title: `Succesfully deployed ${cfData.url}`,
146-
template: 'user-requesting-share',
147-
fields: {
148-
username: actor.type.user.username,
149-
},
150-
}
151-
);
141+
// svc_notification.notify(
142+
// UsernameNotifSelector(actor.type.user.username),
143+
// {
144+
// source: 'worker',
145+
// title: `Succesfully deployed ${cfData.url}`,
146+
// template: 'user-requesting-share',
147+
// fields: {
148+
// username: actor.type.user.username,
149+
// },
150+
// }
151+
// );
152152
} else {
153153
svc_notification.notify(
154154
UsernameNotifSelector(actor.type.user.username),
@@ -195,22 +195,26 @@ class WorkerService extends BaseService {
195195
req: { user: Context.get("actor").type.user },
196196
getParam: () => filePath,
197197
})).get("path");
198-
const userData = await getUserInfo(authorization, this.global_config.api_base_url);
199-
const actor = Context.get("actor");
200198
const es_subdomain = this.services.get('es:subdomain');
201-
const fileData = (await readPuterFile(actor, filePath)).toString();
202-
const cfData = await createWorker(userData, authorization, calculateWorkerNameNew(userData.uuid, workerName), preamble + fileData, PREAMBLE_LENGTH);
203199

200+
const userData = await getUserInfo(authorization, this.global_config.api_base_url);
201+
const actor = Context.get("actor");
204202
await Context.sub({ [SKIP_ES_VALIDATION]: true }).arun(async () => {
205203
const entity = await Entity.create({ om: es_subdomain.om }, {
206-
subdomain: "workers.puter." + calculateWorkerNameNew(userData.uuid, workerName),
204+
subdomain: "workers.puter." + calculateWorkerNameNew(userData, workerName),
207205
root_dir: filePath
208206
});
209207
await es_subdomain.upsert(entity);
210208
});
211209

210+
const fileData = (await readPuterFile(actor, filePath)).toString();
211+
const cfData = await createWorker(userData, authorization, calculateWorkerNameNew(userData.uuid, workerName), preamble + fileData, PREAMBLE_LENGTH);
212+
213+
212214
return cfData;
213215
} catch (e) {
216+
if (e instanceof APIError)
217+
throw e;
214218
console.error(e)
215219
return { success: false, errors: e }
216220
}
@@ -219,20 +223,26 @@ class WorkerService extends BaseService {
219223
try {
220224
workerName = workerName.toLocaleLowerCase(); // just incase
221225
const svc_su = this.services.get("su");
226+
const es_subdomain = this.services.get('es:subdomain');
227+
222228
const userData = await getUserInfo(authorization, this.global_config.api_base_url);
229+
230+
const [result] = (await es_subdomain.select({ predicate: new Eq({ key: "subdomain", value: "workers.puter." + calculateWorkerNameNew(undefined, workerName) }) }));
231+
232+
if (result.values_.owner.uuid !== userData.uuid) {
233+
throw new Error("This is not your worker!");
234+
}
235+
223236
const cfData = await deleteWorker(userData, authorization, workerName);
224237

225-
const es_subdomain = this.services.get('es:subdomain');
226-
const result = await svc_su.sudo(async () => {
227-
const row = (await es_subdomain.select({ predicate: new Eq({ key: "subdomain", value: "workers.puter." + calculateWorkerNameNew(userData.uuid, workerName) }) }));
228-
return row;
229-
})
230238

231-
await es_subdomain.delete(await result[0].get("uid"));
239+
await es_subdomain.delete(await result.get("uid"));
232240
return cfData;
233241

234242

235243
} catch (e) {
244+
if (e instanceof APIError)
245+
throw e;
236246
console.error(e);
237247
return { success: false, e }
238248
}

src/backend/src/services/worker/workerUtils/nameUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function sha1(input) {
77

88
function calculateWorkerNameNew(uuid, workerId) {
99

10-
return `${workerId}-${uuid.replaceAll("-", "")}`
10+
return `${workerId}`; // Used to be ${workerId}-${uuid.replaceAll("-", "")}
1111
}
1212
module.exports = {
1313
sha1,

0 commit comments

Comments
 (0)