Cloud Functions 傳回狀態 500 且不顯示在登錄中

Cloud Functions 傳回狀態 500 且不顯示在登錄中

我的行為很奇怪,我在 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 連線、有線連線和各個網路供應商發送了請求,但問題仍然發生。

難道我做錯了什麼?我錯過了什麼嗎?是不是Google伺服器不穩定?

答案1

好像昨天有一個發生了已知問題,我在我的雲端功能中經歷了相同的行為,並且支援團隊提到了此資訊。如果您的問題仍未解決,建議您聯繫GCP 支援團隊以便他們仔細檢查您的項目並提供進一步的建議:)

相關內容