Quantcast
Channel: User Chris - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Firestore replying entire document changeset/history

$
0
0

I have two cloud functions, when a user authenticates with my frontend web app, it sets a flag on the user object, this triggers the first cloud function to execute:

exports.observeUserUpdate = functions.firestore    .document("/data/{uid}")    .onUpdate(async (snapshot) => {      const user = snapshot.after.data() as User;      if (user.scanRepositories) {        await pubSubClient            .topic("user-repositories-update")            .publish(Buffer.from(JSON.stringify(user)));      }    });

This function will send a topic to a queue where I have a subscriber that will pick up that message and perform some backend operations, modifying nested documents under the user.

I have a second cloud function that is listening for changes under the user document, but only acting on changes that I'm interested in:

exports.observeRepositoryUpdate = functions.firestore    .document("/data/{uid}/repositories/{repositoryId}")    .onUpdate(async (snapshot) => {      const repository = snapshot.after.data() as Repository;      if (repository.task == null || repository.task.length == 0) {        return;      }      if (repository.task.some((task: Task) => task.run)) {        await pubSubClient            .topic("repository-update")            .publish(Buffer.from(JSON.stringify(repository)));      }    });

When documents change and meet a certain condition it will fire off another topic for a different backend system to process.

This is all pretty straightforward and makes sense, where things are going wrong is the user is signing out and when they sign back in, and a flag is set, rightfully the first function triggers and the backend does what it is supposed to, however, all the messages that were sent as part of the second function, are being "replayed" to the consumer of that subject.

For example, the user logs in and creates tasks for the repositories. Those tasks are correctly executed and everything is working as it should. The user then logs out, and then back in, and those same tasks are fired again by the second function when they shouldn't be.

It looks like the second function is firing the entire changeset/history.

Is my data structure just wrong, or are my document paths incorrect? Would love some help and apologies this was very wordy.

edit: To help illustrate here is the sequence in the server logs of what's happening

// User logs in, login task fires as expected2021-10-28 12:12:27.489  INFO 60338 --- [pool-1-thread-2] s.f.g.listeners.consumer.UserConsumer    : User consumer triggered: User(uid=g1OQ0K1...)2021-10-28 12:12:27.492  INFO 60338 --- [pool-1-thread-2] s.f.github.services.GitHubServiceImpl    : Finding repositories2021-10-28 12:12:31.954  INFO 60338 --- [pool-1-thread-2] s.f.g.r.FirestoreRepositoryImpl          : Batch write for 131 repositories2021-10-28 12:13:05.827  INFO 60338 --- [pool-1-thread-2] s.f.g.r.FirestoreRepositoryImpl          : Batch write for 9 languages2021-10-28 12:13:05.829  INFO 60338 --- [pool-1-thread-2] s.f.g.r.FirestoreRepositoryImpl          : Batch write for 5 types// Login task complete // User runs task on repository2021-10-28 12:13:50.392  INFO 60338 --- [pool-1-thread-2] s.f.g.l.consumer.RepositoryConsumer      : Repository consumer triggered: RepositoryDto(id=39931...)2021-10-28 12:13:51.237  INFO 60338 --- [pool-1-thread-2] s.f.g.s.PackageManagerServiceImpl        : Detecting package manage for google-photo-frame with language Java2021-10-28 12:13:52.319  INFO 60338 --- [pool-1-thread-2] s.f.g.r.FirestoreRepositoryImpl          : Batch write for 1 package managers// Task completes// User logs out and back in, the RepositoryConsumer is triggered again when it shouldnt2021-10-28 12:15:09.917  INFO 60338 --- [pool-1-thread-5] s.f.g.l.consumer.RepositoryConsumer      : Repository consumer triggered: RepositoryDto(id=39931...)2021-10-28 12:15:10.807  INFO 60338 --- [pool-1-thread-5] s.f.g.s.PackageManagerServiceImpl        : Detecting package manage for google-photo-frame with language Java2021-10-28 12:15:11.872  INFO 60338 --- [pool-1-thread-5] s.f.g.r.FirestoreRepositoryImpl          : Batch write for 1 repositories2021-10-28 12:15:12.262  INFO 60338 --- [pool-1-thread-5] s.f.g.listeners.consumer.UserConsumer    : User consumer triggered: User(uid=g1OQ0K1...)2021-10-28 12:15:12.262  INFO 60338 --- [pool-1-thread-5] s.f.github.services.GitHubServiceImpl    : Finding repositories2021-10-28 12:12:31.954  INFO 60338 --- [pool-1-thread-2] s.f.g.r.FirestoreRepositoryImpl          : Batch write for 131 repositories2021-10-28 12:13:05.827  INFO 60338 --- [pool-1-thread-2] s.f.g.r.FirestoreRepositoryImpl          : Batch write for 9 languages2021-10-28 12:13:05.829  INFO 60338 --- [pool-1-thread-2] s.f.g.r.FirestoreRepositoryImpl          : Batch write for 5 types

Viewing all articles
Browse latest Browse all 40

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>