我想使用计时器触发的Azure函数来生成多个axios调用,以更改多个CosmosDB集合的吞吐量设置。
根据日志,以下代码将开始(即,出现“ [ Info]函数已启动”消息),但随后将不再显示日志,并且该函数将以状态代码 202已接受。
我怀疑问题是由我处理axios承诺引起的,但是经过多次尝试,我还是向Stackoverflow寻求帮助。
任何帮助将不胜感激。
const axios = require("axios");
const azure = require("azure-storage");
const functionURL = "https://<redacted>.azurewebsites.net/api/ChangeRU?code=<redacted>"
// Update Offer for Collection
function updateOffer_for_Collection_RESTCall(
environment,
database,
collection,
newRU
) {
context.log(`\nUpdate throughput for collection (${collection}) in database (${database}) of environment (${environment}) to: ${newRU}`);
// Execute REST call
const url = functionURL + "&env=" + environment + "&database=" + database + "&collection=" + collection + "&ru=" + newRU;
context.log(`url = ${url}`);
return url;
}
module.exports = function (context, myTimer) {
var timeStamp = new Date().toISOString();
context.log('Scale down job started:', timeStamp);
if (myTimer.isPastDue) {
context.log('Scale down job is running late!');
}
var collectionTableService = azure.createTableService("<storageaccount>", "<redacted>");
const query = new azure.TableQuery()
.top(2)
.where('PartitionKey eq ?', 'DBCollections');
context.log('Query created...');
collectionTableService.queryEntities('DBCollections', query, null, function (error, result, response) {
if (!error) {
// result.entries contains entities matching the query
const collections = result.entries;
let axiosArray = [];
for (let collection of collections) {
context.log("Collection: " + JSON.stringify(collection) + "\n");
// Process collection
let url = updateOffer_for_Collection_RESTCall(collection.environment._, collection.database._, collection.collection._, 400);
let newPromise = axios({
method: 'post',
url: url,
data: {}
});
axiosArray.push(newPromise);
};
axios
.all(axiosArray)
.then(
function (results) {
let temp = results.map(r => r.data);
context.log('submitted all axios calls');
})
.catch(error => { });
context.done();
} else {
context.log('Error retrieving records from table DBCollections: ' + error);
context.done();
}
});
};
答案 0 :(得分:0)
过去几天对Javascript承诺有了更好的了解,我准备回答自己的问题:-)
考虑以下重写:
// This Azure Function will change the request units setting of a number of Cosmos DB database collections at CSP <redacted>
// Use https://github.com/axios/axios as a REST client
const axios = require("axios");
const azure = require("azure-storage");
const functionURL =
"https://<redacted>.azurewebsites.net/api/<redacted>?code=<redacted>";
module.exports = function (context, myTimer) {
context.log("Going in...");
function asyncPostURL(url) {
return new Promise((resolve, reject) => {
axios.post(url)
.then(function (response) {
context.log(`POST response: status=${response.status}, data=${response.data}`);
resolve(response.data);
})
.catch(function (error) {
context.log(error);
reject(error);
});
});
}
// Create POST url to update offer for collection
function create_url(record) {
const environment = record.environment._;
const database = record.database._;
const collection = record.collection._;
const newRU = record.scaleupRU._;
context.log(
`\nUpdate throughput for collection (${collection}) in database (${database}) of environment (${environment}) to: ${newRU}`
);
// Create POST url
const url =
functionURL +
"&env=" +
environment +
"&database=" +
database +
"&collection=" +
collection +
"&ru=" +
newRU;
context.log(`url: ${url}`);
return url;
}
function post_urls(arr, final, context) {
return arr.reduce((promise, record) => {
return promise
.then(result => {
let url = create_url(record);
context.log(`url: ${url}.`);
return asyncPostURL(url).then(result => final.push(result));
})
.catch(error => {
context.log(error);
});
}, Promise.resolve());
}
var timeStamp = new Date().toISOString();
context.log("Scale down job started:", timeStamp);
if (myTimer.isPastDue) {
context.log("Scale down job is running late!");
}
var collectionTableService = azure.createTableService(
"pbscsptoolsstorage",
"<redacted>"
);
const query = new azure.TableQuery()
//.top(2)
.where("PartitionKey eq ?", "DBCollections");
context.log("Query created...");
const get_collections = tbl =>
new Promise((resolve, reject) => {
collectionTableService.queryEntities(tbl, query, null, function (
error,
result,
response
) {
if (!error) {
context.log(`# entries: ${result.entries.length}`);
resolve(result.entries);
} else {
reject(error);
}
});
});
var final = [];
get_collections("<redacted>").then(
collections => {
context.log(`# collections: ${collections.length}`);
post_urls(collections, final, context).then(() => context.log(`FINAL RESULT is ${final}`))
.then(() => context.done());
},
error => {
context.log("Promise get_collections failed: ", error)
.then(() => context.done());
}
)
};