Skip to content

Commit d845001

Browse files
committed
Manually set last seen timestamp as sync integrates
- This was prev being done within sync itself, though the problem there was that the timestamp was set before the updates were integrated in the local DB. Which meant that it could get into states where sync got interrupted between when the timestamp was written and when the updates were integrated, thus skipping those updates the next time sync started
1 parent 6606faa commit d845001

4 files changed

Lines changed: 50 additions & 15 deletions

File tree

src/background-script/setup.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,12 @@ export function createBackgroundModules(options: {
455455
backend:
456456
options.personalCloudBackend ??
457457
new FirestorePersonalCloudBackend({
458+
getFirebase,
459+
getServerStorageManager,
458460
personalCloudService: firebaseService<PersonalCloudService>(
459461
'personalCloud',
460462
callFirebaseFunction,
461463
),
462-
getServerStorageManager,
463464
getCurrentSchemaVersion: () =>
464465
getCurrentSchemaVersion(options.storageManager),
465466
userChanges: () => authChanges(auth.authService),
@@ -475,10 +476,8 @@ export function createBackgroundModules(options: {
475476
.doc(currentUser.id)
476477
.collection('objects')
477478
},
478-
getLastUpdateSeenTime: () =>
479+
getLastUpdateProcessedTime: () =>
479480
personalCloudSettingStore.get('lastSeen'),
480-
setLastUpdateSeenTime: (lastSeen) =>
481-
personalCloudSettingStore.set('lastSeen', lastSeen),
482481
getDeviceId: async () => personalCloud.deviceId!,
483482
getClientDeviceType: () => PersonalDeviceType.DesktopBrowser,
484483
}),

src/personal-cloud/background/index.test.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs'
2-
import { setupSyncBackgroundTest } from './index.tests'
2+
import { setupSyncBackgroundTest, BASE_TIMESTAMP } from './index.tests'
33
import { StorexPersonalCloudBackend } from '@worldbrain/memex-common/lib/personal-cloud/backend/storex'
44
import { TEST_USER } from '@worldbrain/memex-common/lib/authentication/dev'
55
import { BackgroundIntegrationTestSetup } from 'src/tests/integration-tests'
@@ -16,11 +16,6 @@ import {
1616
TEST_PDF_PAGE_TEXTS,
1717
} from 'src/tests/test.data'
1818
import { blobToJson } from 'src/util/blob-utils'
19-
import { Annotation } from '../../annotations/types'
20-
import {
21-
PersonalCloudErrorType,
22-
DataChangeType,
23-
} from '../../../external/@worldbrain/memex-common/ts/personal-cloud/storage/types'
2419

2520
describe('Personal cloud', () => {
2621
const testFullPage = async (testOptions: {
@@ -411,14 +406,35 @@ describe('Personal cloud', () => {
411406
id: 1,
412407
name: 'list 1',
413408
})
409+
410+
expect(
411+
await setups[1].backgroundModules.personalCloud.options.settingStore.get(
412+
'lastSeen',
413+
),
414+
).toEqual(null)
415+
414416
await waitForSync()
417+
418+
expect(
419+
await setups[1].backgroundModules.personalCloud.options.settingStore.get(
420+
'lastSeen',
421+
),
422+
).toEqual(BASE_TIMESTAMP)
423+
415424
await assertCacheEntries([1])
416425

417426
await setups[0].backgroundModules.customLists.createCustomList({
418427
id: 2,
419428
name: 'list 2',
420429
})
421430
await waitForSync()
431+
432+
expect(
433+
await setups[1].backgroundModules.personalCloud.options.settingStore.get(
434+
'lastSeen',
435+
),
436+
).toEqual(BASE_TIMESTAMP + 1)
437+
422438
await assertCacheEntries([2, 1])
423439

424440
await setups[0].backgroundModules.customLists.updateList({
@@ -427,13 +443,27 @@ describe('Personal cloud', () => {
427443
newName: 'list 1 updated',
428444
})
429445
await waitForSync()
446+
447+
expect(
448+
await setups[1].backgroundModules.personalCloud.options.settingStore.get(
449+
'lastSeen',
450+
),
451+
).toEqual(BASE_TIMESTAMP + 2)
452+
430453
await assertCacheEntries([2, 1]) // Updates should not affect cache
431454

432455
await setups[0].backgroundModules.customLists.createCustomList({
433456
id: 3,
434457
name: 'list 3',
435458
})
436459
await waitForSync()
460+
461+
expect(
462+
await setups[1].backgroundModules.personalCloud.options.settingStore.get(
463+
'lastSeen',
464+
),
465+
).toEqual(BASE_TIMESTAMP + 3)
466+
437467
await assertCacheEntries([3, 2, 1])
438468
})
439469
})

src/personal-cloud/background/index.tests.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { STORAGE_VERSIONS } from 'src/storage/constants'
2121
import { createServices } from 'src/services'
2222
import { PersonalDeviceType } from '@worldbrain/memex-common/lib/personal-cloud/storage/types'
2323

24+
export const BASE_TIMESTAMP = 555
25+
2426
const debug = (...args: any[]) => console['log'](...args, '\n\n\n')
2527

2628
type SyncTestSequence = SyncTestStep[]
@@ -282,7 +284,7 @@ export async function setupSyncBackgroundTest(
282284
const serverStorage = await getServerStorage()
283285
const cloudHub = new PersonalCloudHub()
284286

285-
let now = 555
287+
let now = BASE_TIMESTAMP
286288
const getNow = () => now++
287289
const setups: BackgroundIntegrationTestSetup[] = []
288290
for (let i = 0; i < options.deviceCount; ++i) {

src/personal-cloud/background/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,19 @@ export class PersonalCloudBackground {
278278
}
279279

280280
async integrateAllUpdates(): Promise<void> {
281-
const updateBatch = await this.options.backend.bulkDownloadUpdates()
282-
return this.integrateUpdates(updateBatch)
281+
const { backend, settingStore } = this.options
282+
const { batch, lastSeen } = await backend.bulkDownloadUpdates()
283+
await this.integrateUpdates(batch)
284+
await settingStore.set('lastSeen', lastSeen)
283285
}
284286

285287
async integrateContinuously() {
288+
const { backend, settingStore } = this.options
286289
try {
287-
for await (const updates of this.options.backend.streamUpdates()) {
290+
for await (const { batch, lastSeen } of backend.streamUpdates()) {
288291
try {
289-
await this.integrateUpdates(updates)
292+
await this.integrateUpdates(batch)
293+
await settingStore.set('lastSeen', lastSeen)
290294
} catch (err) {
291295
if (this.strictErrorReporting) {
292296
this._integrationError = err

0 commit comments

Comments
 (0)