The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
place-contrib: send a queued value at once instead of minutes later
Mauricead2916903c65
diff --git a/server/src/nest/place-contrib/place-contrib.constants.ts b/server/src/nest/place-contrib/place-contrib.constants.tsindex d438a559e..0348165cb 100644--- a/server/src/nest/place-contrib/place-contrib.constants.ts+++ b/server/src/nest/place-contrib/place-contrib.constants.ts@@ -14,10 +14,6 @@ export const PLACE_BODY_MAX_BYTES = 8 * 1024;export const ANSWER_MAX_BYTES = 64 * 1024;export const REQUEST_TIMEOUT_MS = 15_000;-/** A value waits a few minutes before it goes, a random share of this window per place. */-export const ELIGIBLE_DELAY_MIN_MS = 2 * 60_000;-export const ELIGIBLE_DELAY_MAX_MS = 10 * 60_000;-/** The waits after the first, second, ... failed attempt; the last one repeats. */export const RETRY_DELAYS_MS = [10 * 60_000, 30 * 60_000, 2 * 3_600_000, 6 * 3_600_000] as const;/** After this many failed attempts a value is given up on. */diff --git a/server/src/nest/place-contrib/place-contrib.helpers.ts b/server/src/nest/place-contrib/place-contrib.helpers.tsindex 7eea3eb7d..b619c6ea6 100644--- a/server/src/nest/place-contrib/place-contrib.helpers.ts+++ b/server/src/nest/place-contrib/place-contrib.helpers.ts@@ -1,11 +1,4 @@-import {- ELIGIBLE_DELAY_MAX_MS,- ELIGIBLE_DELAY_MIN_MS,- MAX_RETRY_AFTER_MS,- RETRY_DELAYS_MS,- SUMMARY_MAX_CHARS,- VALUE_MAX_CHARS,-} from './place-contrib.constants';+import { MAX_RETRY_AFTER_MS, RETRY_DELAYS_MS, SUMMARY_MAX_CHARS, VALUE_MAX_CHARS } from './place-contrib.constants';import {parsePlaceHours,placeWebsiteSchema,@@ -151,11 +144,6 @@ export function newEntryCid(): string {.slice(0, 24);}-/** A few minutes, a different share for every place, so a trip's places do not all go together. */-export function eligibleDelayMs(): number {- return randomInt(ELIGIBLE_DELAY_MIN_MS, ELIGIBLE_DELAY_MAX_MS + 1);-}-/** The wait after `attempts` failed attempts (1 for the first). */export function retryDelayMs(attempts: number): number {const index = Math.min(Math.max(attempts, 1), RETRY_DELAYS_MS.length) - 1;diff --git a/server/src/nest/place-contrib/place-contrib.job.ts b/server/src/nest/place-contrib/place-contrib.job.tsindex 8563eda77..22b2ded75 100644--- a/server/src/nest/place-contrib/place-contrib.job.ts+++ b/server/src/nest/place-contrib/place-contrib.job.ts@@ -24,19 +24,44 @@ export class PlaceContribJob implements OnApplicationBootstrap {private readonly registrar: CronRegistrarService,) {}+ /** Whether the schedules run here; off in tests and on a replica, so a save sends nothing there either. */+ private enabled = false;+ private running: Promise<void> | null = null;+ private again = false;+/** @txIndependent two schedules, each registered on its own; neither touches the database. */onApplicationBootstrap(): void {if (!this.registrar.isEnabled()) return;+ this.enabled = true;this.registrar.register('place-contrib-send', SEND_SCHEDULE, () => this.send());this.registrar.register('place-contrib-log-retention', LOG_RETENTION_SCHEDULE, () => this.retain());}+ /** A value was queued: send it now instead of at the next tick, which stays for the retries. */+ sendSoon(): void {+ if (this.enabled) void this.send();+ }++ /** One run at a time; a run asked for while one is going follows right after it. */async send(): Promise<void> {+ if (this.running) {+ this.again = true;+ return this.running;+ }+ this.running = this.runOnce().finally(() => {+ this.running = null;+ });+ return this.running;+ }++ private async runOnce(): Promise<void> {+ this.again = false;try {await this.sender.run();} catch (err: unknown) {logError(`Places contribution: the run failed: ${err instanceof Error ? err.message : String(err)}`);}+ if (this.again) return this.runOnce();}async retain(now: Date = new Date()): Promise<void> {diff --git a/server/src/nest/place-contrib/place-contrib.service.ts b/server/src/nest/place-contrib/place-contrib.service.tsindex d13cedb9e..5b9ffb747 100644--- a/server/src/nest/place-contrib/place-contrib.service.ts+++ b/server/src/nest/place-contrib/place-contrib.service.ts@@ -11,7 +11,8 @@ import { SettingsService } from '../settings/settings.service';import { agreedToCurrentTerms, PlaceContribSettings } from './place-contrib-settings';import { PlaceContribClient } from './place-contrib.client';import { COUNT_WINDOW_DAYS, OUTBOX_CAP, SNOOZE_DAYS } from './place-contrib.constants';-import { contribKeyOf, dayOf, eligibleDelayMs, newEntryCid, payloadOf, summaryOf } from './place-contrib.helpers';+import { contribKeyOf, dayOf, newEntryCid, payloadOf, summaryOf } from './place-contrib.helpers';+import { PlaceContribJob } from './place-contrib.job';import type { ContribValue, ContribWrite } from './place-contrib.types';import { InjectRepository } from '@mikro-orm/nestjs';import { Injectable } from '@nestjs/common';@@ -51,6 +52,7 @@ export class PlaceContribService {private readonly settings: SettingsService,private readonly client: PlaceContribClient,private readonly uow: UnitOfWork,+ private readonly job: PlaceContribJob,) {}async state(adminId: number, now: Date = new Date()): Promise<PlaceContribState> {@@ -183,7 +185,7 @@ export class PlaceContribService {* user's leave the queue whatever the decision; the user's values are* queued only while the installation is eligible and an admin agreed, each* for a field that is switched on, and only for a place the service can key.- * The newest value replaces a queued one; it goes a few minutes later.+ * The newest value replaces a queued one, and the queue is sent at once.** @txStandalone a derived write after the place write committed: it never fails that write.*/@@ -198,9 +200,10 @@ export class PlaceContribService {await this.log.insertLines(shared.map((value) => this.withheldLine(write, value, now, 'no_contrib_key')));return;}- const due = dbNow(new Date(now.getTime() + eligibleDelayMs()));+ const due = dbNow(now);await inOrder(shared, (value) => this.queueOne(write, { key, value, due, now }));});+ this.job.sendSoon();}private async queueOne(diff --git a/server/tests/unit/nest/place-contrib/place-contrib.helpers.test.ts b/server/tests/unit/nest/place-contrib/place-contrib.helpers.test.tsindex c1d65775f..1b2120f43 100644--- a/server/tests/unit/nest/place-contrib/place-contrib.helpers.test.ts+++ b/server/tests/unit/nest/place-contrib/place-contrib.helpers.test.ts@@ -3,17 +3,11 @@* a field sends (or why it stays home), the OSM rule a week of hours becomes,* and the retry clock.*/-import {- MAX_RETRY_AFTER_MS,- RETRY_DELAYS_MS,- ELIGIBLE_DELAY_MAX_MS,- ELIGIBLE_DELAY_MIN_MS,-} from '../../../../src/nest/place-contrib/place-contrib.constants';+import { MAX_RETRY_AFTER_MS, RETRY_DELAYS_MS } from '../../../../src/nest/place-contrib/place-contrib.constants';import {baseHashOf,contribKeyOf,dayOf,- eligibleDelayMs,gersOf,logResultOf,newEntryCid,@@ -154,14 +148,6 @@ describe('the log and the request ids', () => {});describe('the clock', () => {- it('PCH-040: a place waits a few minutes, never hours', () => {- for (let i = 0; i < 50; i += 1) {- const delay = eligibleDelayMs();- expect(delay).toBeGreaterThanOrEqual(ELIGIBLE_DELAY_MIN_MS);- expect(delay).toBeLessThanOrEqual(ELIGIBLE_DELAY_MAX_MS);- }- });-it('PCH-041: retries wait 10 min, 30 min, 2 h, then 6 h', () => {expect([1, 2, 3, 4, 5, 8].map(retryDelayMs)).toEqual([RETRY_DELAYS_MS[0],diff --git a/server/tests/unit/nest/place-contrib/place-contrib.job.test.ts b/server/tests/unit/nest/place-contrib/place-contrib.job.test.tsindex 6dffa2ea2..642bda9ce 100644--- a/server/tests/unit/nest/place-contrib/place-contrib.job.test.ts+++ b/server/tests/unit/nest/place-contrib/place-contrib.job.test.ts@@ -1,7 +1,7 @@/*** The two crons of the community enrichment: registration behind the test- * gate, the log retention cut-off, and that a failing run or sweep stays- * inside its tick.+ * gate, the log retention cut-off, that a failing run or sweep stays inside+ * its tick, and the run a save starts at once.*/import type { PlaceContribLogRepository } from '../../../../src/db/repositories/PlaceContribLog.repository';import { inOrder } from '../../../../src/nest/common/in-order';@@ -75,4 +75,33 @@ describe('PlaceContribJob', () => {await expect(job.retain()).resolves.toBeUndefined();expect(logMock.logError).toHaveBeenCalledTimes(2);});+ it('PCJ-006: a save sends at once where the schedules run, and nowhere else', async () => {+ const off = make(false);+ off.job.onApplicationBootstrap();+ off.job.sendSoon();+ expect(off.sender.run).not.toHaveBeenCalled();++ const on = make();+ on.job.onApplicationBootstrap();+ on.job.sendSoon();+ await vi.waitFor(() => expect(on.sender.run).toHaveBeenCalledTimes(1));+ });++ it('PCJ-007: one run at a time, and a run asked for meanwhile follows right after it', async () => {+ const { job, sender } = make();+ let release: () => void = () => undefined;+ sender.run.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ release = () => resolve({ skipped: null, places: 1, expired: 0 });+ }),+ );+ const first = job.send();+ const second = job.send();+ const third = job.send();+ expect(sender.run).toHaveBeenCalledTimes(1);+ release();+ await Promise.all([first, second, third]);+ expect(sender.run).toHaveBeenCalledTimes(2);+ });});diff --git a/server/tests/unit/nest/place-contrib/place-contrib.service.test.ts b/server/tests/unit/nest/place-contrib/place-contrib.service.test.tsindex 44dadae2e..86f4a23a5 100644--- a/server/tests/unit/nest/place-contrib/place-contrib.service.test.ts+++ b/server/tests/unit/nest/place-contrib/place-contrib.service.test.ts@@ -339,7 +339,7 @@ describe('enqueue', () => {expect(await countRows(orm(), PlaceContribLog)).toBe(0);});- it('PCS-031: a user value of a keyed place is queued, normalized, a few minutes out', async () => {+ it('PCS-031: a user value of a keyed place is queued, normalized and due at once', async () => {const placeId = await newPlace();await agree();await service().enqueue(write(placeId), NOW);@@ -351,8 +351,7 @@ describe('enqueue', () => {country: 'DE',});const due = Date.parse(`${row?.eligible_at.replace(' ', 'T')}Z`);- expect(due - NOW.getTime()).toBeGreaterThanOrEqual(2 * 60_000 - 1000);- expect(due - NOW.getTime()).toBeLessThanOrEqual(10 * 60_000 + 1000);+ expect(Math.abs(due - NOW.getTime())).toBeLessThan(1000);});it('PCS-032: the newest value replaces the queued one, attempts and all', async () => {