-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IND-552] add roundtable task to take fast sync Postgres snapshots ev…
…ery 4 hours (#912)
- Loading branch information
Showing
8 changed files
with
345 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
indexer/services/roundtable/__tests__/tasks/take-fast-sync-snapshot.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import config from '../../src/config'; | ||
import { asMock } from '@dydxprotocol-indexer/dev'; | ||
import { | ||
createDBSnapshot, | ||
getMostRecentDBSnapshotIdentifier, | ||
} from '../../src/helpers/aws'; | ||
import takeFastSyncSnapshotTask from '../../src/tasks/take-fast-sync-snapshot'; | ||
import { DateTime } from 'luxon'; | ||
|
||
jest.mock('../../src/helpers/aws'); | ||
|
||
describe('fast-sync-export-db-snapshot', () => { | ||
const snapshotIdentifier: string = `${config.FAST_SYNC_SNAPSHOT_IDENTIFIER_PREFIX}-postgres-main-staging-2022-05-03-04-16`; | ||
beforeAll(() => { | ||
config.RDS_INSTANCE_NAME = 'postgres-main-staging'; | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
asMock(getMostRecentDBSnapshotIdentifier).mockImplementation( | ||
async () => Promise.resolve(snapshotIdentifier), | ||
); | ||
}); | ||
|
||
afterAll(jest.resetAllMocks); | ||
|
||
it('Last snapshot was taken more than interval ago', async () => { | ||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(createDBSnapshot).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Last snapshot was taken less than interval ago', async () => { | ||
const timestamp: string = DateTime.utc().minus({ minutes: 1 }).toFormat('yyyy-MM-dd-HH-mm'); | ||
asMock(getMostRecentDBSnapshotIdentifier).mockImplementation( | ||
async () => Promise.resolve(`${config.FAST_SYNC_SNAPSHOT_IDENTIFIER_PREFIX}-postgres-main-staging-${timestamp}`), | ||
); | ||
|
||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(createDBSnapshot).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('No existing snapshot', async () => { | ||
asMock(getMostRecentDBSnapshotIdentifier).mockImplementation( | ||
async () => Promise.resolve(undefined), | ||
); | ||
|
||
await takeFastSyncSnapshotTask(); | ||
|
||
expect(createDBSnapshot).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
indexer/services/roundtable/src/tasks/delete-old-fast-sync-snapshots.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { logger, stats } from '@dydxprotocol-indexer/base'; | ||
import RDS from 'aws-sdk/clients/rds'; | ||
|
||
import config from '../config'; | ||
import { deleteOldFastSyncSnapshots } from '../helpers/aws'; | ||
|
||
const statStart: string = `${config.SERVICE_NAME}.delete_old_fast_sync_snapshots`; | ||
|
||
export default async function runTask(): Promise<void> { | ||
const at: string = 'delete-old-fast-sync-snapshots#runTask'; | ||
logger.info({ at, message: 'Starting task.' }); | ||
|
||
const rds: RDS = new RDS(); | ||
|
||
const startDeleteOldSnapshot: number = Date.now(); | ||
// Delete old snapshots. | ||
await deleteOldFastSyncSnapshots(rds); | ||
stats.timing(`${statStart}.deleteOldSnapshots`, Date.now() - startDeleteOldSnapshot); | ||
} |
Oops, something went wrong.