【问题标题】:How to use the nextToken in the Step function's GetHistoryExecution?如何在 Step 函数的 GetHistoryExecution 中使用 nextToken?
【发布时间】:2019-03-12 05:56:02
【问题描述】:

我正在尝试使用 lambda 函数获取所有执行历史记录并将其存储到 DynamoDB。该函数返回大约 20 次执行和一个名为 NextToken 的字符串值,该值将在下一次调用中用于获取其余的执行。

这是我的代码。

const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient({
   region: 'myregion' 
});

exports.handler = (event, context, callback) => {
    const table = 'myDynamoDB';
    const executionARN = "arn:aws:region:accountid:execution:myStateMachine:test";
    var stepfunctions = new AWS.StepFunctions();
    var params = {
        executionArn: executionARN,
        maxResults: 20,
        nextToken: null,
        reverseOrder: false
    };

    stepfunctions.getExecutionHistory(params, function(err, dataExecution) {
        if (err){
          console.log(err, err.stack);
        } 
        else {
            const params2 = {
                TableName: table,
                Item: {
                    id: executionARN,
                    execution_history: dataExecution
                }
            };
                dynamoDb.put(params2).promise();
        }
    });
};

【问题讨论】:

    标签: amazon-web-services lambda amazon-dynamodb aws-step-functions


    【解决方案1】:

    nextToken 需要用于参数传递到下一次调用 getExecutionHistory。您可以递归调用此函数,直到用完所有令牌。通过 Cloud watch 获取日志时遇到了类似的情况。

    递归获取历史的示例,

    将 getExecutionHistory 包装到 Promise 中并添加到不同的 JS 文件(比如说 writer.js),然后您的主 index.js 文件可以像这样调用该函数,

    // writer.js which writes record to Dynamodb
    // returns promise
    // when history is fetched , dynamodb will be inserted and it will resolve dataexecution which has nextToken
    
    module.exports.get = function(fwdtoken) {    
    
        if (fwdtoken) parms.nextToken= fwdtoken;
    
        return new Promise ( (resolve, reject)=>{
        stepfunctions.getExecutionHistory(params, function(err, dataExecution) {
            if (err){
               reject(err.stack)
            } 
            else {
                const params2 = {
                    TableName: table,
                    Item: {
                        id: executionARN,
                        execution_history: dataExecution
                    }
                };
                dynamoDb.put(params2).promise();
            resolve(dataExecution)
            }
          });   
        })    
    };  
    
    //This goes in main logic
    // Invokes getAllLogs recursilvely
    
    var writer = require('./writer');
    var fwdtoken;
    
    function getAllLogs(fwdtoken, fetchCount) {    
        fetchCount = fetchCount || 0;
        if (fetchCount > 40) {
            throw new Error("Fetched too many times.");
        }
        return new Promise( (resolve) => {
                writer.get(fwdtoken).then( function consolidate( dataExecution ) {                  
                resolve( dataExecution );            
            });    
        })
        .then(function ( dataExecution ) {
            if (dataExecution.nextForwardToken) {
                fwdtoken = dataExecution.nextForwardToken;
                getAllLogs(fwdtoken, fetchCount+ 1)
            }
            else
            return fwdtoken        
        });
    }
    getAllLogs(fwdtoken, 0);
    

    【讨论】:

    • 你能分享一些关于如何递归调用这个函数的代码吗?我对 aws 真的很陌生。
    • 更新了我的答案。请注意,我没有运行这个。请参考它的结构。
    • 非常感谢。但是递归的“getAllLogs(fwdtoken, fetchCount+1)”会抛出错误。
    • 我能够获得下一页,但第三页等没有执行。 Lambda 错误,它什么也没说。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2019-01-18
    相关资源
    最近更新 更多