mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Stored Sync stages nest - a rule archive's delivery inside the rule stage,
inside activity delivery, inside the Sync journal - and every layer's guard
checks before and after by calling the guard below it twice. One innermost
check therefore re-ran every outer guard 2^depth times, and one Sync-created
card archived by a rule took 36 s, which is why archive was held back from
durable Sync (1e40e7e06).
reuseWithinEvaluation (server/lib/syncGuardWindow.js) runs each guard at most
once per evaluation: calls made while another guard's check is running share
the results of guards already checked in it. A call from ordinary work -
between stages, after an adapter or a write - starts a new evaluation and
checks everything afresh, so a policy change or a lost lease between stages
still stops the next one. A first attempt with a 100 ms reuse window missed
exactly that and was dropped. Failures never carry over, a guard reached
again through its own check runs fresh instead of waiting on itself, and the
journal's ownership read is left as it was.
With that, archive and unarchive are registered as durable rule adapters
through the stored archive runner, and boards whose rules archive or
unarchive now take durable Sync. The full-app durable Sync test runs an
"archive every created card" rule end to end: 1.1 s for the whole test. The
rule selection test's example of an action without an adapter is now
moveCardToTop. 565 server tests pass.
Thanks to xet7 !
46 lines
2.3 KiB
JavaScript
46 lines
2.3 KiB
JavaScript
'use strict';
|
|
const { EJSON } = require('bson');
|
|
const { validateSyncEffectPolicy, assertSyncEffectPolicy } = require('./syncEffectPolicy');
|
|
const { reuseWithinEvaluation } = require('./syncGuardWindow');
|
|
const { notificationActivityIdentity, planId: notificationId } = require('./syncNotificationPlan');
|
|
const { planId: webhookId } = require('./syncWebhookPlan');
|
|
const copy = value => EJSON.parse(EJSON.stringify(value), { relaxed: true });
|
|
// These adapters must reconcile their own durable receipts on replay. This
|
|
// coordinator never treats activity insertion or a resolved Promise alone as
|
|
// acknowledgement. Rules are mandatory even when notifications are disabled.
|
|
// `trigger` (manual or scheduled) reaches every stage, which checks the Sync
|
|
// activation switches itself (server/lib/syncActivation.js).
|
|
async function deliverSyncActivity({ effectId, activity, policy, assertCurrent, readPolicy,
|
|
rules, notifications, webhooks, trigger }) {
|
|
if (typeof effectId !== 'string' || !/^[a-f0-9]{64}$/.test(effectId) ||
|
|
typeof assertCurrent !== 'function' || typeof readPolicy !== 'function') throw new Error('sync-activity-delivery-invalid');
|
|
const saved = copy(activity), captured = validateSyncEffectPolicy(policy);
|
|
notificationActivityIdentity(saved);
|
|
if (typeof saved.listId !== 'string' || !saved.listId || saved.listId.length > 1024) {
|
|
throw new Error('sync-activity-delivery-invalid');
|
|
}
|
|
if (!captured.activities || typeof rules !== 'function' ||
|
|
(captured.notifications && (typeof notifications !== 'function' || typeof webhooks !== 'function'))) {
|
|
throw new Error('sync-activity-delivery-adapter-required');
|
|
}
|
|
const guard = reuseWithinEvaluation(async () => {
|
|
await assertCurrent();
|
|
await assertSyncEffectPolicy(captured, readPolicy);
|
|
await assertCurrent();
|
|
});
|
|
const run = async (adapter, expected) => {
|
|
await guard();
|
|
const receipt = await adapter({ effectId, activity: copy(saved), policy: { ...captured }, assertCurrent: guard, trigger });
|
|
if (receipt !== expected) throw new Error('sync-activity-delivery-unconfirmed');
|
|
await guard();
|
|
};
|
|
await run(rules, effectId);
|
|
if (captured.notifications) {
|
|
await run(notifications, notificationId(saved._id));
|
|
await run(webhooks, webhookId(saved._id));
|
|
}
|
|
await guard();
|
|
return effectId;
|
|
}
|
|
module.exports = { deliverSyncActivity };
|