Skip to content

Add Stop Stale Environment Service

Allen Cook requested to merge 19724-add-stop-stale-environments-worker into master

What does this MR do and why?

Adds a service that finds and stops environments that haven't been deployed to or updated after a certain date, which will be used in later MRs to add an API for stopping stale environments

SQL Query

SELECT "environments".id FROM "environments" LEFT JOIN "deployments" on "deployments"."environment_id" = "environments".id AND "deployments"."project_id" = 278964 AND deployments.updated_at >= '01-01-2023' WHERE environments.state IN ('AVAILABLE') AND "environments"."project_id" = 278964 AND "environments"."updated_at" < '01-01-2023' AND "environments"."name" != 'protected_prod' AND "environments"."tier" != 0 GROUP BY environments.id, deployments.id HAVING deployments.id IS NULL

https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/14241/commands/49983

How to set up and validate locally

  1. Execute Environment::StopStaleService in a console with Environments::StopStaleService.new(project, user, { before: 2.weeks.ago }).execute
    • project is a project with environments that haven't been used in over 2 weeks
    • user is a user with stop_environment permissions on the project
  2. You should see the message Successfully scheduled stale environments to stop returned
  3. project.environments should show any older environments to be stopped

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #19724 (closed)

Edited by Allen Cook

Merge request reports