2012-06-14 145 views
0

我以編程方式在Google Apps腳本中設置觸發器,以便在作業正在其他地方處理時延遲提取數據一段時間。Google Apps腳本中的觸發器已過期 - 垃圾?

我已經注意到,在腳本編輯器的「查看當前腳本的觸發器」中,舊的(過期的)觸發器持續存在 - 我懷疑它們也計算了最大觸發器數量限制。

Google Apps腳本會刪除這些過期的觸發器嗎?還是必須通過觸發器列表並刪除過期的觸發器?如果是的話,有人想分享一個代碼示例嗎? (我建議,這也應該包含在文檔中)。

我使用.AT

var d = new Date(); 
    d.setMinutes(d.getMinutes() + 5); 

    try { 
    ScriptApp.newTrigger("retrieveOrder") 
     .timeBased() 
     .at(d) 
     .create(); 
    } catch(e) { 
    sendErrorMail(e + " \n \n" + "Date: " + d); 
    return false; 
    } 

由於創建觸發器,萬事如意

馬里奧

回答

0

我把Trigger的唯一id和它被設置爲過期到ScriptDb的時間標記夾在一起。然後,我用這個cludge清除:

/** 
* @param {ScriptDbInstance} db 
*/ 
function clearExpiredTimeTriggers(db){ 

    var now = new Date(); 
    var nts = now.getTime(); 
    var res = db.query({trigId:db.anyValue(), trigExpiration: db.lessThan(nts)}); 
    var expiredTriggers = []; 
    while (res.hasNext()){ 
    var item = res.next(); 
    expiredTriggers.push(item.trigId); 
    db.remove(item); 
    } 

    var triggers = ScriptApp.getProjectTriggers(); 
    for (var i = 0; i < triggers.length; i++) { 
    if (expiredTriggers.indexOf(triggers[i].getUniqueId()) > -1){ 
     ScriptApp.deleteTrigger(triggers[i]); 
    } 
    } 
} 
+0

由於您的觸發器的實際執行時間爲+/- 15分鐘,因此此方法未充分證明(https://developers.google.com/apps-script/reference/script/clock-trigger-builder#at(Date )),所以當你添加上面的時間戳時,確保添加一個合適的15分鐘的餘地。 – LeonidR

2

你不得不刪除觸發器。以docs爲例

0

如果你知道它被稱爲從那麼你可以通過處理函數刪除... 功能「updateGmailPhotoFromDriveBatcher」被稱爲然後調用本身

// this is called by a daily trigger. I don't want it deleted 
function runPhotoUpdates() { 
    // NEEDS to be a separate fnc so it doesn't get deleted 
    updateGmailPhotoFromDriveBatcher() 
} 

// this function does a job, then reschedules itself until a blank is returned 
function updateGmailPhotoFromDriveBatcher() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); //fetches the spreadsheet that the script is running in 
    var sheet = ss.getSheetByName("Groups"); 


    var sgfrom = sheet.getRange(10, 21).getValue(); 
    var npt = sheet.getRange(11, 21).getValue(); 

    var oneJob = oneBatch(npt,sgfrom) 
    var currTime = (new Date()).getTime(); 
    // WRITE THE new values; oneJob[0] is blank when completed; npt is a next page token of something that times out if you run the whole user base 
    sheet.getRange(10, 21).setValue(oneJob[1]); 
    sheet.getRange(11, 21).setValue(oneJob[0]); 
    sheet.getRange(12, 21).setValue(oneJob[2]); 
    sheet.getRange(13, 21).setValue(currTime) 


    SpreadsheetApp.flush(); 

    if (oneJob[0] != "") { 
    // SCHEDULE NEW JOB 

    ScriptApp.newTrigger("updateGmailPhotoFromDriveBatcher") 
      .timeBased() 
      .at(new Date(currTime+(20*1000))) // 20 seconds from NOW 
      .create(); 
    } else { // FINISHED 
     // KILL TRIGGERS 
     deleteTriggers() 
    } 

} 

function deleteTriggers() { // cleans up the triggers made above 
    var triggers = ScriptApp.getProjectTriggers(); 
    for (var i = 0; i < triggers.length; i++) { 
     var thf = triggers[i].getHandlerFunction() //String Returns the function that will be called when the trigger fires. 
     // if it's the one I kept making above 
     if (thf == "updateGmailPhotoFromDriveBatcher") { 
      ScriptApp.deleteTrigger(triggers[i]); 
     } 

    } 

} 
相關問題