How To Purge Background Jobs on Postgresql
Steps to Purge Hangfire Jobs When the the following Feature Management Flag is enabled:
HangfirePostgresqlJobStorage
- Set the namespace:
export ns=<ENTER YOUR NAMESPACE>
Note: For embedded cluster, it will be export ns=default
- Stop the Swimlane API and Task Services:
kubectl -n $ns scale deploy swimlane-api swimlane-tasks --replicas=0
- Clear all Hangfire jobs and associated resources:
kubectl -n $ns exec -it postgresql-0 -- psql postgres://swimlane:@localhost:5432/swimlane -c 'drop schema hangfire cascade;'
- Re-start the Swimlane API and Task Services: NOTE: Scale the replicas back to their original settings (e.g., 1, 3, 5, etc.).
kubectl -n $ns scale deploy swimlane-api swimlane-tasks --replicas=<ORIGINAL-SETTINGS>
- Verify that Hangfire resources have been successfully re-created:
export ns=<your namespace>
# Log into Postgresql cli
kubectl -n $ns exec -it postgresql-0 -- psql postgres://swimlane:$(kubectl -n $ns get secrets swimlane.postgresql.credentials.postgresql.acid.zalan.do -o jsonpath="{.data.password}" | base64 -d)@localhost:5432/swimlane
# List of relations for hangfire schema
\dt hangfire.*
# Total number of records for each table within the hangfire schema
SELECT table_name, (SELECT n_live_tup FROM pg_stat_user_tables WHERE relname = table_name) AS row_count FROM information_schema.tables WHERE table_schema = 'hangfire';
# Select all records given the state status (Enqueued, Processing, Succeeded, Deleted)
SELECT * FROM hangfire.job WHERE statename='<ENTER STATE STATUS>'
Other helpful commands:
kubectl -n $ns exec -it postgresql-0 -- psql postgres://swimlane:@localhost:5432/swimlane -c '\dt hangfire.*'