2014-10-01 37 views
0

我正在編寫的後臺作業設計爲遍歷每個用戶並每次執行代碼。當我運行它時,它只對11個用戶執行,然後停止。似乎沒有任何會導致這種情況的解析背景作業限制,所以我不確定它爲什麼會發生。解析後臺作業在11個用戶後停止

迭代通過用戶的後臺作業的部分如下,如果需要任何其他部分的工作,我會很樂意添加它。我在日誌中看到這條消息:Reached max log messages per request, later messages for this request are truncated.,所以我甚至無法看到日誌中出現了什麼問題。

編輯:在檢查上解析的工作狀態,我看到這個錯誤:enter image description here

代碼:

Parse.Cloud.job("MatchCenterBackground", function(request, status) { 
    // ... other code to setup usersQuery ... 
    Parse.Cloud.useMasterKey(); 
    var usersQuery = new Parse.Query(Parse.User); 

    return usersQuery.each(function (user) { 
    return processUser(user).then(function(eBayResults){ 
     return matchCenterComparison(user, eBayResults); 
    }); 
    }).then(function() { 
    // Set the job's success status 
    status.success("MatchCenterBackground completed successfully."); 
    }, function(error) { 
    // Set the job's error status 
    status.error("Got an error " + error.code + " : " + error.message); 
    }); 
}); 

// process user, return promise 
function processUser(user) { 
    // ... code to setup per-user query ... 
    var matchCenterItem = Parse.Object.extend("matchCenterItem"); 
    var query = new Parse.Query(matchCenterItem); 
    query.equalTo('parent', user); 

    // easy way to share multiple arrays 
    var shared = { 
     promises: [], 
     searchTerms: [], 
    }; 

    return query.find().then(function(results) { 
     // process results, populate shared data (promises and searchTerms) 
    console.log('matchCenterItem query results:' + results); 
    if (results.length > 0) { 
     console.log('we have entered the matchcenteritem query'); 

     for (i = 0; i < results.length; i++) { 

      console.log('we have also entered the loop inside the matchCenterItem query'); 
      // later in your loop where you populate promises: 
      var searchTerm = results[i].get('searchTerm'); 
      // add it to the array just like you add the promises: 
      shared.searchTerms.push(searchTerm); 

      url = 'http://svcs.ebay.com/services/search/FindingService/v1'; 
      //push function containing criteria for every matchCenterItem into promises array 
      shared.promises.push((function() { 

      if (results[i].get('itemLocation') == 'US') 
      { 
       console.log('americuh!'); 
       var httpRequestPromise = Parse.Cloud.httpRequest({ 
       url: url, 
       params: { 
        'OPERATION-NAME': 'findItemsByKeywords', 
        'SERVICE-VERSION': '1.12.0', 
        'SECURITY-APPNAME': '*APP ID GOES HERE*', 
        'GLOBAL-ID': 'EBAY-US', 
        'RESPONSE-DATA-FORMAT': 'JSON', 
        'REST-PAYLOAD&sortOrder': 'BestMatch', 
        'paginationInput.entriesPerPage': '3', 
        'outputSelector=AspectHistogram&itemFilter(0).name=Condition&itemFilter(0).value(0)': 'New', 
        'itemFilter(0).value(1)': results[i].get('itemCondition'), 
        'itemFilter(1).name=MaxPrice&itemFilter(1).value': results[i].get('maxPrice'), 
        'itemFilter(1).paramName=Currency&itemFilter(1).paramValue': 'USD', 
        'itemFilter(2).name=MinPrice&itemFilter(2).value': results[i].get('minPrice'), 
        'itemFilter(2).paramName=Currency&itemFilter(2).paramValue': 'USD', 
        'itemFilter(3).name=LocatedIn&itemFilter(3).value': 'US', 
        'itemFilter(4).name=ListingType&itemFilter(4).value': 'FixedPrice', 
        'keywords': results[i].get('searchTerm'), 
       } 
       }); 
      } 

      else if (results[i].get('itemLocation') == 'WorldWide') 
      { 
       console.log('Mr worlwide!'); 
       var httpRequestPromise = Parse.Cloud.httpRequest({ 
       url: url, 
       params: { 
        'OPERATION-NAME': 'findItemsByKeywords', 
        'SERVICE-VERSION': '1.12.0', 
        'SECURITY-APPNAME': '*APP ID GOES HERE*', 
        'GLOBAL-ID': 'EBAY-US', 
        'RESPONSE-DATA-FORMAT': 'JSON', 
        'REST-PAYLOAD&sortOrder': 'BestMatch', 
        'paginationInput.entriesPerPage': '3', 
        'outputSelector=AspectHistogram&itemFilter(0).name=Condition&itemFilter(0).value(0)': 'New', 
        'itemFilter(0).value(1)': results[i].get('itemCondition'), 
        'itemFilter(1).name=MaxPrice&itemFilter(1).value': results[i].get('maxPrice'), 
        'itemFilter(1).paramName=Currency&itemFilter(1).paramValue': 'USD', 
        'itemFilter(2).name=MinPrice&itemFilter(2).value': results[i].get('minPrice'), 
        'itemFilter(2).paramName=Currency&itemFilter(2).paramValue': 'USD', 
        'itemFilter(3).name=ListingType&itemFilter(3).value': 'FixedPrice', 
        'keywords': results[i].get('searchTerm'), 
       } 
       }); 
      } 

      return httpRequestPromise; 
      })()); 
     } 
     } 

     //buildEbayRequestPromises(results, shared); 
    }).then(function() { 
     // process promises, return query promise 
     return Parse.Promise.when(shared.promises).then(function() { 

      // process the results of the promises, returning a query promise 
      console.log('were in the when.then of promise'); 

      var eBayResults = []; 
      for (var i = 0; i < arguments.length; i++) { 
      var httpResponse = arguments[i]; 
      // since they're in the same order, this is OK: 
      var searchTerm = shared.searchTerms[i]; 
      // pass it as a param: 
      var top3 = buildEbayRequestPromises(httpResponse.text, searchTerm); 

      eBayResults.push(top3); 

      } 

      return eBayResults; 
     }); 
    }); 
} 

