Changeset View
Changeset View
Standalone View
Standalone View
swh/scheduler/backend.py
Show First 20 Lines • Show All 340 Lines • ▼ Show 20 Lines | ) -> List[ListedOrigin]: | ||||||||||||||||||||
timestamp = utcnow() | timestamp = utcnow() | ||||||||||||||||||||
origin_select_cols = ", ".join(ListedOrigin.select_columns()) | origin_select_cols = ", ".join(ListedOrigin.select_columns()) | ||||||||||||||||||||
query_args: List[Any] = [] | query_args: List[Any] = [] | ||||||||||||||||||||
where_clauses = [] | where_clauses = [] | ||||||||||||||||||||
# list of (name, query) handled as CTEs before the main query | |||||||||||||||||||||
common_table_expressions: List[Tuple[str, str]] = [] | |||||||||||||||||||||
# "NOT enabled" = the lister said the origin no longer exists | # "NOT enabled" = the lister said the origin no longer exists | ||||||||||||||||||||
where_clauses.append("enabled") | where_clauses.append("enabled") | ||||||||||||||||||||
# Only schedule visits of the given type | # Only schedule visits of the given type | ||||||||||||||||||||
where_clauses.append("visit_type = %s") | where_clauses.append("visit_type = %s") | ||||||||||||||||||||
query_args.append(visit_type) | query_args.append(visit_type) | ||||||||||||||||||||
if scheduled_cooldown: | if scheduled_cooldown: | ||||||||||||||||||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | ) -> List[ListedOrigin]: | ||||||||||||||||||||
# order by decreasing visit lag | # order by decreasing visit lag | ||||||||||||||||||||
order_by = """\ | order_by = """\ | ||||||||||||||||||||
listed_origins.last_update | listed_origins.last_update | ||||||||||||||||||||
- GREATEST( | - GREATEST( | ||||||||||||||||||||
origin_visit_stats.last_eventful, | origin_visit_stats.last_eventful, | ||||||||||||||||||||
origin_visit_stats.last_uneventful | origin_visit_stats.last_uneventful | ||||||||||||||||||||
) | ) | ||||||||||||||||||||
DESC | DESC | ||||||||||||||||||||
""" | """ | ||||||||||||||||||||
ardumont: We discussed the possibility to make this an enum with olasd (but that's a refactoring for… | |||||||||||||||||||||
else: | else: | ||||||||||||||||||||
raise UnknownPolicy(f"Unknown scheduling policy {policy}") | raise UnknownPolicy(f"Unknown scheduling policy {policy}") | ||||||||||||||||||||
select_query = f""" | # fmt: off | ||||||||||||||||||||
Done Inline Actions
ardumont: | |||||||||||||||||||||
common_table_expressions.insert(0, ("selected_origins", f""" | |||||||||||||||||||||
Done Inline Actionswe probably want those joins to be computed as well now. ardumont: we probably want those joins to be computed as well now. | |||||||||||||||||||||
Done Inline ActionsDrop this join which is no longer needed with the previous comment. ardumont: Drop this join which is no longer needed with the previous comment. | |||||||||||||||||||||
SELECT | SELECT | ||||||||||||||||||||
{origin_select_cols} | {origin_select_cols}, next_visit_queue_position | ||||||||||||||||||||
FROM | FROM | ||||||||||||||||||||
listed_origins | listed_origins | ||||||||||||||||||||
LEFT JOIN | LEFT JOIN | ||||||||||||||||||||
origin_visit_stats USING (url, visit_type) | origin_visit_stats USING (url, visit_type) | ||||||||||||||||||||
WHERE | WHERE | ||||||||||||||||||||
({") AND (".join(where_clauses)}) | ({") AND (".join(where_clauses)}) | ||||||||||||||||||||
Done Inline Actions
Use the limit parameter in the call to the method. ardumont: Use the limit parameter in the call to the method. | |||||||||||||||||||||
ORDER BY | ORDER BY | ||||||||||||||||||||
{order_by} | {order_by} | ||||||||||||||||||||
LIMIT %s | LIMIT %s | ||||||||||||||||||||
""" | """)) | ||||||||||||||||||||
# fmt: on | |||||||||||||||||||||
query_args.append(count) | query_args.append(count) | ||||||||||||||||||||
query = f""" | # fmt: off | ||||||||||||||||||||
WITH selected_origins AS ( | common_table_expressions.append(("update_stats", """ | ||||||||||||||||||||
{select_query} | |||||||||||||||||||||
), | |||||||||||||||||||||
update_stats AS ( | |||||||||||||||||||||
INSERT INTO | INSERT INTO | ||||||||||||||||||||
origin_visit_stats ( | origin_visit_stats (url, visit_type, last_scheduled) | ||||||||||||||||||||
url, visit_type, last_scheduled | |||||||||||||||||||||
) | |||||||||||||||||||||
SELECT | SELECT | ||||||||||||||||||||
url, visit_type, %s | url, visit_type, %s | ||||||||||||||||||||
FROM | FROM | ||||||||||||||||||||
selected_origins | selected_origins | ||||||||||||||||||||
ON CONFLICT (url, visit_type) DO UPDATE | ON CONFLICT (url, visit_type) DO UPDATE | ||||||||||||||||||||
SET last_scheduled = GREATEST( | SET last_scheduled = GREATEST( | ||||||||||||||||||||
origin_visit_stats.last_scheduled, | origin_visit_stats.last_scheduled, | ||||||||||||||||||||
EXCLUDED.last_scheduled | EXCLUDED.last_scheduled | ||||||||||||||||||||
) | ) | ||||||||||||||||||||
""")) | |||||||||||||||||||||
# fmt: on | |||||||||||||||||||||
query_args.append(timestamp) | |||||||||||||||||||||
formatted_ctes = ",\n".join( | |||||||||||||||||||||
f"{name} AS (\n{cte}\n)" for name, cte in common_table_expressions | |||||||||||||||||||||
) | ) | ||||||||||||||||||||
query = f""" | |||||||||||||||||||||
WITH | |||||||||||||||||||||
{formatted_ctes} | |||||||||||||||||||||
SELECT | SELECT | ||||||||||||||||||||
Done Inline Actions
This need to be a template and conditionned upon the right policy (the new one from this diff) ardumont: This need to be a template and conditionned upon the right policy (the new one from this diff) | |||||||||||||||||||||
* | {origin_select_cols} | ||||||||||||||||||||
Done Inline Actions
ardumont: | |||||||||||||||||||||
FROM | FROM | ||||||||||||||||||||
selected_origins | selected_origins | ||||||||||||||||||||
""" | """ | ||||||||||||||||||||
query_args.append(timestamp) | |||||||||||||||||||||
cur.execute(query, tuple(query_args)) | cur.execute(query, tuple(query_args)) | ||||||||||||||||||||
return [ListedOrigin(**d) for d in cur] | return [ListedOrigin(**d) for d in cur] | ||||||||||||||||||||
task_create_keys = [ | task_create_keys = [ | ||||||||||||||||||||
"type", | "type", | ||||||||||||||||||||
"arguments", | "arguments", | ||||||||||||||||||||
"next_run", | "next_run", | ||||||||||||||||||||
▲ Show 20 Lines • Show All 575 Lines • Show Last 20 Lines |
We discussed the possibility to make this an enum with olasd (but that's a refactoring for another time).