@1mill/journal 中文文档教程

发布于 3年前 浏览 26 项目主页 更新于 3年前

@1mill/journal

Context

https://awslabs.github.io/aws-lambda -powertools-python/latest/utilities/idempotency/#idempotency-request-flow

Install

npm install @1mill/journal
// * index.js
const { Journal } = require('@1mill/journal')

const journal = new Journal({
  name: 'my-journal-db' || process.env.MILL_JOURNAL_NAME,
  table: 'my-collection' || process.env.MILL_JOURNAL_TABLE,
  uri: 'mongodb://...:27017' || process.env.MILL_JOURNAL_URI,
})

export.handler = async (cloudevent, ctx) => {
  // * https://www.jeremydaly.com/reuse-database-connections-aws-lambda/
  ctx.callbackWaitsForEmptyEventLoop = false

  try {
    const { skip } = await journal.entry({ cloudevent })
    if (skip) return // * Already being performed by another lambda

    // * Perform business logic
    // ...
  } catch (err) {
    console.error(err)
    // * Erase entry on business logic failure (e.g. fetch to
    // * third-party API failed)
    await journal.erase({ cloudevent })
    throw new Error(err) // * Requeue event to run again
  }
}

@1mill/journal

Context

https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/idempotency/#idempotency-request-flow

Install

npm install @1mill/journal
// * index.js
const { Journal } = require('@1mill/journal')

const journal = new Journal({
  name: 'my-journal-db' || process.env.MILL_JOURNAL_NAME,
  table: 'my-collection' || process.env.MILL_JOURNAL_TABLE,
  uri: 'mongodb://...:27017' || process.env.MILL_JOURNAL_URI,
})

export.handler = async (cloudevent, ctx) => {
  // * https://www.jeremydaly.com/reuse-database-connections-aws-lambda/
  ctx.callbackWaitsForEmptyEventLoop = false

  try {
    const { skip } = await journal.entry({ cloudevent })
    if (skip) return // * Already being performed by another lambda

    // * Perform business logic
    // ...
  } catch (err) {
    console.error(err)
    // * Erase entry on business logic failure (e.g. fetch to
    // * third-party API failed)
    await journal.erase({ cloudevent })
    throw new Error(err) // * Requeue event to run again
  }
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文