// process matchCenterItem results to build eBay promises 
function buildEbayRequestPromises(eBayResponseText, shared) { 
    // ... code that pushes items into shared.promises and shared.searchTerms ... 

    var ebayResponse = JSON.parse(eBayResponseText); 
    var matchCenterItems = []; 

    //Parses through ebay's response, pushes each individual item and its properties into an array 
    ebayResponse.findItemsByKeywordsResponse.forEach(function(itemByKeywordsResponse) { 
    itemByKeywordsResponse.searchResult.forEach(function(result) { 
     if (result.item){ 
     result.item.forEach(function(item) { 
      matchCenterItems.push(item); 
     }); 
     } 
    }); 
    }); 

    var top3Titles = []; 
    var top3Prices = []; 
    var top3ImgURLS = []; 
    var top3ItemURLS = []; 

    //where the title, price, and img url are set 
    matchCenterItems.forEach(function(item) { 
    var title = item.title[0]; 
    var price = item.sellingStatus[0].convertedCurrentPrice[0].__value__; 
    var imgURL = item.galleryURL[0]; 
    var itemURL = item.viewItemURL[0]; 

    top3Titles.push(title); 
    top3Prices.push(price); 
    top3ImgURLS.push(imgURL); 
    top3ItemURLS.push(itemURL); 
    }); 

    console.log('about to define top3 value'); 
    //Top 3 item info for every MatchCenterItem 
    var top3 = 
    { 
    "Top 3": 
     [ 
     { 
     "Title": top3Titles[0], 
     "Price": top3Prices[0], 
     "Image URL": top3ImgURLS[0], 
     "Item URL": top3ItemURLS[0] 
     }, 

     { 
     "Title": top3Titles[1], 
     "Price": top3Prices[1], 
     "Image URL": top3ImgURLS[1], 
     "Item URL": top3ItemURLS[1] 
     }, 

     { 
     "Title": top3Titles[2], 
     "Price": top3Prices[2], 
     "Image URL": top3ImgURLS[2], 
     "Item URL": top3ItemURLS[2] 
     } 
     ] 
    }; 

    return top3; 
} 

