简体   繁体   English

优化 NetSuite restlet 避免超时错误

[英]Optimize NetSuite restlet to avoid timeout error

I'm writing a restlet that will return all Bill, Credit Card, and Journal transactions within a NetSuite account (see code below).我正在编写一个 restlet,它将返回 NetSuite 帐户中的所有账单、信用卡和日记账交易(请参见下面的代码)。 My issue is that given the volume of data (100k+ transaction records), I'm getting a timeout error.我的问题是,鉴于数据量(超过 100k 的交易记录),我遇到了超时错误。 Is there any way for me to optimize my code to avoid this timeout error?我有什么办法可以优化我的代码来避免这个超时错误吗? Is there a way for me to pass the restlet parameters around the PageRanges and just make multiple calls?有没有办法让我在 PageRanges 周围传递 restlet 参数并进行多次调用?

/**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */
 define(['N/error', 'N/search'],
 function(error, search) {
     function doValidation(args, argNames, methodName) {
         for (var i = 0; i < args.length; i++)
             if (!args[i] && args[i] !== 0)
                 throw error.create({
                     name: 'MISSING_REQ_ARG',
                     message: 'Missing a required argument: [' + argNames[i] + '] for method: ' + methodName
                 });
     }
     function _get(context) {
         doValidation('GET');
        
         var mySearch = search.create({
            type: search.Type.TRANSACTION,
            columns: ['account', 'recordtype','trandate', 'tranid', 'memo', 'amount', 'department', 'entity' ],
            filters: [['recordtype', 'is', 'vendorbill'], 'or', ['recordtype', 'is', 'creditcardcharge'],'or', ['recordtype', 'is', 'journalentry']]
        });
        results = []
        var myPagedData = mySearch.runPaged({
            pageSize: 1000
        })

        myPagedData.pageRanges.forEach(function(pageRange){
            var myPage = myPagedData.fetch({index: pageRange.index})
            results.push(myPage.data)
        })
        return results
         }  
         
     return {
         get: _get,
     };
 }); 

You can refresh the restLet for time exceed error as it has 5 minute(300 seconds) time limit only.您可以刷新 restLet 的时间超过错误,因为它只有 5 分钟(300 秒)的时间限制。 you can use N/runtime moduleto get its governance limit and N/cache module to store the already done data.你可以使用N/runtime模块来获取它的治理限制和N/cache模块来存储已经完成的数据。 check the remaining governance and curren t time in loop.在循环中检查剩余治理和当前时间。 and break the loop and call again restLet after storing already done data using cache module.在使用缓存模块存储已完成的数据后,打破循环并再次调用 restLet。 Pass remaining data as a parameter while calling.调用时将剩余数据作为参数传递。

 var script_startdate = new Date(); // this will be in starting on script 
function
script_Start_time = script_startdate.getTime(); 
for(var i=0;i<i+2;i++){   // here i used infinity loop, you can use yours
var script_workdate = new Date();
var script_workTime = script_workdate.getTime();
remainingTime= script_workTime - script_Start_time;
var remainingContentIndex=content.indexOf(content[i]);
var remainingUsage = runtime.getCurrentScript().getRemainingUsage();
if ((remainingTime> 240000) ||(remainingUsage<80))break;
}

if ((substraction > 240000) ||(remainingUsage<80))
   {
      var myCacheRecId = cache.getCache({
           name: 'temporaryCacheRecId',
           scope: cache.Scope.PUBLIC
           });
        myCacheRecId.put({
              key: 'myvar',
              value: "already completed data""
              });
     var slice_index=remainingContentIndex+1;
     var remainingContent=content.slice(slice_index);
     var content=content.toString(remainingContent);
              redirect.redirect({
                scriptId: 'customscript_scriptid,
                deploymentId: 'customdeploy_deployid',
                  parameters: {
                       content: content
                        }
                      });
                  log.debug("called restlet", "called restlet");
    
           };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM