【问题标题】:Why is my data import to Algolia search using the API script timing out为什么我使用 API 脚本将数据导入到 Algolia 搜索时超时
【发布时间】:2020-08-26 19:36:25
【问题描述】:

我正在尝试使用 Algoliasearch 为我的 iOS 移动应用程序实现单索引搜索。我的应用程序中有大约 110 个用户。但是,当我将他们的数据上传到 Algolia 搜索的索引时,该功能会在上传所有用户之前超时。相反,它会在 http 浏览器中引发错误消息,并在 Firestore 控制台中声明超时。

Firestore 控制台:

sendCollectionToAlgolia

Function execution took 60044 ms, finished with status: 'timeout'

我使用本教程创建了函数: https://medium.com/@soares.rfarias/how-to-set-up-firestore-and-algolia-319fcf2c0d37

虽然我遇到了一些复杂情况,但如果您的应用使用 swiftUI iOS 平台并使用 Typescript 实现云功能,我强烈推荐该教程。

这是我的功能:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import algoliasearch from 'algoliasearch';

admin.initializeApp();
const db = admin.firestore();

const algoliaClient = algoliasearch(functions.config().algolia.appid, functions.config().algolia.apikey)
const collectionIndexName = functions.config().projectId === 'PROJECT-XXXX' ? 'prod_SEARCH' : 'dev_SEARCH';
const collectionIndex = algoliaClient.initIndex(collectionIndexName);
//rename to uploadUsersToAlgolia
export const sendCollectionToAlgolia = functions.https.onRequest(async (req, res) => {

    const algoliaRecords: any[] = [];

    const querySnapshot = await db.collection('users').get();

    querySnapshot.docs.forEach(doc => {
        const document = doc.data();

        const record = {
            objectID: doc.id,
            fullname: document.fullname,
            bio: document.bio,
            username: document.username,
            uid: document.uid,
            profileImageURL: document.profileImageURL,
            backgroundImageURL: document.backgroundImageURL,
            fcmToken: document.fcmToken,
            accountCreated: document.accountCreated,
            inspirationCount: document.inspriationCount,
            BucketListCount: document.BucketListCount,
            CompletedBucketListCount: document.CompletedBucketListCount,
            FriendsCount: document.FriendsCount
        };

        algoliaRecords.push(record);
    });

    // After all records are created, we save them to 
    collectionIndex.saveObjects(algoliaRecords, (_error: any, content: any) => {
        res.status(200).send("users collection was indexed to Algolia successfully.");
    });

});

【问题讨论】:

  • 您是否建议它适用于较少的用户,并且您是否因为正在处理的数据量超过了默认的 1 分钟超时时间?
  • 也许,我不确定是不是我编写的函数效率太低,无法在超时发生之前上传所有数据。有没有办法配置超时来延长定时器,或者我应该分组上传用户数据? @DougStevenson

标签: javascript firebase google-cloud-firestore google-cloud-functions algolia


【解决方案1】:

如果您只想更改默认的 1 分钟超时,可以在 configure the function 时进行。

functions.runWith({timeoutSeconds: X}).https.onRequest(async (req, res)

如果您的函数最终没有发送响应,则增加超时将无济于事,因此您还应该添加一些日志记录/调试来确定对res.send() 的最终调用是否真的发生了。如果函数从不发送响应,那么无论发生什么,它肯定会超时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2021-03-21
    • 2013-12-07
    • 2014-01-02
    • 2018-12-04
    • 1970-01-01
    • 2018-09-05
    相关资源
    最近更新 更多