Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎉 Implement scheduled event for positive TempIDs list #27

Merged
merged 16 commits into from
Apr 20, 2020

Conversation

shogo-mitomo
Copy link
Member

@shogo-mitomo shogo-mitomo commented Apr 17, 2020

TODO

  • get positive users in the last 14 days from Firestore
  • upsert the file of above positive users' TempIDs (in the last 17 days) list on Cloud Storage
  • turn this process into a scheduled event

Closes #10

@shogo-mitomo shogo-mitomo self-assigned this Apr 17, 2020
@shogo-mitomo shogo-mitomo added the enhancement New feature or request label Apr 17, 2020
@shogo-mitomo shogo-mitomo marked this pull request as draft April 17, 2020 02:54
@shogo-mitomo shogo-mitomo marked this pull request as ready for review April 19, 2020 15:50
Copy link
Member

@yashmurty yashmurty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added an issue that I faced. 🙇

serverless.yml Outdated
@@ -42,6 +43,11 @@ functions:
path: /{proxy+}
method: any
cors: true
schedule-main:
handler: dist/schedule-main.handler
Copy link
Member

@yashmurty yashmurty Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo
I can verify that this might not work, because I was trying to do something similar and ran into the same issue. 😢

I get the error:

[Nest] 29960   - 04/20/2020, 1:20:00 AM   
[ExceptionHandler] The default Firebase app already exists. 
This means you called initializeApp() more than once without providing an app name as the second argument. 
In most cases you only need to call initializeApp() once. 
But if you do want to initialize multiple apps, 
pass a second argument to initializeApp() to give each app a unique name. +4ms

You can reproduce this error by:

  1. Let the cron function run successfully.
  2. Try to load any endpoint, ex: http://localhost:3000/dev.

The app will crash.
I think it's happening because both the handlers try to create AppModule, which tries to re-create firebase instance each time for the lambda-main handler and schedule-main handler.
Hence this error message.
I'm further investigating right now how to handle this.

I guess easiest solution would be to split the handlers into mutliple serverless.yml files. This would mean the cron nest app will run in different AWS Lambda from main nest app.

This might actually be the behavior we want because we might not want to scale up our cron job as our main app scales up?. 🤔

@@ -42,6 +43,12 @@ functions:
path: /{proxy+}
method: any
cors: true
schedule-main:
handler: dist/schedule-main.handler
reservedConcurrency: 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo for reservedConcurrency
Does this mean that even if there are 100 lambda instances running, only 1 instance will run this cron function? 😲

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yashmurty
my understanding is:
Originally, lambda-main and schedule-main are separated as a Lambda Function, so they are invoked and scaled separately.
schedule-main function does not scale when many HTTP requests come in. This is because there is no http event defined for this function.
reservedConcurrency is a setting to prevent the next function from being invoked while the old function is running because it takes more time to process than the interval of Cron, for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogo-mitomo

Originally, lambda-main and schedule-main are separated as a Lambda Function, so they are invoked and scaled separately.

Noted.
If that is the case, then you will not face the firebase issue of multiple initialization as well, because each function will run on separate lambda. 🙏

Copy link
Member

@yashmurty yashmurty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🎉

@shogo-mitomo shogo-mitomo merged commit 9de47f6 into master Apr 20, 2020
@shogo-mitomo shogo-mitomo deleted the mitomo/positive_list branch April 20, 2020 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] creating a positive tempIDs list in Cloud Storage
2 participants