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

add exception handling to ckan upload script #3227

Merged
merged 3 commits into from
Jan 11, 2024

Conversation

charlie-costanzo
Copy link
Member

@charlie-costanzo charlie-costanzo commented Jan 10, 2024

Description

As described in #3182, when trying to upload data to the open data portal (CKAN), if one file fails (specifically, stop times tends to fail because of its size), then the subsequent files (in alphabetical order) are not even attempted.

This PR adds exception handling to warehouse/scripts/publish.py so that if one file upload fails the others are attemped.

Resolves #3182

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature

How has this been tested?

Will have to test in prod as this is an error generated by Caltrans servers

Post-merge follow-ups

  • No action required

@charlie-costanzo charlie-costanzo self-assigned this Jan 10, 2024
@charlie-costanzo charlie-costanzo force-pushed the handle-open-data-exceptions branch from c5f5e70 to 3654e25 Compare January 11, 2024 15:31
Copy link
Contributor

@SorenSpicknall SorenSpicknall left a comment

Choose a reason for hiding this comment

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

A few changes to request. Let me know if you want more context for any of this, or if there's anything I'm missing that informs the choices you made in the first draft!

file=fp,
resource_id=resource.id,
)
except requests.exceptions.RequestException as e:
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that this should be requests.exceptions.HTTPError, which is the specific class of error produced when an upload times out.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes this makes sense! I almost did that but defaulted to a more general exception for some unknown reason

resource_id=resource.id,
)
except requests.exceptions.RequestException as e:
print(e)
Copy link
Contributor

@SorenSpicknall SorenSpicknall Jan 11, 2024

Choose a reason for hiding this comment

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

We're already using typer.echo statements instead of the built-in print tool elsewhere in this script, so I'd make this into something like:

typer.echo(f"Failed to upload to {fpath} due to error: {e}")

We use styled logs in this script (which don't actually display with styling in the Airflow UI, so jury's out on whether it's worth continuing to do so). But if you want to align this with most of the other logging in the same script, you'd use the slightly more complex typer.secho command like this one:

typer.secho(f"Failed to upload to {fpath} due to error: {e}", fg=typer.colors.RED)

Copy link
Member Author

Choose a reason for hiding this comment

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

ah yes good point!

)
except requests.exceptions.RequestException as e:
print(e)
continue
Copy link
Contributor

@SorenSpicknall SorenSpicknall Jan 11, 2024

Choose a reason for hiding this comment

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

I don't think you need this continue statement. The end of the loop iteration, a few lines further down, manually deletes the df dataframe after the if-else logic but before the loop iteration completes, which was probably done for resource constraint reasons. We likely want to still be able to reach that line after an error is logged, after which point the loop will still move on to its next iteration as normal.

Copy link
Member Author

Choose a reason for hiding this comment

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

makes a lot of sense, I went back on forth on this for a long time too hah

@charlie-costanzo
Copy link
Member Author

A few changes to request. Let me know if you want more context for any of this, or if there's anything I'm missing that informs the choices you made in the first draft!

This review was very helpful Soren, thank you! I just pushed the changes now. Let me know if anything else looks off

@charlie-costanzo charlie-costanzo marked this pull request as ready for review January 11, 2024 17:38
@charlie-costanzo charlie-costanzo merged commit fde6d52 into main Jan 11, 2024
4 checks passed
@charlie-costanzo charlie-costanzo deleted the handle-open-data-exceptions branch January 11, 2024 19:31
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.

Open data publishing (GTFS): Handle file-level errors so subsequent files can be attempted
2 participants