@curless/hotel-storefront 0.84.8 → 0.85.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.d.ts +35 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/huazhu/ari-sync.d.ts +93 -6
- package/dist/huazhu/ari-sync.d.ts.map +1 -1
- package/dist/huazhu/ari-sync.js +303 -28
- package/dist/huazhu/ari-sync.js.map +1 -1
- package/dist/huazhu/backend.d.ts +9 -0
- package/dist/huazhu/backend.d.ts.map +1 -1
- package/dist/huazhu/backend.js +62 -22
- package/dist/huazhu/backend.js.map +1 -1
- package/dist/huazhu/catalog.d.ts +58 -1
- package/dist/huazhu/catalog.d.ts.map +1 -1
- package/dist/huazhu/catalog.js +318 -15
- package/dist/huazhu/catalog.js.map +1 -1
- package/dist/huazhu/live-asks.d.ts +63 -0
- package/dist/huazhu/live-asks.d.ts.map +1 -0
- package/dist/huazhu/live-asks.js +127 -0
- package/dist/huazhu/live-asks.js.map +1 -0
- package/dist/huazhu/make.d.ts.map +1 -1
- package/dist/huazhu/make.js +50 -4
- package/dist/huazhu/make.js.map +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -1
- package/package.json +1 -1
package/dist/huazhu/ari-sync.js
CHANGED
|
@@ -43,8 +43,34 @@ export const SYNC_WINDOWS = [
|
|
|
43
43
|
];
|
|
44
44
|
/** What a hotel sells (`/ari/products`: rooms x rates, how many days ahead) - as 0.77.4 kept it. */
|
|
45
45
|
export const SYNC_PRODUCTS_EVERY_MS = 6 * HOUR;
|
|
46
|
-
/**
|
|
47
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Jobs a minute at most (a job is a window's offers + inventory - two calls - or a hotel's products -
|
|
48
|
+
* one): the default, kept as it was until Huazhu answers how many calls its price interface takes
|
|
49
|
+
* (question 42, docs/hotel-connectors.md). `data.api.ariSync.maxPerMinute` sets it (0.85.0).
|
|
50
|
+
*/
|
|
51
|
+
export const SYNC_DEFAULT_PER_MINUTE = 12;
|
|
52
|
+
/** One job (a window's offers + inventory, or a hotel's products) starts this long after the last, at the soonest - at the default budget. */
|
|
53
|
+
export const SYNC_GAP_MS = 60_000 / SYNC_DEFAULT_PER_MINUTE;
|
|
54
|
+
/** The most a minute may be set to: past this the limit on calls to Huazhu (2 a second, `OUTBOUND_DEFAULTS`) is what answers. */
|
|
55
|
+
export const SYNC_MAX_PER_MINUTE = 60;
|
|
56
|
+
/**
|
|
57
|
+
* How many hotels the copy keeps in memory at once; the rest stay on disk and are read when a
|
|
58
|
+
* guest looks at them or a job pulls them (0.85.0: one hotel's file is ~240 KB for 24 products, 400
|
|
59
|
+
* of them were 250 MB of heap).
|
|
60
|
+
*/
|
|
61
|
+
export const SYNC_RESIDENT_HOTELS = 120;
|
|
62
|
+
/** The hotels the schedule covers are chosen again this often (and when the catalog changes). */
|
|
63
|
+
export const SYNC_PLAN_EVERY_MS = 5 * 60_000;
|
|
64
|
+
/** Jobs a day that the schedule plans to use; the rest of the budget is for retries and `soon` pulls. */
|
|
65
|
+
export const SYNC_PLAN_SHARE = 0.9;
|
|
66
|
+
/** A hotel looked at within this long is "hot": its first pulls go before the other hotels'. */
|
|
67
|
+
export const SYNC_HOT_MS = 24 * HOUR;
|
|
68
|
+
/** The `seen` list is written at most this often, and keeps hotels seen within this long. */
|
|
69
|
+
const SEEN_SAVE_EVERY_MS = 5 * 60_000;
|
|
70
|
+
const SEEN_KEEP_MS = 14 * 24 * HOUR;
|
|
71
|
+
const SEEN_KEEP_MAX = 5_000;
|
|
72
|
+
/** How far the far windows (31 days on) and what a hotel sells are stretched when the budget cannot cover every hotel. */
|
|
73
|
+
export const SYNC_STRETCH = 2;
|
|
48
74
|
/** A job our own limit on calls did not send at all is tried again this long after. */
|
|
49
75
|
export const SYNC_NOT_SENT_RETRY_MS = 60_000;
|
|
50
76
|
/**
|
|
@@ -62,7 +88,10 @@ const LOOK_AT_LEAST_EVERY_MS = 60_000;
|
|
|
62
88
|
*/
|
|
63
89
|
export const syncServedForMs = (everyMs) => 2 * everyMs + HOUR;
|
|
64
90
|
const KINDS = ['P', 'W0', 'W1', 'W2', 'W3'];
|
|
91
|
+
/** The rule's own period for a job - never shorter than this, whatever the budget. */
|
|
65
92
|
const everyOf = (k) => k === 'P' ? SYNC_PRODUCTS_EVERY_MS : SYNC_WINDOWS[windowIndex(k)].everyMs;
|
|
93
|
+
/** Jobs a day, per hotel, with the far windows and the products stretched `stretch` times (the nearest week never is). */
|
|
94
|
+
export const jobsPerDay = (stretch) => KINDS.reduce((n, k) => n + (24 * HOUR) / (everyOf(k) * (k === 'W0' ? 1 : stretch)), 0);
|
|
66
95
|
const windowIndex = (k) => Number(k.slice(1));
|
|
67
96
|
/** Where in its period a hotel's job falls, past the hotel's own place: its windows do not start together. */
|
|
68
97
|
const SHIFT = { W0: 0, W1: 0.2, W2: 0.4, W3: 0.6, P: 0.8 };
|
|
@@ -131,6 +160,23 @@ export class AriSync {
|
|
|
131
160
|
/** Jobs not sent at all, and when to try them again. */
|
|
132
161
|
retryAt = new Map();
|
|
133
162
|
passes = new Map();
|
|
163
|
+
/** When a guest last looked at each hotel (0.85.0): what the schedule covers first. */
|
|
164
|
+
seenAt = new Map();
|
|
165
|
+
seenSavedAt = 0;
|
|
166
|
+
seenDirty = false;
|
|
167
|
+
/** The hotels the schedule pulls, sorted - all of them when the budget allows (`replan`). */
|
|
168
|
+
covered = [];
|
|
169
|
+
coverIndex = new Map();
|
|
170
|
+
stretch = 1;
|
|
171
|
+
plannedAt;
|
|
172
|
+
plannedFor;
|
|
173
|
+
/** Hotels with their chunks in memory, least recently used first. */
|
|
174
|
+
resident = new Set();
|
|
175
|
+
/** The hotel whose job is running: its chunks stay in memory until it has written them. */
|
|
176
|
+
busy;
|
|
177
|
+
seenFailing = false;
|
|
178
|
+
gapMs;
|
|
179
|
+
maxPerMinute;
|
|
134
180
|
lastFull = new Map();
|
|
135
181
|
ranked = [];
|
|
136
182
|
rankOf = new Map();
|
|
@@ -154,7 +200,11 @@ export class AriSync {
|
|
|
154
200
|
fellBackCount = 0;
|
|
155
201
|
constructor(opts) {
|
|
156
202
|
this.opts = opts;
|
|
203
|
+
const per = opts.maxPerMinute ?? SYNC_DEFAULT_PER_MINUTE;
|
|
204
|
+
this.maxPerMinute = Math.min(SYNC_MAX_PER_MINUTE, Math.max(1, per));
|
|
205
|
+
this.gapMs = Math.ceil(60_000 / this.maxPerMinute);
|
|
157
206
|
this.load();
|
|
207
|
+
this.loadSeen();
|
|
158
208
|
}
|
|
159
209
|
log(line) {
|
|
160
210
|
(this.opts.log ?? console.log)(`[huazhu sync] ${line}`);
|
|
@@ -167,7 +217,7 @@ export class AriSync {
|
|
|
167
217
|
*/
|
|
168
218
|
ari(hotelId, start, end) {
|
|
169
219
|
const k = this.kept.get(hotelId);
|
|
170
|
-
if (!k
|
|
220
|
+
if (!k || end < start || !this.chunksLoaded(hotelId, k).length)
|
|
171
221
|
return undefined;
|
|
172
222
|
const t = this.opts.now();
|
|
173
223
|
const owners = [];
|
|
@@ -177,7 +227,7 @@ export class AriSync {
|
|
|
177
227
|
if (c.start <= d && d <= c.end && (!best || c.at > best.at))
|
|
178
228
|
best = c;
|
|
179
229
|
const w = best ? SYNC_WINDOWS[best.w] : undefined;
|
|
180
|
-
if (!best || !w || t - best.at > syncServedForMs(w.everyMs))
|
|
230
|
+
if (!best || !w || t - best.at > syncServedForMs(w.everyMs * this.stretchOf(best.w)))
|
|
181
231
|
return undefined;
|
|
182
232
|
owners.push(best);
|
|
183
233
|
}
|
|
@@ -205,7 +255,7 @@ export class AriSync {
|
|
|
205
255
|
/** What a hotel sells and how far ahead, from the copy - or undefined when it holds none fresh enough. */
|
|
206
256
|
products(hotelId) {
|
|
207
257
|
const p = this.kept.get(hotelId)?.products;
|
|
208
|
-
if (!p || this.opts.now() - p.at > syncServedForMs(SYNC_PRODUCTS_EVERY_MS))
|
|
258
|
+
if (!p || this.opts.now() - p.at > syncServedForMs(SYNC_PRODUCTS_EVERY_MS * this.stretch))
|
|
209
259
|
return undefined;
|
|
210
260
|
return { value: p.value, sellDays: p.sellDays };
|
|
211
261
|
}
|
|
@@ -216,6 +266,27 @@ export class AriSync {
|
|
|
216
266
|
else
|
|
217
267
|
this.fellBackCount += 1;
|
|
218
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* A guest looked at this hotel (a list tile, its page, a comparison, a plan, a booking): what the
|
|
271
|
+
* schedule covers first when the budget cannot cover every hotel. Kept across restarts.
|
|
272
|
+
*/
|
|
273
|
+
seen(hotelId) {
|
|
274
|
+
if (!SAFE_ID.test(hotelId))
|
|
275
|
+
return;
|
|
276
|
+
const t = this.opts.now();
|
|
277
|
+
// A look a minute is as good as every look; no write for the rest.
|
|
278
|
+
if (t - (this.seenAt.get(hotelId) ?? 0) < 60_000)
|
|
279
|
+
return;
|
|
280
|
+
this.seenAt.set(hotelId, t);
|
|
281
|
+
this.seenDirty = true;
|
|
282
|
+
}
|
|
283
|
+
/** How many periods a window's pulls are stretched (the nearest week never is). */
|
|
284
|
+
stretchOf(w) {
|
|
285
|
+
return w === 0 ? 1 : this.stretch;
|
|
286
|
+
}
|
|
287
|
+
every(k) {
|
|
288
|
+
return everyOf(k) * (k === 'W0' ? 1 : this.stretch);
|
|
289
|
+
}
|
|
219
290
|
/**
|
|
220
291
|
* Huazhu's prebook (or booking) said a stay's price moved, or no room is left: the windows the
|
|
221
292
|
* stay is in are asked at the first time the rule allows - a period after they were last asked
|
|
@@ -259,9 +330,13 @@ export class AriSync {
|
|
|
259
330
|
for (const id of [...this.kept.keys()])
|
|
260
331
|
if (!this.rankOf.has(id))
|
|
261
332
|
this.drop(id);
|
|
333
|
+
for (const id of [...this.seenAt.keys()])
|
|
334
|
+
if (!this.rankOf.has(id))
|
|
335
|
+
this.seenAt.delete(id);
|
|
262
336
|
}
|
|
263
337
|
drop(hotelId) {
|
|
264
338
|
this.kept.delete(hotelId);
|
|
339
|
+
this.resident.delete(hotelId);
|
|
265
340
|
for (const k of KINDS) {
|
|
266
341
|
this.soonJobs.delete(`${hotelId}|${k}`);
|
|
267
342
|
this.retryAt.delete(`${hotelId}|${k}`);
|
|
@@ -273,11 +348,69 @@ export class AriSync {
|
|
|
273
348
|
// Gone from the schedule either way.
|
|
274
349
|
}
|
|
275
350
|
}
|
|
276
|
-
/**
|
|
351
|
+
/**
|
|
352
|
+
* The hotels the schedule pulls, chosen again every `SYNC_PLAN_EVERY_MS` and when the catalog
|
|
353
|
+
* changes (0.85.0, robin 11-26: 15,000 hotels, and a budget of calls that holds 380).
|
|
354
|
+
*
|
|
355
|
+
* The budget is `maxPerMinute` jobs a minute, `SYNC_PLAN_SHARE` of it planned. A hotel at Huazhu's
|
|
356
|
+
* own pace is `jobsPerDay(1)` = 45 jobs a day. If every hotel fits, every hotel is pulled, as ever.
|
|
357
|
+
* If not, the far windows and what a hotel sells are pulled `SYNC_STRETCH` times less often
|
|
358
|
+
* (never more often than the rule: a longer period is allowed, and a copy that old is served for
|
|
359
|
+
* twice its own period) and the hotels covered are those guests looked at most recently, then
|
|
360
|
+
* those already in the copy, then in id order. The others are not pulled - a copy older than its
|
|
361
|
+
* time would only be dropped when asked for - and their prices are asked when a guest looks (a
|
|
362
|
+
* capped few at a time, backend.ts); a hotel looked at comes into the schedule at the next plan.
|
|
363
|
+
*/
|
|
364
|
+
replan(t) {
|
|
365
|
+
this.plannedAt = t;
|
|
366
|
+
this.plannedFor = this.hotelsAt;
|
|
367
|
+
const all = this.hotels();
|
|
368
|
+
const cap = this.maxPerMinute * 60 * 24 * SYNC_PLAN_SHARE;
|
|
369
|
+
// Stretched once the hotels do not fit; back only when they fit with a margin (no flapping at the edge).
|
|
370
|
+
const fits1 = all.length * jobsPerDay(1) <= cap * (this.stretch > 1 ? 0.85 : 1);
|
|
371
|
+
this.stretch = fits1 ? 1 : SYNC_STRETCH;
|
|
372
|
+
const room = Math.max(1, Math.floor(cap / jobsPerDay(this.stretch)));
|
|
373
|
+
let chosen = all;
|
|
374
|
+
if (all.length > room) {
|
|
375
|
+
const hasCopy = (id) => {
|
|
376
|
+
const k = this.kept.get(id);
|
|
377
|
+
return k && (k.ok.W0 !== undefined || k.ok.P !== undefined) ? 1 : 0;
|
|
378
|
+
};
|
|
379
|
+
chosen = [...all]
|
|
380
|
+
.sort((a, b) => (this.seenAt.get(b) ?? 0) - (this.seenAt.get(a) ?? 0) ||
|
|
381
|
+
hasCopy(b) - hasCopy(a) ||
|
|
382
|
+
(this.rankOf.get(a) ?? 0) - (this.rankOf.get(b) ?? 0))
|
|
383
|
+
.slice(0, room)
|
|
384
|
+
.sort();
|
|
385
|
+
}
|
|
386
|
+
const before = this.covered.length;
|
|
387
|
+
this.covered = chosen;
|
|
388
|
+
this.coverIndex = new Map(chosen.map((id, i) => [id, i]));
|
|
389
|
+
if (before !== chosen.length && (before > 0 || chosen.length < all.length))
|
|
390
|
+
this.log(`budget ${this.maxPerMinute} jobs a minute: covering ${chosen.length} of ${all.length} hotels${this.stretch > 1 ? `, far windows and products every ${this.stretch}x their period` : ''}`);
|
|
391
|
+
// A hotel left out for longer than its copy could be served is dropped from the copy (its file too).
|
|
392
|
+
const oldest = syncServedForMs(24 * HOUR * this.stretch);
|
|
393
|
+
for (const [id, k] of [...this.kept]) {
|
|
394
|
+
if (this.coverIndex.has(id))
|
|
395
|
+
continue;
|
|
396
|
+
// ⚠️ THE LAST ASK, ANSWERED OR NOT (`tried`): a hotel that never answered was asked all the same,
|
|
397
|
+
// and forgetting that asks it again at once when it comes back - sooner than the rule.
|
|
398
|
+
const last = Math.max(0, ...KINDS.map((x) => Math.max(k.ok[x] ?? 0, k.tried[x] ?? 0)));
|
|
399
|
+
if (t - last > oldest)
|
|
400
|
+
this.drop(id);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
ensurePlan(t) {
|
|
404
|
+
if (this.plannedAt === undefined ||
|
|
405
|
+
this.plannedFor !== this.hotelsAt ||
|
|
406
|
+
t - this.plannedAt >= SYNC_PLAN_EVERY_MS)
|
|
407
|
+
this.replan(t);
|
|
408
|
+
}
|
|
409
|
+
/** The job's own times: its place in the hotels covered, and its window, of each period. */
|
|
277
410
|
phaseOf(hotelId, k) {
|
|
278
|
-
const n = Math.max(1, this.
|
|
279
|
-
const rank = this.
|
|
280
|
-
return ((rank + SHIFT[k]) / n) *
|
|
411
|
+
const n = Math.max(1, this.covered.length);
|
|
412
|
+
const rank = this.coverIndex.get(hotelId) ?? 0;
|
|
413
|
+
return ((rank + SHIFT[k]) / n) * this.every(k);
|
|
281
414
|
}
|
|
282
415
|
/**
|
|
283
416
|
* When a job is due. Never asked: at the start (the first pull goes in turn, `pick`). Asked
|
|
@@ -293,8 +426,10 @@ export class AriSync {
|
|
|
293
426
|
const last = kept?.tried[k];
|
|
294
427
|
if (last === undefined)
|
|
295
428
|
return { due: Math.max(this.startedAt ?? t, retry), first: true };
|
|
296
|
-
|
|
297
|
-
const
|
|
429
|
+
// ⚠️ THE RULE'S OWN PERIOD IS THE SHORTEST (`everyOf`); a stretched one (budget short) is longer.
|
|
430
|
+
const rule = everyOf(k);
|
|
431
|
+
const every = this.every(k);
|
|
432
|
+
const earliest = last + rule;
|
|
298
433
|
if (this.soonJobs.has(key))
|
|
299
434
|
return { due: Math.max(earliest, retry), first: false };
|
|
300
435
|
// ⚠️ A FAILED ASK IS ASKED AGAIN AT THE FIRST TIME THE RULE ALLOWS (0.80.2), not at the job's
|
|
@@ -306,16 +441,22 @@ export class AriSync {
|
|
|
306
441
|
if (ok === undefined || ok < last)
|
|
307
442
|
return { due: Math.max(earliest, retry), first: false };
|
|
308
443
|
const phase = this.phaseOf(hotelId, k);
|
|
309
|
-
const slot = phase + Math.ceil((
|
|
310
|
-
return { due: Math.max(slot,
|
|
444
|
+
const slot = phase + Math.ceil((last + every - every * SYNC_SLOT_GRACE - phase) / every) * every;
|
|
445
|
+
return { due: Math.max(slot, last + every, retry), first: false };
|
|
311
446
|
}
|
|
312
|
-
/** The job to run at `t`: the first pulls in turn, then the one due longest. */
|
|
447
|
+
/** The job to run at `t`: the first pulls in turn (hot hotels' first), then the one due longest. */
|
|
313
448
|
pick(t) {
|
|
314
449
|
let best;
|
|
315
450
|
const before = (a, b) => {
|
|
316
451
|
if (a.first !== b.first)
|
|
317
452
|
return a.first;
|
|
318
453
|
if (a.first) {
|
|
454
|
+
// A hotel guests looked at in the last day is pulled before the rest - whatever its window.
|
|
455
|
+
if (a.hot !== b.hot)
|
|
456
|
+
return a.hot;
|
|
457
|
+
// ...the one looked at last, first - all of its jobs, then the next one's.
|
|
458
|
+
if (a.hot && a.seen !== b.seen)
|
|
459
|
+
return a.seen > b.seen;
|
|
319
460
|
if (STAGE[a.k] !== STAGE[b.k])
|
|
320
461
|
return STAGE[a.k] < STAGE[b.k];
|
|
321
462
|
const ra = this.rankOf.get(a.hotelId) ?? 0;
|
|
@@ -324,17 +465,21 @@ export class AriSync {
|
|
|
324
465
|
return ra < rb;
|
|
325
466
|
return ORDER[a.k] < ORDER[b.k];
|
|
326
467
|
}
|
|
327
|
-
|
|
468
|
+
// Overdue by the most first; the nearer window first when they are as late.
|
|
469
|
+
return a.due !== b.due ? a.due < b.due : ORDER[a.k] < ORDER[b.k];
|
|
328
470
|
};
|
|
329
|
-
for (const hotelId of this.
|
|
471
|
+
for (const hotelId of this.covered) {
|
|
472
|
+
const seen = this.seenAt.get(hotelId) ?? Number.NEGATIVE_INFINITY;
|
|
473
|
+
const hot = t - seen <= SYNC_HOT_MS;
|
|
330
474
|
for (const k of KINDS) {
|
|
331
475
|
const { due, first } = this.dueOf(hotelId, k, t);
|
|
332
476
|
if (due > t)
|
|
333
477
|
continue;
|
|
334
|
-
const c = { hotelId, k, due, first };
|
|
478
|
+
const c = { hotelId, k, due, first, hot, seen };
|
|
335
479
|
if (!best || before(c, best))
|
|
336
480
|
best = c;
|
|
337
481
|
}
|
|
482
|
+
}
|
|
338
483
|
return best ? { hotelId: best.hotelId, k: best.k } : undefined;
|
|
339
484
|
}
|
|
340
485
|
/** When `tick` will next have a job to start - for a test's clock, and nothing else. */
|
|
@@ -345,26 +490,30 @@ export class AriSync {
|
|
|
345
490
|
if (hotels.length === 0)
|
|
346
491
|
return undefined;
|
|
347
492
|
const t = this.opts.now();
|
|
493
|
+
this.ensurePlan(t);
|
|
348
494
|
let min = Number.POSITIVE_INFINITY;
|
|
349
|
-
for (const hotelId of
|
|
495
|
+
for (const hotelId of this.covered)
|
|
350
496
|
for (const k of KINDS)
|
|
351
497
|
min = Math.min(min, this.dueOf(hotelId, k, t).due);
|
|
352
498
|
return Math.max(min, this.nextStartAt, this.opts.pausedUntil());
|
|
353
499
|
}
|
|
354
500
|
/**
|
|
355
501
|
* One look at the schedule: starts the job most due, if one is and the last started at least
|
|
356
|
-
* `
|
|
357
|
-
* after a 流控, or before the catalog has hotels. Answers
|
|
502
|
+
* the budget's gap ago (`maxPerMinute`; 5 s at the default), and waits for it. Nothing while a
|
|
503
|
+
* job runs, while price calls are held after a 流控, or before the catalog has hotels. Answers
|
|
504
|
+
* whether a job ran.
|
|
358
505
|
*/
|
|
359
506
|
async tick() {
|
|
360
507
|
if (this.running || this.stopped)
|
|
361
508
|
return false;
|
|
362
509
|
const t = this.opts.now();
|
|
510
|
+
this.saveSeen(t);
|
|
363
511
|
if (t < this.nextStartAt || t < this.opts.pausedUntil())
|
|
364
512
|
return false;
|
|
365
513
|
if (this.hotels().length === 0)
|
|
366
514
|
return false;
|
|
367
515
|
this.reconcile();
|
|
516
|
+
this.ensurePlan(t);
|
|
368
517
|
if (this.startedAt === undefined) {
|
|
369
518
|
this.startedAt = t;
|
|
370
519
|
for (const k of KINDS)
|
|
@@ -374,7 +523,7 @@ export class AriSync {
|
|
|
374
523
|
if (!job)
|
|
375
524
|
return false;
|
|
376
525
|
this.running = true;
|
|
377
|
-
this.nextStartAt = t +
|
|
526
|
+
this.nextStartAt = t + this.gapMs;
|
|
378
527
|
const run = this.run(job.hotelId, job.k).finally(() => {
|
|
379
528
|
this.running = false;
|
|
380
529
|
});
|
|
@@ -390,7 +539,7 @@ export class AriSync {
|
|
|
390
539
|
start() {
|
|
391
540
|
if (this.timer || this.stopped)
|
|
392
541
|
return;
|
|
393
|
-
this.log(`on: ${SYNC_WINDOWS.map((w) => `${w.name} ${w.from}-${w.to} days every ${w.everyMs / HOUR} h`).join(', ')}, products every ${SYNC_PRODUCTS_EVERY_MS / HOUR} h; ${this.kept.size} hotel(s) in the copy at ${this.opts.dir}`);
|
|
542
|
+
this.log(`on: ${SYNC_WINDOWS.map((w) => `${w.name} ${w.from}-${w.to} days every ${w.everyMs / HOUR} h`).join(', ')}, products every ${SYNC_PRODUCTS_EVERY_MS / HOUR} h, at most ${this.maxPerMinute} jobs a minute; ${this.kept.size} hotel(s) in the copy at ${this.opts.dir}`);
|
|
394
543
|
this.lookLater(0);
|
|
395
544
|
}
|
|
396
545
|
lookLater(ms) {
|
|
@@ -418,16 +567,62 @@ export class AriSync {
|
|
|
418
567
|
clearTimeout(this.timer);
|
|
419
568
|
this.timer = undefined;
|
|
420
569
|
await this.current?.catch(() => { });
|
|
570
|
+
this.saveSeen(this.opts.now(), true);
|
|
421
571
|
}
|
|
422
572
|
// ----------------------------------------------------------------- jobs ----
|
|
423
573
|
keptOf(hotelId) {
|
|
424
574
|
let k = this.kept.get(hotelId);
|
|
425
575
|
if (!k) {
|
|
426
|
-
k = { tried: {}, ok: {}, why: {}, chunks: [], bytes: 0 };
|
|
576
|
+
k = { tried: {}, ok: {}, why: {}, chunks: [], bytes: 0, loaded: true };
|
|
427
577
|
this.kept.set(hotelId, k);
|
|
428
578
|
}
|
|
429
579
|
return k;
|
|
430
580
|
}
|
|
581
|
+
/**
|
|
582
|
+
* A hotel's chunks, in memory: read from its file when they are not (the copy holds
|
|
583
|
+
* `SYNC_RESIDENT_HOTELS` hotels' at a time, the least recently used given up first). A file that
|
|
584
|
+
* cannot be read leaves the hotel as if never pulled - its jobs come due as at a first start.
|
|
585
|
+
*/
|
|
586
|
+
chunksLoaded(hotelId, k) {
|
|
587
|
+
if (!k.loaded) {
|
|
588
|
+
try {
|
|
589
|
+
const raw = readFileSync(this.fileOf(hotelId), 'utf8');
|
|
590
|
+
const f = JSON.parse(raw);
|
|
591
|
+
k.chunks = Array.isArray(f.chunks) ? f.chunks.filter(isChunk) : [];
|
|
592
|
+
k.bytes = Buffer.byteLength(raw);
|
|
593
|
+
k.loaded = true;
|
|
594
|
+
}
|
|
595
|
+
catch (err) {
|
|
596
|
+
// ⚠️ NO FILE IS EMPTY; A FILE THAT CANNOT BE READ IS NOT (0.85.0 review): left unloaded, and never
|
|
597
|
+
// written over - what is on the disk may be all the prices there are.
|
|
598
|
+
if (err?.code === 'ENOENT') {
|
|
599
|
+
k.chunks = [];
|
|
600
|
+
k.loaded = true;
|
|
601
|
+
}
|
|
602
|
+
else {
|
|
603
|
+
k.chunks = [];
|
|
604
|
+
return k.chunks;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
this.resident.delete(hotelId);
|
|
609
|
+
this.resident.add(hotelId);
|
|
610
|
+
const most = Math.max(1, this.opts.residentHotels ?? SYNC_RESIDENT_HOTELS);
|
|
611
|
+
for (const id of this.resident) {
|
|
612
|
+
if (this.resident.size <= most)
|
|
613
|
+
break;
|
|
614
|
+
// ⚠️ NEVER THE HOTEL A JOB IS PULLING: its chunks are what the job writes back (0.85.0 review).
|
|
615
|
+
if (id === hotelId || id === this.busy)
|
|
616
|
+
continue;
|
|
617
|
+
const other = this.kept.get(id);
|
|
618
|
+
this.resident.delete(id);
|
|
619
|
+
if (other) {
|
|
620
|
+
other.chunks = [];
|
|
621
|
+
other.loaded = false;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return k.chunks;
|
|
625
|
+
}
|
|
431
626
|
sentNow(n) {
|
|
432
627
|
const t = this.opts.now();
|
|
433
628
|
for (let i = 0; i < n; i += 1)
|
|
@@ -444,6 +639,23 @@ export class AriSync {
|
|
|
444
639
|
async run(hotelId, k) {
|
|
445
640
|
const key = `${hotelId}|${k}`;
|
|
446
641
|
const kept = this.keptOf(hotelId);
|
|
642
|
+
this.busy = hotelId;
|
|
643
|
+
try {
|
|
644
|
+
await this.runLoaded(hotelId, k, key, kept);
|
|
645
|
+
}
|
|
646
|
+
finally {
|
|
647
|
+
this.busy = undefined;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
async runLoaded(hotelId, k, key, kept) {
|
|
651
|
+
this.chunksLoaded(hotelId, kept);
|
|
652
|
+
if (!kept.loaded) {
|
|
653
|
+
// The hotel's file is there and cannot be read: nothing is pulled over it, and nothing was asked.
|
|
654
|
+
if (!this.retryAt.has(key))
|
|
655
|
+
this.log(`${hotelId} ${labelOf(k)}: its file cannot be read; not pulled over it, tried again every minute`);
|
|
656
|
+
this.retryAt.set(key, this.opts.now() + SYNC_NOT_SENT_RETRY_MS);
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
447
659
|
let failure;
|
|
448
660
|
let sent = 0;
|
|
449
661
|
/** The dates a window asked for, for the log: " 2027-01-20..2027-02-18". */
|
|
@@ -573,8 +785,8 @@ export class AriSync {
|
|
|
573
785
|
pass.ok.add(hotelId);
|
|
574
786
|
else
|
|
575
787
|
pass.ok.delete(hotelId);
|
|
576
|
-
const hotels = this.
|
|
577
|
-
if (!hotels.every((id) => pass.tried.has(id)))
|
|
788
|
+
const hotels = this.covered;
|
|
789
|
+
if (hotels.length === 0 || !hotels.every((id) => pass.tried.has(id)))
|
|
578
790
|
return;
|
|
579
791
|
const done = hotels.filter((id) => pass.ok.has(id)).length;
|
|
580
792
|
this.log(`${labelOf(k)} ${done}/${hotels.length} hotels in ${Math.round((t - pass.startedAt) / 60_000)} min, ${pass.flow} 流控`);
|
|
@@ -590,6 +802,11 @@ export class AriSync {
|
|
|
590
802
|
const kept = this.kept.get(hotelId);
|
|
591
803
|
if (!kept)
|
|
592
804
|
return;
|
|
805
|
+
// ⚠️ NEVER WRITTEN WITHOUT ITS CHUNKS: read back first, and left alone if they cannot be.
|
|
806
|
+
if (!kept.loaded)
|
|
807
|
+
this.chunksLoaded(hotelId, kept);
|
|
808
|
+
if (!kept.loaded)
|
|
809
|
+
return;
|
|
593
810
|
const file = {
|
|
594
811
|
version: 1,
|
|
595
812
|
hotelId,
|
|
@@ -650,8 +867,10 @@ export class AriSync {
|
|
|
650
867
|
ok: timesOf(f.ok),
|
|
651
868
|
why: whysOf(f.why),
|
|
652
869
|
...(isProducts(f.products) ? { products: f.products } : {}),
|
|
653
|
-
|
|
870
|
+
// Read again when wanted (`chunksLoaded`): 400 hotels' chunks were 250 MB of heap.
|
|
871
|
+
chunks: [],
|
|
654
872
|
bytes: Buffer.byteLength(raw),
|
|
873
|
+
loaded: false,
|
|
655
874
|
});
|
|
656
875
|
}
|
|
657
876
|
catch {
|
|
@@ -659,12 +878,56 @@ export class AriSync {
|
|
|
659
878
|
}
|
|
660
879
|
}
|
|
661
880
|
}
|
|
881
|
+
/** The hotels guests looked at, kept beside the copy (`_seen.json`, which no hotel's file is named). */
|
|
882
|
+
seenFile() {
|
|
883
|
+
return join(this.opts.dir, '_seen.json');
|
|
884
|
+
}
|
|
885
|
+
loadSeen() {
|
|
886
|
+
try {
|
|
887
|
+
const f = JSON.parse(readFileSync(this.seenFile(), 'utf8'));
|
|
888
|
+
for (const [id, at] of Object.entries(f.seen ?? {}))
|
|
889
|
+
if (SAFE_ID.test(id) && typeof at === 'number' && Number.isFinite(at))
|
|
890
|
+
this.seenAt.set(id, at);
|
|
891
|
+
}
|
|
892
|
+
catch {
|
|
893
|
+
// Nothing seen yet.
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
/** Written when something changed, at most every five minutes (and once more at `stop`). */
|
|
897
|
+
saveSeen(t, force = false) {
|
|
898
|
+
if (!this.seenDirty || (!force && t - this.seenSavedAt < SEEN_SAVE_EVERY_MS))
|
|
899
|
+
return;
|
|
900
|
+
const keep = [...this.seenAt.entries()]
|
|
901
|
+
.filter(([, at]) => t - at <= SEEN_KEEP_MS)
|
|
902
|
+
.sort((a, b) => b[1] - a[1])
|
|
903
|
+
.slice(0, SEEN_KEEP_MAX);
|
|
904
|
+
try {
|
|
905
|
+
mkdirSync(this.opts.dir, { recursive: true });
|
|
906
|
+
const tmp = `${this.seenFile()}.${process.pid}.tmp`;
|
|
907
|
+
writeFileSync(tmp, JSON.stringify({ version: 1, seen: Object.fromEntries(keep) }));
|
|
908
|
+
renameSync(tmp, this.seenFile());
|
|
909
|
+
this.seenSavedAt = t;
|
|
910
|
+
this.seenDirty = false;
|
|
911
|
+
this.seenFailing = false;
|
|
912
|
+
}
|
|
913
|
+
catch (err) {
|
|
914
|
+
// Kept in memory and tried again at the next look; only a restart would forget it.
|
|
915
|
+
this.seenSavedAt = t - SEEN_SAVE_EVERY_MS + 60_000;
|
|
916
|
+
if (!this.seenFailing)
|
|
917
|
+
this.log(`the hotels guests looked at could not be written: ${err.message}`);
|
|
918
|
+
this.seenFailing = true;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
662
921
|
// --------------------------------------------------------------- health ----
|
|
663
922
|
health() {
|
|
664
923
|
const t = this.opts.now();
|
|
665
|
-
const
|
|
924
|
+
const all = this.hotels();
|
|
925
|
+
// The hotels the schedule covers (all of them, unless the budget is short - see `replan`).
|
|
926
|
+
if (all.length && this.plannedAt === undefined)
|
|
927
|
+
this.replan(t);
|
|
928
|
+
const hotels = this.covered;
|
|
666
929
|
const of = (k) => {
|
|
667
|
-
const every =
|
|
930
|
+
const every = this.every(k);
|
|
668
931
|
let due = 0;
|
|
669
932
|
let done = 0;
|
|
670
933
|
let never = 0;
|
|
@@ -706,6 +969,18 @@ export class AriSync {
|
|
|
706
969
|
return {
|
|
707
970
|
on: true,
|
|
708
971
|
hotels: hotels.length,
|
|
972
|
+
// The budget of calls and what it covers (0.85.0): `jobsPerDayWanted` is what every hotel in the
|
|
973
|
+
// catalog would take at Huazhu's own pace, `jobsPerDayBudget` what the budget gives.
|
|
974
|
+
budget: {
|
|
975
|
+
maxPerMinute: this.maxPerMinute,
|
|
976
|
+
hotelsInCatalog: all.length,
|
|
977
|
+
hotelsCovered: hotels.length,
|
|
978
|
+
farWindowsStretch: this.stretch,
|
|
979
|
+
jobsPerDayWanted: Math.round(all.length * jobsPerDay(1)),
|
|
980
|
+
jobsPerDayBudget: this.maxPerMinute * 60 * 24,
|
|
981
|
+
hotelsSeenLast24h: [...this.seenAt.values()].filter((at) => t - at <= SYNC_HOT_MS).length,
|
|
982
|
+
hotelsInMemory: this.resident.size,
|
|
983
|
+
},
|
|
709
984
|
windows: { W0: of('W0'), W1: of('W1'), W2: of('W2'), W3: of('W3') },
|
|
710
985
|
products: of('P'),
|
|
711
986
|
callsLastHour: this.sent.length,
|