feat(syncing-server): add content size recalculation job

This commit is contained in:
Karol Sójko 2022-11-11 13:53:18 +01:00
parent 3ad95afa84
commit 7e404ae71a
No known key found for this signature in database
GPG key ID: 78C42DF8267BAF26
6 changed files with 80 additions and 166 deletions

View file

@ -0,0 +1,52 @@
import 'reflect-metadata'
import 'newrelic'
import { Logger } from 'winston'
import { ContainerConfigLoader } from '../src/Bootstrap/Container'
import TYPES from '../src/Bootstrap/Types'
import { Env } from '../src/Bootstrap/Env'
import { DomainEventFactoryInterface } from '../src/Domain/Event/DomainEventFactoryInterface'
import { DomainEventPublisherInterface } from '@standardnotes/domain-events'
const inputArgs = process.argv.slice(2)
const userUuid = inputArgs[0]
const fixContentSize = async (
domainEventFactory: DomainEventFactoryInterface,
domainEventPublisher: DomainEventPublisherInterface,
): Promise<void> => {
await domainEventPublisher.publish(domainEventFactory.createUserContentSizeRecalculationRequestedEvent(userUuid))
}
const container = new ContainerConfigLoader()
void container.load().then((container) => {
const env: Env = new Env()
env.load()
const logger: Logger = container.get(TYPES.Logger)
logger.info(`Starting content size fixing for user ${userUuid} ...`)
if (!userUuid) {
logger.error('No user uuid passed as argument. Skipped.')
process.exit(1)
}
const domainEventFactory: DomainEventFactoryInterface = container.get(TYPES.DomainEventFactory)
const domainEventPublisher: DomainEventPublisherInterface = container.get(TYPES.DomainEventPublisher)
Promise.resolve(fixContentSize(domainEventFactory, domainEventPublisher))
.then(() => {
logger.info('Content size fix complete.')
process.exit(0)
})
.catch((error) => {
logger.error(`Could not finish content size fix: ${error.message}`)
process.exit(1)
})
})

View file

@ -19,6 +19,12 @@ case "$COMMAND" in
yarn workspace @standardnotes/syncing-server worker
;;
'content-size-recalculate' )
echo "Starting Content Size Recalculation..."
USER_UUID=$1 && shift 1
yarn workspace @standardnotes/syncing-server content-size $USER_UUID
;;
* )
echo "Unknown command"
;;

View file

