* Don't require auth for .ping * Finally introducing loggers * Remove file list caching from disk_space_primitives I don't think this is required anymore with the sync model * Simplification of space primitives and more tests * More reliable online status * Add .zed settings * Remove selfUpdate argument from `writeFile` no longer used - Add `sync` config schema and document sync options - Move sync config handling to client boot logic - Move BootConfig type to ui_types.ts - Remove sync env vars from server - Update documentation for sync and architecture * Improve sync logic and proxy routing for full sync state * Add BoxProxy for late-bound client and improve Lua boot - Add isMobile syscall for editor * Add concurrency to sync process and log indexing duration * Better service worker lifecycle management
101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
import type { Hook, Manifest } from "../types.ts";
|
|
import type { System } from "../system.ts";
|
|
import { throttle } from "../../async.ts";
|
|
import type { MQHookT } from "../../manifest.ts";
|
|
import type { DataStoreMQ, QueueWorker } from "../../data/mq.datastore.ts";
|
|
import type { MQMessage } from "../../../type/datastore.ts";
|
|
|
|
export class MQHook implements Hook<MQHookT> {
|
|
subscriptions: QueueWorker[] = [];
|
|
throttledReloadQueues = throttle(() => {
|
|
this.reloadQueues();
|
|
}, 1000);
|
|
|
|
constructor(private system: System<MQHookT>, readonly mq: DataStoreMQ) {
|
|
}
|
|
|
|
apply(system: System<MQHookT>): void {
|
|
this.system = system;
|
|
system.on({
|
|
plugLoaded: () => {
|
|
this.throttledReloadQueues();
|
|
},
|
|
plugUnloaded: () => {
|
|
this.throttledReloadQueues();
|
|
},
|
|
});
|
|
|
|
this.throttledReloadQueues();
|
|
}
|
|
|
|
stop() {
|
|
this.subscriptions.forEach((worker) => worker.stop());
|
|
this.subscriptions = [];
|
|
}
|
|
|
|
reloadQueues() {
|
|
this.stop();
|
|
for (const plug of this.system.loadedPlugs.values()) {
|
|
if (!plug.manifest) {
|
|
continue;
|
|
}
|
|
for (
|
|
const [name, functionDef] of Object.entries(
|
|
plug.manifest.functions,
|
|
)
|
|
) {
|
|
if (!functionDef.mqSubscriptions) {
|
|
continue;
|
|
}
|
|
const subscriptions = functionDef.mqSubscriptions;
|
|
for (const subscriptionDef of subscriptions) {
|
|
const queue = subscriptionDef.queue;
|
|
this.subscriptions.push(
|
|
this.mq.subscribe(
|
|
queue,
|
|
{
|
|
batchSize: subscriptionDef.batchSize,
|
|
pollInterval: subscriptionDef.pollInterval,
|
|
},
|
|
async (messages: MQMessage[]) => {
|
|
try {
|
|
await plug.invoke(name, [messages]);
|
|
if (subscriptionDef.autoAck) {
|
|
await this.mq.batchAck(queue, messages.map((m) => m.id));
|
|
}
|
|
} catch (e: any) {
|
|
console.error(
|
|
"Execution of mqSubscription for queue",
|
|
queue,
|
|
"invoking",
|
|
name,
|
|
"with messages",
|
|
messages,
|
|
"failed:",
|
|
e,
|
|
);
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
validateManifest(manifest: Manifest<MQHookT>): string[] {
|
|
const errors: string[] = [];
|
|
for (const functionDef of Object.values(manifest.functions)) {
|
|
if (!functionDef.mqSubscriptions) {
|
|
continue;
|
|
}
|
|
for (const subscriptionDef of functionDef.mqSubscriptions) {
|
|
if (!subscriptionDef.queue) {
|
|
errors.push("Missing queue name for mqSubscription");
|
|
}
|
|
}
|
|
}
|
|
return errors;
|
|
}
|
|
}
|