Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(daemon,cli): Rename terminate/kill to cancel #2073

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from 'os';
import { E } from '@endo/far';
import { withEndoParty } from '../context.js';

export const killCommand = async ({ name, partyNames }) =>
export const cancelCommand = async ({ name, partyNames, reason }) =>
withEndoParty(partyNames, { os, process }, async ({ party }) => {
await E(party).terminate(name);
await E(party).cancel(name, reason);
});
10 changes: 5 additions & 5 deletions packages/cli/src/endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,13 @@ export const main = async rawArgs => {
});

program
.command('kill <name>')
.command('cancel <name> [reason]')
.option(...commonOptions.as)
.description('terminate a value and its deps, recovering resources')
.action(async (name, cmd) => {
.description('cancel a value and its deps, recovering resources')
.action(async (name, reason, cmd) => {
const { as: partyNames } = cmd.opts();
const { killCommand } = await import('./commands/kill.js');
return killCommand({ name, partyNames });
const { cancelCommand } = await import('./commands/cancel.js');
return cancelCommand({ name, partyNames, reason });
});

const where = program
Expand Down
80 changes: 80 additions & 0 deletions packages/daemon/src/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// @ts-check

import { makePromiseKit } from '@endo/promise-kit';

export const makeContextMaker = ({
controllerForFormulaIdentifier,
provideControllerForFormulaIdentifier,
}) => {
/**
* @param {string} formulaIdentifier
*/
const makeContext = formulaIdentifier => {
let done = false;
const { promise: cancelled, reject: rejectCancelled } =
/** @type {import('@endo/promise-kit').PromiseKit<never>} */ (
makePromiseKit()
);
const { promise: disposed, resolve: resolveDisposed } =
/** @type {import('@endo/promise-kit').PromiseKit<void>} */ (
makePromiseKit()
);

/** @type {Map<string, import('./types.js').Context>} */
const dependents = new Map();
/** @type {Array<() => void>} */
const hooks = [];

const cancel = (reason, prefix = '*') => {
if (done) return disposed;
done = true;
rejectCancelled(reason || harden(new Error('Cancelled')));

console.log(`${prefix} ${formulaIdentifier}`);

controllerForFormulaIdentifier.delete(formulaIdentifier);
for (const dependentContext of dependents.values()) {
dependentContext.cancel(reason, ` ${prefix}`);
}
dependents.clear();

// Execute all cancellation hooks and resolve a single `undefined` for them.
resolveDisposed(Promise.all(hooks.map(hook => hook())).then(() => {}));
rekmarks marked this conversation as resolved.
Show resolved Hide resolved

return disposed;
};

const thatDiesIfThisDies = dependentFormulaIdentifier => {
assert(!done);
const dependentController = provideControllerForFormulaIdentifier(
dependentFormulaIdentifier,
);
dependents.set(dependentFormulaIdentifier, dependentController.context);
};

const thisDiesIfThatDies = dependencyIdentifier => {
const dependencyController =
provideControllerForFormulaIdentifier(dependencyIdentifier);
dependencyController.context.thatDiesIfThisDies(formulaIdentifier);
};

/**
* @param {() => void} hook
*/
const onCancel = hook => {
assert(!done);
hooks.push(hook);
};

return {
cancel,
cancelled,
disposed,
thatDiesIfThisDies,
thisDiesIfThatDies,
onCancel,
};
};

return makeContext;
};
Loading
Loading