如何解决为什么使用API脚本超时将数据导入Algolia搜索
我正在尝试使用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分钟超时,则可以在configure the function时执行。
functions.runWith({timeoutSeconds: X}).https.onRequest(async (req,res)
如果您的函数最终没有发送响应,则增加超时时间将无济于事,因此您还应该添加一些日志记录/调试程序,以了解对res.send()
的最终调用是否确实在发生。如果该函数从不发送响应,则无论发生什么情况,它肯定都会超时。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。