Skip to content

Commit

Permalink
refactor(daemon): Synchronize provideWorker()
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed Mar 13, 2024
1 parent 0d42e2b commit 5f17afc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
19 changes: 16 additions & 3 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1011,9 +1011,22 @@ const makeDaemonCore = async (
/**
* @type {import('./types.js').DaemonCore['incarnateWorker']}
*/
const incarnateWorker = async () => {
const formulaNumber = await formulaGraphJobs.enqueue(randomHex512);
return incarnateNumberedWorker(formulaNumber);
const incarnateWorker = async deferredTasks => {
return incarnateNumberedWorker(
await formulaGraphJobs.enqueue(async () => {
const formulaNumber = await randomHex512();

await deferredTasks.execute({
workerFormulaIdentifier: formatId({
type: 'worker',
number: formulaNumber,
node: ownNodeIdentifier,
}),
});

return formulaNumber;
}),
);
};

/**
Expand Down
37 changes: 21 additions & 16 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,23 @@ export const makeHostMaker = ({
* @param {string} workerName
*/
const provideWorker = async workerName => {
if (typeof workerName !== 'string') {
throw new Error('worker name must be string');
}
let workerFormulaIdentifier = petStore.identifyLocal(workerName);
if (workerFormulaIdentifier === undefined) {
({ formulaIdentifier: workerFormulaIdentifier } =
await incarnateWorker());
assertPetName(workerName);
await petStore.write(workerName, workerFormulaIdentifier);
} else if (!workerFormulaIdentifier.startsWith('worker:')) {
throw new Error(`Not a worker ${q(workerName)}`);
}
return /** @type {Promise<import('./types.js').EndoWorker>} */ (
// Behold, recursion:
// eslint-disable-next-line no-use-before-define
provideValueForFormulaIdentifier(workerFormulaIdentifier)
/** @type {import('./types.js').DeferredTasks<import('./types.js').WorkerDeferredTaskParams>} */
const tasks = makeDeferredTasks();
// eslint-disable-next-line no-use-before-define
const workerFormulaIdentifier = prepareWorkerFormulaIdentifier(
workerName,
tasks.push,
);

if (workerFormulaIdentifier !== undefined) {
return /** @type {Promise<import('./types.js').EndoWorker>} */ (
// Behold, recursion:
provideValueForFormulaIdentifier(workerFormulaIdentifier)
);
}

const { value } = await incarnateWorker(tasks);
return value;
};

/**
Expand All @@ -159,6 +159,11 @@ export const makeHostMaker = ({
deferTask(identifiers =>
petStore.write(workerName, identifiers.workerFormulaIdentifier),
);
} else {
const { type } = parseId(workerFormulaIdentifier);
if (type !== undefined && type !== 'worker') {
throw new Error(`Not a worker ${q(workerName)}`);
}
}
return workerFormulaIdentifier;
};
Expand Down
11 changes: 10 additions & 1 deletion packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ type WorkerFormula = {
type: 'worker';
};

/**
* Deferred tasks parameters for `host` and `guest` formulas.
*/
export type WorkerDeferredTaskParams = {
workerFormulaIdentifier: string;
};

/**
* Deferred tasks parameters for `host` and `guest` formulas.
*/
Expand Down Expand Up @@ -810,7 +817,9 @@ export interface DaemonCore {
incarnateEndoBootstrap: (
specifiedFormulaNumber: string,
) => IncarnateResult<FarEndoBootstrap>;
incarnateWorker: () => IncarnateResult<EndoWorker>;
incarnateWorker: (
deferredTasks: DeferredTasks<WorkerDeferredTaskParams>,
) => IncarnateResult<EndoWorker>;
incarnateDirectory: () => IncarnateResult<EndoDirectory>;
incarnateHost: (
endoFormulaIdentifier: string,
Expand Down

0 comments on commit 5f17afc

Please sign in to comment.