Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lambda/fetchDeviceShadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { proto } from '@hello.nrfcloud.com/proto/hello'
import { getShadowUpdateTime } from '@hello.nrfcloud.com/proto/nrfCloud'
import middy from '@middy/core'
import { fromEnv } from '@nordicsemiconductor/from-env'
import { chunk } from 'lodash-es'
import { chunk, once } from 'lodash-es'
import pLimit from 'p-limit'
import { deviceShadowFetcher } from '../nrfcloud/getDeviceShadowFromnRFCloud.js'
import { defaultApiEndpoint } from '../nrfcloud/settings.js'
Expand Down Expand Up @@ -56,7 +56,7 @@ const connectionsRepo = connectionsRepository(
db,
websocketDeviceConnectionsTableName,
)
const deviceShadowPromise = (async () => {
const deviceShadowPromise = once(async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This effectively does the same thing as before. It ensures that deviceShadowPromise only exists once. It will stay in a rejected stated if parameters cannot be fetched.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. Now I see. It does fix the problem, because the lambda is invoked only in the handler.

const [apiKey, apiEndpoint] = await getNRFCloudSSMParameters(stackName, [
'apiKey',
'apiEndpoint',
Expand All @@ -68,17 +68,17 @@ const deviceShadowPromise = (async () => {
apiEndpoint !== undefined ? new URL(apiEndpoint) : defaultApiEndpoint,
apiKey,
})
})()
})

const h = async (): Promise<void> => {
const deviceShadow = await deviceShadowPromise
const lockAcquired = await lock.acquiredLock(lockName, lockTTLSeconds)
if (lockAcquired === false) {
log.info(`Other process is still running, then ignore`)
return
}

try {
const lockAcquired = await lock.acquiredLock(lockName, lockTTLSeconds)
if (lockAcquired === false) {
log.info(`Other process is still running, then ignore`)
return
}

const deviceShadow = await deviceShadowPromise()
const connections = await connectionsRepo.getAll()
log.info(`Found ${connections.length} active connections`)
metrics.addMetric('connections', MetricUnits.Count, connections.length)
Expand Down