@ -20,6 +20,7 @@
"test": "jest --coverage --config=./jest.config.js --maxWorkers=50%",
"start": "yarn node dist/bin/server.js",
"worker": "yarn node dist/bin/worker.js",
"content-size": "yarn node dist/bin/content.js",
"upgrade:snjs": "yarn ncu -u '@standardnotes/*'"
},
"dependencies": {

View file

@ -1,166 +0,0 @@
import 'reflect-metadata'
import { TimerInterface } from '@standardnotes/time'
import { DomainEventFactory } from './DomainEventFactory'
describe('DomainEventFactory', () => {
let timer: TimerInterface
const createFactory = () => new DomainEventFactory(timer)
beforeEach(() => {
timer = {} as jest.Mocked<TimerInterface>
timer.getUTCDate = jest.fn().mockReturnValue(new Date(1))
})
it('should create a ITEMS_SYNCED event', () => {
expect(
createFactory().createItemsSyncedEvent({
userUuid: '1-2-3',
extensionUrl: 'https://test.com',
extensionId: '2-3-4',
itemUuids: ['3-4-5'],
forceMute: false,
skipFileBackup: false,
source: 'realtime-extensions-sync',
}),
).toEqual({
createdAt: expect.any(Date),
meta: {
correlation: {
userIdentifier: '1-2-3',
userIdentifierType: 'uuid',
},
origin: 'syncing-server',
},
payload: {
userUuid: '1-2-3',
extensionUrl: 'https://test.com',
extensionId: '2-3-4',
itemUuids: ['3-4-5'],
forceMute: false,
skipFileBackup: false,
source: 'realtime-extensions-sync',
},
type: 'ITEMS_SYNCED',
})
})
it('should create a DROPBOX_BACKUP_FAILED event', () => {
expect(createFactory().createDropboxBackupFailedEvent('1-2-3', 'test@test.com')).toEqual({
createdAt: expect.any(Date),
meta: {
correlation: {
userIdentifier: 'test@test.com',
userIdentifierType: 'email',
},
origin: 'syncing-server',
},
payload: {
email: 'test@test.com',
muteCloudEmailsSettingUuid: '1-2-3',
},
type: 'DROPBOX_BACKUP_FAILED',
})
})
it('should create a GOOGLE_DRIVE_BACKUP_FAILED event', () => {
expect(createFactory().createGoogleDriveBackupFailedEvent('1-2-3', 'test@test.com')).toEqual({
createdAt: expect.any(Date),
meta: {
correlation: {
userIdentifier: 'test@test.com',
userIdentifierType: 'email',
},
origin: 'syncing-server',
},
payload: {
email: 'test@test.com',
muteCloudEmailsSettingUuid: '1-2-3',
},
type: 'GOOGLE_DRIVE_BACKUP_FAILED',
})
})
it('should create a ONE_DRIVE_BACKUP_FAILED event', () => {
expect(createFactory().createOneDriveBackupFailedEvent('1-2-3', 'test@test.com')).toEqual({
createdAt: expect.any(Date),
meta: {
correlation: {
userIdentifier: 'test@test.com',
userIdentifierType: 'email',
},
origin: 'syncing-server',
},
payload: {
email: 'test@test.com',
muteCloudEmailsSettingUuid: '1-2-3',
},
type: 'ONE_DRIVE_BACKUP_FAILED',
})
})
it('should create a EMAIL_ARCHIVE_EXTENSION_SYNCED event', () => {
expect(createFactory().createEmailArchiveExtensionSyncedEvent('1-2-3', '2-3-4')).toEqual({
createdAt: expect.any(Date),
meta: {
correlation: {
userIdentifier: '1-2-3',
userIdentifierType: 'uuid',
},
origin: 'syncing-server',
},
payload: {
userUuid: '1-2-3',
extensionId: '2-3-4',
},
type: 'EMAIL_ARCHIVE_EXTENSION_SYNCED',
})
})
it('should create a EMAIL_BACKUP_ATTACHMENT_CREATED event', () => {
expect(
createFactory().createEmailBackupAttachmentCreatedEvent({
backupFileName: 'backup-file',
email: 'test@test.com',
backupFileIndex: 1,
backupFilesTotal: 2,
}),
).toEqual({
createdAt: expect.any(Date),
meta: {
correlation: {
userIdentifier: 'test@test.com',
userIdentifierType: 'email',
},
origin: 'syncing-server',
},
payload: {
backupFileName: 'backup-file',
email: 'test@test.com',
backupFileIndex: 1,
backupFilesTotal: 2,
},
type: 'EMAIL_BACKUP_ATTACHMENT_CREATED',
})
})
it('should create a DUPLICATE_ITEM_SYNCED event', () => {
expect(createFactory().createDuplicateItemSyncedEvent('1-2-3', '2-3-4')).toEqual({
createdAt: expect.any(Date),
meta: {
correlation: {
userIdentifier: '2-3-4',
userIdentifierType: 'uuid',
},
origin: 'syncing-server',
},
payload: {
itemUuid: '1-2-3',
userUuid: '2-3-4',
},
type: 'DUPLICATE_ITEM_SYNCED',
})
})
})

View file

@ -1,3 +1,4 @@
/* istanbul ignore file */
import {
DomainEventService,
DropboxBackupFailedEvent,
@ -7,6 +8,7 @@ import {
GoogleDriveBackupFailedEvent,
ItemsSyncedEvent,
OneDriveBackupFailedEvent,
UserContentSizeRecalculationRequestedEvent,
} from '@standardnotes/domain-events'
import { TimerInterface } from '@standardnotes/time'
import { inject, injectable } from 'inversify'
@ -17,6 +19,23 @@ import { DomainEventFactoryInterface } from './DomainEventFactoryInterface'
export class DomainEventFactory implements DomainEventFactoryInterface {
constructor(@inject(TYPES.Timer) private timer: TimerInterface) {}
createUserContentSizeRecalculationRequestedEvent(userUuid: string): UserContentSizeRecalculationRequestedEvent {
return {
type: 'USER_CONTENT_SIZE_RECALCULATION_REQUESTED',
createdAt: this.timer.getUTCDate(),
meta: {
correlation: {
userIdentifier: userUuid,
userIdentifierType: 'uuid',
},
origin: DomainEventService.SyncingServer,
},
payload: {
userUuid,
},
}
}
createDuplicateItemSyncedEvent(itemUuid: string, userUuid: string): DuplicateItemSyncedEvent {
return {
type: 'DUPLICATE_ITEM_SYNCED',

View file

@ -6,9 +6,11 @@ import {
GoogleDriveBackupFailedEvent,
ItemsSyncedEvent,
OneDriveBackupFailedEvent,
UserContentSizeRecalculationRequestedEvent,
} from '@standardnotes/domain-events'
export interface DomainEventFactoryInterface {
createUserContentSizeRecalculationRequestedEvent(userUuid: string): UserContentSizeRecalculationRequestedEvent
createDropboxBackupFailedEvent(muteCloudEmailsSettingUuid: string, email: string): DropboxBackupFailedEvent
createGoogleDriveBackupFailedEvent(muteCloudEmailsSettingUuid: string, email: string): GoogleDriveBackupFailedEvent
createOneDriveBackupFailedEvent(muteCloudEmailsSettingUuid: string, email: string): OneDriveBackupFailedEvent