Skip to content

Commit

Permalink
Add retry process to getSecretFromCache
Browse files Browse the repository at this point in the history
  • Loading branch information
MasaGon committed Oct 7, 2024
1 parent 5ebbffa commit 648ab1c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
12 changes: 9 additions & 3 deletions cdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
"orionEndpoint": "https://orion.dev.hamamatsu.makeour.city",
"fiwareService": "pifaa_test",
"fiwareServicePath": "",
"pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud"
"pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud",
"peopleFlowSensorId": "INFO.LOUNGE_00114116",
"environmentDataSensorId": "017fbf7850b5da0adc59c80b00000000"
}
},
"stg": {
Expand All @@ -101,7 +103,9 @@
"orionEndpoint": "https://orion.stg.hamamatsu.makeour.city",
"fiwareService": "",
"fiwareServicePath": "",
"pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud"
"pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud",
"peopleFlowSensorId": "INFO.LOUNGE_00114116",
"environmentDataSensorId": "017fbf7850b5da0adc59c80b00000000"
}
},
"prd": {
Expand All @@ -123,7 +127,9 @@
"orionEndpoint": "https://orion.hamamatsu.makeour.city",
"fiwareService": "hamamatsu_pifaa",
"fiwareServicePath": "",
"pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud"
"pifaaEndpoint": "https://ap-northeast-1.pifaa.cloud",
"peopleFlowSensorId": "HAMAMATSU-CITY_00324084",
"environmentDataSensorId": "017ed23ee24032205aa7cb7700000000"
}
}
}
Expand Down
24 changes: 20 additions & 4 deletions lambda/src/helper/secretsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@ export const getSecretFromCache = async (secretId: string): Promise<any> => {
'X-Aws-Parameters-Secrets-Token': process.env.AWS_SESSION_TOKEN,
}
}
const response = await axios.get(requestEndpoint, requestOptions)
if (response.data.SecretString === undefined) {
return undefined

// not ready to serve traffic, please wait エラーに対応してリトライ処理を追加
const maxRetries = 3
const retryDelay = 1000

let attempt = 0
while (attempt < maxRetries) {
try {
const response = await axios.get(requestEndpoint, requestOptions)
if (response.data.SecretString === undefined) {
return undefined
}
return JSON.parse(response.data.SecretString)
} catch (error) {
console.log(`Attempt ${attempt} failed: ${error}`)
await new Promise(resolve => setTimeout(resolve, retryDelay))
attempt++
}
}
return JSON.parse(response.data.SecretString)
console.log('Max retries reached. Throwing error.')
throw new Error('Failed to retrieve secret after maximum retries.')
}

/**
Expand Down
28 changes: 10 additions & 18 deletions lambda/src/server.pifaa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault("Asia/Tokyo");

// 人流センサーID
const peopleFlowSensorId = 'INFO.LOUNGE_00114116'

/**
* Username / Password でPifaaにログインし、Cookie一式をSecrets Managerに格納するLambdaハンドラ (30分毎に実行)
*/
Expand Down Expand Up @@ -47,14 +44,13 @@ export const setPifaaCookie: Handler = async () => {
export const importPeopleFlowHandler: Handler = async () => {
console.log('定期的なジョブを実行中: ' + new Date())

try {
const endpoint = process.env.PIFAA_ENDPOINT ?? ''
const endpoint = process.env.PIFAA_ENDPOINT ?? ''
const peopleFlowSensorId = process.env.SENSOR_ID ?? ''

console.log(process.env.SECRET_PIFAA_COOKIES)

const cookies: {[key: string]: string} = await getSecretFromCache(process.env.SECRET_PIFAA_COOKIES ?? '')
const cookieString = Object.keys(cookies).reduce((str, key) => str + `${key}=${cookies[key]}; `, '')
const cookies: {[key: string]: string} = await getSecretFromCache(process.env.SECRET_PIFAA_COOKIES ?? '')
const cookieString = Object.keys(cookies).reduce((str, key) => str + `${key}=${cookies[key]}; `, '')

try {
const currentTimestamp = Math.floor(Date.now() / 1e3)
const roundTimestamp10Min = Math.floor(currentTimestamp / 600) * 600 // 10分単位切り下げ
const timestamp_start = roundTimestamp10Min - 600 * 2 // 20分前
Expand Down Expand Up @@ -110,24 +106,20 @@ export const importPeopleFlowHandler: Handler = async () => {
}
}

// 環境センサーID
const environmentDataSensorId = '017fbf7850b5da0adc59c80b00000000'

/**
* 10分おきに環境データを取得し、Orionに格納するLambdaハンドラ
* 気温・湿度・二酸化炭素濃度
*/
export const importEnvironmentDataHandler: Handler = async () => {
console.log('定期的な環境データ取得ジョブを実行:', new Date())

try {
const endpoint = process.env.PIFAA_ENDPOINT ?? ''
const environmentDataSensorId = process.env.SENSOR_ID ?? ''

const endpoint = process.env.PIFAA_ENDPOINT ?? ''
console.log(process.env.SECRET_PIFAA_COOKIES)

const cookies: {[key: string]: string} = await getSecretFromCache(process.env.SECRET_PIFAA_COOKIES ?? '')
const cookieString = Object.keys(cookies).reduce((str, key) => str + `${key}=${cookies[key]}; `, '')
const cookies: {[key: string]: string} = await getSecretFromCache(process.env.SECRET_PIFAA_COOKIES ?? '')
const cookieString = Object.keys(cookies).reduce((str, key) => str + `${key}=${cookies[key]}; `, '')

try {
const currentTimestamp = Math.floor(Date.now() / 1e3)
const roundTimestamp10Min = Math.floor(currentTimestamp / 600) * 600 // 10分単位切り下げ
const timestamp_start = roundTimestamp10Min - 600 * 1 // 10分前
Expand Down
6 changes: 5 additions & 1 deletion lib/hamamatsu-pifaa-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LambdaFunction } from 'aws-cdk-lib/aws-events-targets'
import {
Runtime,
ParamsAndSecretsLayerVersion,
ParamsAndSecretsVersions
ParamsAndSecretsVersions,
} from 'aws-cdk-lib/aws-lambda'
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'
import { Secret } from 'aws-cdk-lib/aws-secretsmanager'
Expand All @@ -20,6 +20,8 @@ export interface Context {
fiwareService: string
fiwareServicePath: string
pifaaEndpoint: string
peopleFlowSensorId: string
environmentDataSensorId: string
}

interface HamamatsuPifaaProps extends cdk.StackProps {
Expand Down Expand Up @@ -63,6 +65,7 @@ export class HamamatsuPifaaStack extends cdk.Stack {
FIWARE_SERVICE: props.context.fiwareService,
FIWARE_SERVICE_PATH: props.context.fiwareServicePath,
PIFAA_ENDPOINT: props.context.pifaaEndpoint,
SENSOR_ID: props.context.peopleFlowSensorId,
},
paramsAndSecrets, // Use AWS Parameters and Secrets Lambda Extension
})
Expand All @@ -80,6 +83,7 @@ export class HamamatsuPifaaStack extends cdk.Stack {
FIWARE_SERVICE: props.context.fiwareService,
FIWARE_SERVICE_PATH: props.context.fiwareServicePath,
PIFAA_ENDPOINT: props.context.pifaaEndpoint,
SENSOR_ID: props.context.environmentDataSensorId,
},
paramsAndSecrets,
})
Expand Down

0 comments on commit 648ab1c

Please sign in to comment.