云功能-在可变时间后删除Firestore文档并通知用户 [英] Cloud Functions - Remove Firestore documents after variable time and notify user

查看:60
本文介绍了云功能-在可变时间后删除Firestore文档并通知用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到过类似的问题,即根据此处的写入触发器,在一定时间后删除Firebase Realtime Database数据

I have seen a similar question with deleting Firebase Realtime Database data after a certain time, based on a write trigger here

删除2小时之前的Firebase数据

如何在Firebase Firestore中实现这一目标?

信息:

我的Firestore布局为:
主要收藏
-文件1
------子集合
---------- Doc xyz1
---------- Doc xyz2
-文件2
------子集合
---------- Doc abc1
----------博士tyu1
---------- Doc tyu2

My Firestore layout is:
Main Collection
--Doc 1
------Sub Collection
----------Doc xyz1
----------Doc xyz2
--Doc 2
------Sub Collection
----------Doc abc1
----------Doc tyu1
----------Doc tyu2

每个用户可能有多个子文档(xyz1,xyz2),他们可以更改将其加载到哪个主文档中.这两个集合都是已知的并且不会更改,但是主要文档和子文档都是可变的.该功能应等待一段可变的时间(10-120分钟),然后再删除特定的子文档.完成后,已删除子文档的用户/所有者需要通知.

我能找到的最好的办法是,我没有能够使用的Firestore生存时间(TTL)文档,并且建议进行的定期检查/清理对我不起作用具体用例.

Each user may have multiple Sub Docs (xyz1, xyz2), and they could change which Main Doc it's loaded into. Both collections are known and won't change, but the main doc and sub docs are variable. The function should wait a variable amount of time (10-120 minutes) before removing specific Sub Docs. When complete, the user/owner of the deleted Sub Docs needs a notification.

Best I can find is there are no Time To Live (TTL) documents for Firestore which I would be able to make work, and periodic checking/cleaning that is advised just doesn't work for my specific use case.

编辑2-其他详细信息-::每个用户将私下保存其唯一的文档(abc1).他们将自行决定选择一个主要文档(上述文档2),并将其文档提交给其子集合.
在当前时间点,当用户私人文档成功提交给公共子集合时,aysnc任务将具有倒数计时器,计时器完成后将删除用户上述文档.这很好用,但是当用户离开应用程序或在计时器到时没有互联网连接时,不会删除文档.

这使我想到了最初的问题,以为我可以在计时器倒计时后利用Cloud Functions来执行删除操作.

Edit 2 -Further Detail-: Each user will privately hold their unique doc/s (abc1). At their discretion, they will pick a Main doc (Doc 2 in the above), and commit their doc to its sub-collection.
At this current point in time, when the users private doc is successfully submitted to the public sub-collection, an aysnc task has a countdown timer, on completion of which, will then remove the users aforementioned document. This works great, but when the user leaves the app or doesn't have internet connection when the timer is up, the docs aren't deleted.

This leads me to the original question, thinking I could utilise Cloud Functions to perform the removal actions after the timer count-down.

推荐答案

Firestore没有TTL机制.而且Cloud Functions没有调度机制.您将不得不找到另一种安排某些工作的方法,这些工作将清除旧文档.

Firestore doesn't have a TTL mechanism. And Cloud Functions doesn't have a scheduling mechanism. You will have to find another way to schedule some work that will wipe out old documents.

您可以选择以下几种方式进行调查:

You have a couple of options to investigate:

  1. 您可以使用一些 cron式调度机制来ping定期查询和删除旧文档的功能.这意味着它们可能无法在您想要的时间被完全删除.
  2. 使用 Cloud Tasks 安排要在以后进行的工作.当前,该任务将需要在App Engine引擎实例上运行,并且可能会通过您在项目中创建的另一个函数来回调到Cloud Functions中.
  1. You could using some cron-style scheduling mechanism to ping a function to periodically query for and delete old documents. This implies that they may not be deleted exactly at the time you want.
  2. Use Cloud Tasks to schedule work to be done at a later time. Currently, the task will need to run on an App Engine engine instance, and may call back into Cloud Functions through another function you create in your project.

这两种设置和维护都很重要.

Both of these are non-trivial to set up and maintain. Read this blog for a more

这篇关于云功能-在可变时间后删除Firestore文档并通知用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