我正在编写的后台作业旨在遍历每个用户,并且每次都执行代码。当我运行它时,它只对11个用户执行,然后停止。似乎没有任何类型的Parse后台作业限制会导致这种情况,因此我不确定它为什么会发生。
下面是通过用户进行迭代的后台作业部分,如果需要该作业的任何其他部分,我很乐意添加它。我在日志中收到此消息:Reached max log messages per request, later messages for this request are truncated.
,所以我甚至无法在日志中看到出现了什么问题。
编辑:在解析时检查作业状态时,我看到此错误:
代码:
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 :(得分: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]
}
]
};