// compare eBayResults to the users MCItems Array in their MComparisonArray object 
function matchCenterComparison(parentUser, eBayResults) { 

    console.log('izayak habibi, eBayResults are the following:' + eBayResults); 

    var matchCenterComparisonPromise = new Parse.Promise(); 

    // if the user has MatchCenter items, do this: 

    console.log('ando ishal'); 
    if (eBayResults.length > 0) { 
     console.log('yes the ebay results be longer than 0'); 

     var mComparisonArray = Parse.Object.extend("MComparisonArray"); 
     var mComparisonQuery = new Parse.Query(mComparisonArray); 

     // Query that compares MCItems array contents to eBayResults 
     mComparisonQuery.equalTo('parent', parentUser); 
     mComparisonQuery.contains('Name', 'MatchCenter'); 
     mComparisonQuery.containedIn('MCItems', eBayResults); 

     console.log('setup query criteria, about to run it'); 
     mComparisonQuery.find().then(function(results) { 
     //No new items      
     if (results.length > 0) { 
      console.log("No new items, you're good to go!"); 

      //Add user to the "DON'T send push notification" channel 
      //////// 
      var installationQuery = new Parse.Query(Parse.Installation); 
      installationQuery.equalTo('userId', parentUser); 

      installationQuery.first().then(function(result) { 
      result.set('channels', ["noPush"]); 
      result.save(); 
      }); 
      /////// 
      console.log('done updating channel'); 
     } 

     //New items found 
     else if (results.length === 0) { 
      console.log('no matching mComparisonArray, lets push some new shit'); 

      var mComparisonEditQuery = new Parse.Query(mComparisonArray); 
      mComparisonEditQuery.contains('Name', 'MatchCenter'); 
      mComparisonEditQuery.equalTo('parent', parentUser); 

      console.log('setup query criteria again, about to run it'); 

      // Update MComparisonArray with new eBayResults 
      mComparisonEditQuery.first().then(function(results) { 
      results.set('MCItems', eBayResults); 
      results.save(); 

      console.log('totally just updated the mComparisonArray, NBD'); 
      }).then(function() { 
       //////// 
       //Add user to the "send push notification" channel 
       var installationQuery = new Parse.Query(Parse.Installation); 
       installationQuery.equalTo('userId', parentUser); 

       installationQuery.first().then(function(result) { 
       result.set('channels', ["yesPush"]); 
       result.save(); 
       }); 
       //////// 
       console.log('done updating channel'); 

      }); 
     } 
     }); 
    matchCenterComparisonPromise.resolve(console.log('MatchCenterComparison Suceeded sen!')); 
    } else { 
    matchCenterComparisonPromise.reject({ message: 'No work done, expression failed' }); 
    } 
    //return matchCenterComparisonPromise; 

} 
+0

每個後臺作業都被限制爲15分鐘,您確定沒有達到該限制嗎? – sarvesh 2014-10-01 23:45:57

+0

@sarvesh絕對不會,這發生在10秒鐘的功能。 – Ghobs 2014-10-02 02:07:08

+0

沒有看到代碼有什麼問題,你可以發佈剩下的代碼嗎? – sarvesh 2014-10-02 15:48:00

回答

0

發現錯誤,註釋掉imgurl值固定的問題,因爲該變量贏得總是存在,取決於用戶是否有物品。

if (matchCenterItems.length > 0){ 

    matchCenterItems.forEach(function(item) { 
     var title = item.title[0]; 
     var price = item.sellingStatus[0].convertedCurrentPrice[0].__value__; 
     //var imgURL = item.galleryURL[0]; 
     var itemURL = item.viewItemURL[0]; 

     top3Titles.push(title); 
     top3Prices.push(price); 
     //top3ImgURLS.push(imgURL); 
     top3ItemURLS.push(itemURL); 
    }); 

    } 
    console.log('about to define top3 value'); 
    //Top 3 item info for every MatchCenterItem 
    var top3 = 
    { 
    "Top 3": 
     [ 
     { 
     "Title": top3Titles[0], 
     "Price": top3Prices[0], 
     //"Image URL": top3ImgURLS[0], 
     "Item URL": top3ItemURLS[0] 
     }, 

     { 
     "Title": top3Titles[1], 
     "Price": top3Prices[1], 
     //"Image URL": top3ImgURLS[1], 
     "Item URL": top3ItemURLS[1] 
     }, 

     { 
     "Title": top3Titles[2], 
     "Price": top3Prices[2], 
     //"Image URL": top3ImgURLS[2], 
     "Item URL": top3ItemURLS[2] 
     } 
     ] 
    };