微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有长时间运行的函数的 doPost 超时 答案:示例:参考资料:示例:参考资料:

如何解决带有长时间运行的函数的 doPost 超时 答案:示例:参考资料:示例:参考资料:

在 Google App Script 中,doPost(e) 期望在 3 秒内响应,否则会触发超时错误

function doPost(e) {
    //do stuff
    long_time_function (); \\takes longer than 3 seconds

    return ContentService.createtextoutput(); 
}

在另一个问题 (link) 中,通过为另一个稍后运行的函数创建触发器,该问题得到了部分解决

function longRunningFunction(){
  // Some code here to execute later
}

function doPost(e) {

  var Now  = new Date();
  Now.setMinutes(Now.getMinutes()+2);
  ScriptApp.newTrigger('longRunningFunction').timeBased().at(Now).create();

  return ContentService.createtextoutput('My Respnse')
}

这对我帮助很大。但是,我对这个问题还有一个补充,目前我无法弄清楚。

是否可以将 e 中的数据(来自 doPost(e))包含在“LongRunningFunction()”中?

谢谢!

解决方法

答案:

将数据保存到 doPost() 函数中的脚本属性并在 longRunningFunction 中访问它们。

示例:

在doPost中,将你需要的数据保存到属性中:

function doPost(e) {
  const props = PropertiesService.getUserProperties()
  const data = e.parameters.name // or whatever
  props.setProperty("propertyName",data)

  const now  = new Date()
  now.setMinutes(now.getMinutes() + 2)
  ScriptApp.newTrigger('longRunningFunction').timeBased().at(now).create()

  return ContentService.createTextOutput('My Respnse')
}

然后在 longRunningFunction 中访问它:

function longRunningFunction() {
  const props = PropertiesService.getUserProperties()
  const data = props.getProperty("propertyName")

  // do stuff with data
}

希望对你有帮助!

参考资料:

  • [物业服务|应用程序脚本 | Google 开发者](### 答案: 将数据保存到 doPost() 函数中的脚本属性并在 longRunningFunction 中访问它们。

示例:

在doPost中,将你需要的数据保存到属性中:

function doPost(e) {
  const props = PropertiesService.getUserProperties()
  const data = e.parameters.name // or whatever
  props.setProperty("propertyName",data)

  const now  = new Date()
  now.setMinutes(now.getMinutes() + 2)
  ScriptApp.newTrigger('longRunningFunction').timeBased().at(now).create()

  return ContentService.createTextOutput('My Respnse')
}

然后在 longRunningFunction 中访问它:

function longRunningFunction() {
  const props = PropertiesService.getUserProperties()
  const data = props.getProperty("propertyName")

  // do stuff with data
}

希望对你有帮助!

参考资料:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。