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

perf(potato): reorganize promises #19

Merged
merged 2 commits into from
Nov 10, 2022
Merged

perf(potato): reorganize promises #19

merged 2 commits into from
Nov 10, 2022

Conversation

JonasBa
Copy link
Member

@JonasBa JonasBa commented Nov 5, 2022

Moved some promises around and used Promise.all so we can resolve them in parallel.

@JonasBa JonasBa force-pushed the jb/perf/gib-potato branch from 8a202ca to 8b58011 Compare November 5, 2022 18:20
return result["user"]["real_name"];
} catch (error) {
// Maybe do some proper error handling here
console.error(error);
Copy link
Member Author

Choose a reason for hiding this comment

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

I've removed these as uncaught exceptions are already going to be picked up by sentry.

console.error(error);
Sentry.captureException(error);

return "";
Copy link
Member Author

Choose a reason for hiding this comment

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

This would return an empty id in case of error and we were not checking if the id is falsy downstream. We'll see the crash in Sentry if it happens as opposed to silently hiding it. My guess is that this would not happen unless there is an intermittent problem with the slack query in which case we would still want to error.

return uid;
} else {
return user["id"];
if(user){
Copy link
Member Author

Choose a reason for hiding this comment

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

Happy path first

// If the user is not found in the Database add it to the Database
return getUserNameBySlackId(slackId).then(async (fullName) => {
const userId = uuid()
const imageURL = (await app.client.users.profile.get({ user: slackId }))[
Copy link
Member Author

Choose a reason for hiding this comment

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

only resolve the image if we are inserting a user

// Check that there are potatos left to give for the sender (sender ids)
const senderDBId = await getUserDbIdOrCreateUser(senderSlackId);
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved down in case we early return from any of the above statements and this is unused

@@ -219,17 +211,12 @@ async function givePotato({user, text, channel, ts}) {
}

// These will be our DB ids for the people that where mentioned
let userDBIds = [];

for (const userSlackId of receiverSlackIds) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This was executing each promise sequentially, we can do it in parallel.


// Add the message's to the DB
userDBIds.forEach(async (user) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

I dont think this was doing what we were expecting it to do... forEach here will return asap and not after all of the promises are awaited https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop

@JonasBa JonasBa changed the title perf(potato): reorganize a couple promises perf(potato): reorganize promises Nov 5, 2022
@JonasBa JonasBa requested a review from cleptric November 7, 2022 13:23
@cleptric
Copy link
Member

cleptric commented Nov 7, 2022

@JonasBa Thanks! Might take a while until this gets merged, I have to test it which is a bit involved 😬

@JonasBa
Copy link
Member Author

JonasBa commented Nov 7, 2022

Np, @cleptric, I'm fairly confident this should be fine, but better to be safe than sorry :D

@cleptric cleptric enabled auto-merge (squash) November 10, 2022 15:22
@cleptric cleptric disabled auto-merge November 10, 2022 15:22
@cleptric cleptric merged commit d89f6f0 into main Nov 10, 2022
@cleptric cleptric deleted the jb/perf/gib-potato branch November 10, 2022 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants