Skip to content

Commit

Permalink
fix: improve logging of handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Mar 17, 2024
1 parent 92610c8 commit 11a7251
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
26 changes: 22 additions & 4 deletions packages/api/src/functions/refreshMetadataCollection/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { InvokeCommand } from "@aws-sdk/client-lambda"
import { STAGE } from "src/constants"
import { COLLECTION_METADATA_LOCK_MN } from "@oboku/shared"
import { lock } from "@libs/supabase/lock"
import { Logger } from "@libs/logger"

const logger = Logger.child({ module: "handler" })

const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
event
Expand All @@ -25,12 +28,27 @@ const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
})
})

const lockId = `metadata-collection_${event.body.collectionId}`
logger.info(`invoke for ${event.body.collectionId}`)

try {
const lockId = `metadata-collection_${event.body.collectionId}`

const { alreadyLocked } = await lock(lockId, COLLECTION_METADATA_LOCK_MN)

if (!alreadyLocked) {
const response = await client.send(command)

const { alreadyLocked } = await lock(lockId, COLLECTION_METADATA_LOCK_MN)
logger.info(
`${event.body.collectionId}: command sent with success ${response.$metadata.requestId}`
)
logger.info(response)
} else {
logger.info(`${event.body.collectionId} is already locked, ignoring!`)
}
} catch (error) {
logger.error(error)

if (!alreadyLocked) {
await client.send(command)
throw error
}

return {
Expand Down
26 changes: 22 additions & 4 deletions packages/api/src/functions/syncDataSource/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { STAGE } from "../../constants"
import schema from "./schema"
import { InvokeCommand } from "@aws-sdk/client-lambda"
import { lock } from "@libs/supabase/lock"
import { Logger } from "@libs/logger"

const LOCK_MAX_DURATION_MN = 10

const logger = Logger.child({ module: "handler" })

const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
event
) => {
Expand All @@ -25,12 +28,27 @@ const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
})
})

const lockId = `sync_${event.body.dataSourceId}`
logger.info(`invoke for ${event.body.dataSourceId}`)

try {
const lockId = `sync_${event.body.dataSourceId}`

const { alreadyLocked } = await lock(lockId, LOCK_MAX_DURATION_MN)

if (!alreadyLocked) {
const response = await client.send(command)

const { alreadyLocked } = await lock(lockId, LOCK_MAX_DURATION_MN)
logger.info(
`${event.body.dataSourceId}: command sent with success ${response.$metadata.requestId}`
)
logger.info(response)
} else {
logger.info(`${event.body.dataSourceId} is already locked, ignoring!`)
}
} catch (error) {
logger.error(error)

if (!alreadyLocked) {
await client.send(command)
throw error
}

return {
Expand Down

0 comments on commit 11a7251

Please sign in to comment.