我遇到了一个奇怪的行为,我在 Firebase Cloud Functions 中有几个 http 函数。它们工作正常,但有些日子它们会开始返回状态 500 一段时间,然后恢复正常工作几分钟,然后再次开始返回状态 500,这种行为持续一整天。
最奇怪的是,我没有在我的堆栈驱动程序上收到任何错误消息,事实上,没有关于这些调用的注册表,就好像这些调用以某种方式没有到达谷歌的服务,或者它只是被拒绝并且没有关于它的注册表。
我将发布我的应用程序中最常用的功能之一的实现:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp()
exports.changeOrderStatus_1 = functions.https.onRequest((request, response) =>
{
//Check Headers
const clientID = request.get('ClientID');
if(clientID === null || clientID === undefined || clientID === "")
{
console.error(new Error('clientID not provided.'));
return response.status(500).send('clientID not provided.');
}
const unitID = request.get('UnitID');
if(unitID === null || unitID === undefined || unitID === "")
{
console.error(new Error('unitID not provided.'));
return response.status(500).send('unitID not provided.');
}
//Check body
const orderID = request.body.OrderID;
if(orderID === null || orderID === undefined || orderID === "")
{
console.error(new Error('orderID not provided.'));
return response.status(500).send('orderID not provided.');
}
const orderStatus = request.body.OrderStatus;
if(orderStatus === null || orderStatus === undefined || orderStatus === "")
{
console.error(new Error('orderStatus not provided.'));
return response.status(500).send('orderStatus not provided.');
}
const orderStatusInt = Number.parseInt(String(orderStatus));
const notificationTokenString = String(request.body.NotificationToken);
const customerID = request.body.CustomerID;
const promises: any[] = [];
const p1 = admin.database().ref('Clients/' + clientID + '/UnitData/'+ unitID +'/FreshData/Orders/' + orderID + '/Status').set(orderStatusInt);
promises.push(p1);
if(notificationTokenString !== null && notificationTokenString.length !== 0 && notificationTokenString !== 'undefined' && !(customerID === null || customerID === undefined || customerID === ""))
{
const p2 = admin.database().ref('Customers/' + customerID + '/OrderHistory/' + orderID + '/Status').set(orderStatusInt);
promises.push(p2);
if(orderStatusInt > 0 && orderStatusInt < 4)
{
const p3 = admin.database().ref('Customers/' + customerID + '/ActiveOrders/' + orderID).set(orderStatusInt);
promises.push(p3);
}
else
{
const p4 = admin.database().ref('Customers/' + customerID + '/ActiveOrders/' + orderID).set(null);
promises.push(p4);
}
let title = String(request.body.NotificationTitle);
let message = String(request.body.NotificationMessage);
if(title === null || title.length === 0)
title = "?????";
if(message === null || message.length === 0)
message = "?????";
const payload =
{
notification:
{
title: title,
body: message,
icon: 'notification_icon',
sound : 'default'
}
};
const p5 = admin.messaging().sendToDevice(notificationTokenString, payload);
promises.push(p5);
}
return Promise.all(promises).then(r => { return response.status(200).send('success') })
.catch(error =>
{
console.error(new Error(error));
return response.status(500).send(error)
});
})
这是我调用它的方式,客户端应用程序在使用 c# 语言的 Xamarin Forms 应用程序上运行:
static HttpClient Client;
public static void Initialize()
{
Client = new HttpClient();
Client.BaseAddress = new Uri("My cloud functions adress");
Client.DefaultRequestHeaders.Add("UnitID", UnitService.GetUnitID());
Client.DefaultRequestHeaders.Add("ClientID", AuthenticationService.GetFirebaseAuth().User.LocalId);
}
public static async Task<bool> CallChangeOrderStatus(OrderHolder holder, int status)
{
Debug.WriteLine("CallChangeOrderStatus: " + status);
try
{
var content = new Dictionary<string, string>();
content.Add("OrderID", holder.Order.ID);
content.Add("OrderStatus", status.ToString());
if (!string.IsNullOrEmpty(holder.Order.NotificationToken) && NotificationService.ShouldSend(status))
{
content.Add("CustomerID", holder.Order.SenderID);
content.Add("NotificationToken", holder.Order.NotificationToken);
content.Add("NotificationTitle", NotificationService.GetTitle(status));
content.Add("NotificationMessage", NotificationService.GetMessage(status));
}
var result = await Client.PostAsync("changeOrderStatus_1", new FormUrlEncodedContent(content));
return result.IsSuccessStatusCode;
}
catch (HttpRequestException exc)
{
#if DEBUG
ErrorHandlerService.ShowErrorMessage(exc);
#endif
Crashes.TrackError(exc);
return false;
}
}
这些函数每分钟会被调用几次,但可能会长达一小时而不被调用。
我已经从移动连接、wifi 连接、有线连接和各种互联网提供商发送了请求,但问题仍然存在。
我做错了什么吗?我遗漏了什么吗?是谷歌服务器不稳定吗?
答案1
似乎昨天已知问题发生,我在 Cloud Functions 中遇到了同样的问题,支持团队也提到了此信息。如果您的问题仍未解决,我建议您联系GCP 支持团队以便他们仔细检查您的项目并提供进一步的建议:)