Skip to content

Commit

Permalink
refactor: auto-generate timestamp when reindexing
Browse files Browse the repository at this point in the history
  • Loading branch information
naftis committed Jun 26, 2024
1 parent 0388a55 commit 0adf8e9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/migration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"status:openhim": "migrate-mongo status -f ./src/migrate-mongo-config-openhim.js",
"status:user-mgnt": "migrate-mongo status -f ./src/migrate-mongo-config-user-mgnt.js",
"status:application-config": "migrate-mongo status -f ./src/migrate-mongo-config-application-config.js",
"reindex-search": "tsx src/utils/reindex-search.ts",
"reindex-search": "tsx src/reindex-search.ts",
"precommit": "lint-staged",
"test:compilation": "tsc --noEmit",
"build": "rimraf build && tsc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { format } from 'date-fns'

const SEARCH_URL = process.env.SEARCH_URL || 'http://localhost:9090/'

/**
* Streams MongoDB collections to ElasticSearch documents. Useful when the ElasticSearch schema changes.
*/
export const triggerReindex = async (timestamp: string) => {
export const triggerReindex = async () => {
const response = await fetch(new URL('reindex', SEARCH_URL), {
method: 'POST',
body: JSON.stringify({ timestamp }),
headers: { 'Content-Type': 'application/json' }
method: 'POST'
})

if (!response.ok) {
Expand Down Expand Up @@ -51,9 +48,8 @@ export const checkReindexStatus = async (jobId: string) => {
}

async function main() {
const timestamp = format(new Date(), 'yyyyMMddHHmmss')
console.info(`Reindexing ${timestamp}...`)
const jobId = await triggerReindex(timestamp)
console.info(`Reindexing search...`)
const jobId = await triggerReindex()
await new Promise<void>((resolve, reject) => {
const intervalId = setInterval(async () => {
try {
Expand All @@ -68,7 +64,7 @@ async function main() {
}
}, 1000)
})
console.info(`...done reindexing ${timestamp} (job id ${jobId})`)
console.info(`...done reindexing search with job id ${jobId}`)
}

main()
7 changes: 1 addition & 6 deletions packages/search/src/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,7 @@ export const getRoutes = () => {
handler: reindexHandler,
config: {
tags: ['api'],
auth: false,
validate: {
payload: Joi.object({
timestamp: Joi.string()
})
}
auth: false
}
},
{
Expand Down
7 changes: 3 additions & 4 deletions packages/search/src/features/reindex/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ const indexingStatuses: Record<
> = {}

export async function reindexHandler(
request: Hapi.Request,
_request: Hapi.Request,
h: Hapi.ResponseToolkit
) {
const { timestamp } = request.payload as { timestamp: string }
const jobId = uuid()

process.nextTick(async () => {
try {
indexingStatuses[jobId] = 'started'
await reindex(timestamp)
await reindex()
await updateAliases()
await prune()
indexingStatuses[jobId] = 'completed'
Expand All @@ -41,7 +40,7 @@ export async function reindexHandler(

return h
.response({
message: `ElasticSearch reindexing started for timestamp ${timestamp}`,
message: `ElasticSearch reindexing started for job ${jobId}`,
status: indexingStatuses[jobId] ?? 'accepted',
jobId
})
Expand Down
9 changes: 5 additions & 4 deletions packages/search/src/features/reindex/reindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ import { logger } from '@opencrvs/commons'
import { getEventType } from '@search/utils/event'
import { Transform } from 'stream'
import { orderBy } from 'lodash'
import { format } from 'date-fns'

const eventTransformers = {
[EVENT_TYPE.BIRTH]: composeBirthDocument,
[EVENT_TYPE.DEATH]: composeDeathDocument,
[EVENT_TYPE.MARRIAGE]: composeMarriageDocument
} satisfies Record<EVENT_TYPE, (record: ValidRecord) => SearchDocument>

export const formatIndexName = (timestamp: string) =>
`${OPENCRVS_INDEX_NAME}-${timestamp}`
export const formatIndexName = () =>
`${OPENCRVS_INDEX_NAME}-${format(new Date(), 'yyyyMMddHHmmss')}`

/** Streams the MongoDB records to ElasticSearch */
export const reindex = async (timestamp: string) => {
export const reindex = async () => {
const t1 = performance.now()
const index = formatIndexName(timestamp)
const index = formatIndexName()
logger.info(`Reindexing to ${index}`)

const stream = await streamAllRecords(true)
Expand Down
16 changes: 1 addition & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7894,23 +7894,14 @@
dependencies:
"@types/react" "*"

"@types/react@*", "@types/react@16 || 17 || 18", "@types/[email protected]", "@types/react@>=16":
"@types/react@*", "@types/react@16 || 17 || 18", "@types/[email protected]", "@types/react@>=16", "@types/react@^16":
version "18.3.1"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.3.1.tgz#fed43985caa834a2084d002e4771e15dfcbdbe8e"
integrity sha512-V0kuGBX3+prX+DQ/7r2qsv1NsdfnCLnTgnRJ1pYnxykBhGMz+qj+box5lq7XsO5mtZsBqpjwwTu/7wszPfMBcw==
dependencies:
"@types/prop-types" "*"
csstype "^3.0.2"

"@types/react@^16":
version "16.14.60"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.14.60.tgz#f7ab62a329b82826f12d02bc8031d4ef4b5e0d81"
integrity sha512-wIFmnczGsTcgwCBeIYOuy2mdXEiKZ5znU/jNOnMZPQyCcIxauMGWlX0TNG4lZ7NxRKj7YUIZRneJQSSdB2jKgg==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "^0.16"
csstype "^3.0.2"

"@types/readdir-glob@*":
version "1.1.3"
resolved "https://registry.npmjs.org/@types/readdir-glob/-/readdir-glob-1.1.3.tgz"
Expand Down Expand Up @@ -7979,11 +7970,6 @@
dependencies:
htmlparser2 "^8.0.0"

"@types/scheduler@^0.16":
version "0.16.8"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.8.tgz#ce5ace04cfeabe7ef87c0091e50752e36707deff"
integrity sha512-WZLiwShhwLRmeV6zH+GkbOFT6Z6VklCItrDioxUnv+u4Ll+8vKeFySoFyK/0ctcRpOmwAicELfmys1sDc/Rw+A==

"@types/semver@^7.3.4":
version "7.5.4"
resolved "https://registry.npmjs.org/@types/semver/-/semver-7.5.4.tgz"
Expand Down

0 comments on commit 0adf8e9

Please sign in to comment.