-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add cleanup function for cron job run details #511
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
base: 12-04-add_ensure_workers_sql_function_for_worker_invocation_orchestration
Are you sure you want to change the base?
Conversation
|
|
View your CI Pipeline Execution ↗ for commit 19991aa
☁️ Nx Cloud last updated this comment at |
| create or replace function pgflow.cleanup_ensure_workers_logs( | ||
| retention_hours integer default 24 | ||
| ) | ||
| returns table (cron_deleted bigint) | ||
| language sql | ||
| security definer | ||
| set search_path = pgflow, cron, pg_temp | ||
| as $$ | ||
| with deleted as ( | ||
| delete from cron.job_run_details | ||
| where job_run_details.end_time < now() - (cleanup_ensure_workers_logs.retention_hours || ' hours')::interval | ||
| returning 1 | ||
| ) | ||
| select count(*)::bigint as cron_deleted from deleted | ||
| $$; |
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.
The function lacks validation for the retention_hours parameter. Passing a negative value or zero could cause catastrophic data loss:
- Negative value:
retention_hours => -5creates the conditionend_time < now() + 5 hours, deleting records including those with recent or future end times - Zero:
retention_hours => 0createsend_time < now(), deleting ALL completed job records
Fix by adding validation at the start:
create or replace function pgflow.cleanup_ensure_workers_logs(
retention_hours integer default 24
)
returns table (cron_deleted bigint)
language sql
security definer
set search_path = pgflow, cron, pg_temp
as $$
with deleted as (
delete from cron.job_run_details
where job_run_details.end_time < now() - (cleanup_ensure_workers_logs.retention_hours || ' hours')::interval
and cleanup_ensure_workers_logs.retention_hours > 0 -- Add validation
returning 1
)
select count(*)::bigint as cron_deleted from deleted
$$;Or raise an exception for invalid input before the DELETE.
| create or replace function pgflow.cleanup_ensure_workers_logs( | |
| retention_hours integer default 24 | |
| ) | |
| returns table (cron_deleted bigint) | |
| language sql | |
| security definer | |
| set search_path = pgflow, cron, pg_temp | |
| as $$ | |
| with deleted as ( | |
| delete from cron.job_run_details | |
| where job_run_details.end_time < now() - (cleanup_ensure_workers_logs.retention_hours || ' hours')::interval | |
| returning 1 | |
| ) | |
| select count(*)::bigint as cron_deleted from deleted | |
| $$; | |
| create or replace function pgflow.cleanup_ensure_workers_logs( | |
| retention_hours integer default 24 | |
| ) | |
| returns table (cron_deleted bigint) | |
| language sql | |
| security definer | |
| set search_path = pgflow, cron, pg_temp | |
| as $$ | |
| with deleted as ( | |
| delete from cron.job_run_details | |
| where job_run_details.end_time < now() - (cleanup_ensure_workers_logs.retention_hours || ' hours')::interval | |
| and cleanup_ensure_workers_logs.retention_hours > 0 | |
| returning 1 | |
| ) | |
| select count(*)::bigint as cron_deleted from deleted | |
| $$; | |
Spotted by Graphite Agent
Is this helpful? React 👍 or 👎 to let us know.
7bc511b to
c298fab
Compare
6232598 to
ac3a640
Compare
c298fab to
6d218a6
Compare
ac3a640 to
28eab2e
Compare
6d218a6 to
923cbfb
Compare
28eab2e to
19991aa
Compare
🔍 Preview Deployment: Website✅ Deployment successful! 🔗 Preview URL: https://pr-511.pgflow.pages.dev 📝 Details:
_Last updated: _ |

Add cleanup function for cron job run details
This PR adds a new
cleanup_ensure_workers_logsfunction to prevent thecron.job_run_detailstable from growing indefinitely. The function:The PR includes:
Note that HTTP response logs (
net._http_response) are not cleaned by this function as they are automatically managed by pg_net with a 6-hour TTL.