-
Notifications
You must be signed in to change notification settings - Fork 216
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 DAG to remove Flickr thumbnails #2302
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d89cd3
Add `flickr_thumbnails_removal` DAG
b8873c6
Avoid shadowing built-in name 'license' in Flickr DAG
fe6cb1f
Expose the upstream DB port
19b4806
Update DAG docs
eaf5fad
Add locking clause
krysal 3bdce28
Log only the first run of the update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
One-time run DAG to remove progressively all the old Flickr thumbnails, | ||
as they were determined to be unsuitable for the Openverse UI requirements. | ||
""" | ||
import logging | ||
from datetime import timedelta | ||
from textwrap import dedent | ||
|
||
from airflow.decorators import dag, task | ||
|
||
from common.constants import DAG_DEFAULT_ARGS, POSTGRES_CONN_ID | ||
from common.slack import send_message | ||
from common.sql import PostgresHook | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
DAG_ID = "flickr_thumbnails_removal" | ||
|
||
|
||
@dag( | ||
dag_id=DAG_ID, | ||
default_args={ | ||
**DAG_DEFAULT_ARGS, | ||
"retries": 0, | ||
"execution_timeout": timedelta(days=7), | ||
}, | ||
schedule=None, | ||
catchup=False, | ||
doc_md=__doc__, | ||
) | ||
def flickr_thumbnails_removal(): | ||
pg = PostgresHook(postgres_conn_id=POSTGRES_CONN_ID) | ||
select_conditions = "FROM image WHERE provider = 'flickr' AND thumbnail IS NOT NULL" | ||
|
||
@task() | ||
def count(): | ||
num_thumbs = pg.get_first(f"SELECT COUNT(*) {select_conditions}")[0] | ||
logger.info(f"Flickr thumbnails found: {num_thumbs}.") | ||
|
||
return num_thumbs | ||
|
||
@task() | ||
def delete(num_thumbs): | ||
log_sql = True | ||
if num_thumbs == 0: | ||
logger.info("No Flickr thumbnails found.") | ||
|
||
while num_thumbs > 0: | ||
query = dedent( | ||
f""" | ||
UPDATE image SET thumbnail = NULL WHERE identifier IN ( | ||
SELECT identifier {select_conditions} | ||
FETCH FIRST 10000 ROWS ONLY FOR UPDATE SKIP LOCKED | ||
) | ||
""" | ||
) | ||
pg.run(query, log_sql=log_sql) | ||
num_thumbs -= 10000 | ||
logger.info( | ||
f"Flickr thumbnails left: {num_thumbs if num_thumbs > 0 else 0}." | ||
) | ||
log_sql = False | ||
|
||
@task() | ||
def report(): | ||
msg = ( | ||
"All Flickr thumbnails were successfully removed. " | ||
f"The `{DAG_ID}` DAG can be retired." | ||
) | ||
send_message(msg, DAG_ID) | ||
|
||
num_thumbs = count() | ||
d = delete(num_thumbs) | ||
r = report() | ||
d >> r | ||
|
||
|
||
flickr_thumbnails_removal() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my own edification, what is the reason behind this rename?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pycharm was complaining it was shadowing the built-in name.