Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Wait to fetch image data a few times in Watchdog. #4656

Merged
merged 3 commits into from
Jul 26, 2018

Conversation

chenba
Copy link
Collaborator

@chenba chenba commented Jul 17, 2018

Fixes #4612

Copy link
Member

@jaredhirsch jaredhirsch left a comment

Choose a reason for hiding this comment

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

I think the iteration code could be usefully simplified, see inline comments

return new Promise((resolve, reject) => {
const tryGetBytes = function() {
getRawBytesPromise(DELAY).then(obj => {
if (obj === null) {
Copy link
Member

Choose a reason for hiding this comment

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

The beauty of Promise code is that it's not necessary to manually maintain a counter.

What if getRawBytesPromise just did something like:

// Three attempts with 1000 msec delay
getRawBytesPromise(1000)
  .then(bytes => { return bytes ? bytes : getRawBytesPromise(1000) })
  .then(bytes => { return bytes ? bytes : getRawBytesPromise(1000) })
  .then(bytes => { bytes ? resolve(bytes) : reject(new Error(...)) }

I think this approach would be simpler and more idiomatic.

Copy link
Member

Choose a reason for hiding this comment

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

Er, sorry, I meant "what if getImageRawBytes did something like..."

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd argue in this case the counter is not necessary because we call the function three times. Promises don't have anything to do with it. 😃 It is easier to read though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently the code retries on the first two rejections from Shot.getRawBytesForClip and rejects with its Error on the third. I don't think we can switch to the proposed promise chain without losing the original Error from Shot.getRawBytesForClip. In order for getRawBytesPromise to return something falsey, it needs to ignore the rejections.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, you could handle the rejection case any number of ways while still getting rid of the manual iteration counting code. We can chat about this tomorrow after standup 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, I wasn't clear: I want to reject if it's null but retry two times if it's a rejection. Shot.getRawBytesForClip returns null when a (not {deleted,blocked,expired}) image is not found in the DB, so that's not an AWS/S3 related issue.

setTimeout(() => resolve(), waitInMs);
});
};
const getRawBytesPromise = function(timeToWait) {
Copy link
Member

Choose a reason for hiding this comment

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

Probably couldn't hurt to move getRawBytesPromise and the delay function out to top level, to avoid redeclaring them over and over.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmmm I did that to minimize the scope of the functions. I think it wouldn't hurt anything? I'll move them up.

@chenba chenba dismissed jaredhirsch’s stale review July 26, 2018 15:11

Added more commits based on feedback.

@chenba
Copy link
Collaborator Author

chenba commented Jul 26, 2018

Pushed additional commits for ease of review. For the promise chain approach, I added a flag for retrying on Shot.getRawBytesForClip rejection.

@jaredhirsch jaredhirsch merged commit dd3ceea into mozilla-services:master Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants