feat: clean up old instances automatically

This commit is contained in:
insects 2025-03-12 14:08:04 +01:00
parent 5aa5fe0f5a
commit 070ffa8eeb
8 changed files with 85 additions and 42 deletions

View file

@ -0,0 +1,13 @@
class InstanceCleanupJob < ApplicationJob
queue_as :default
def perform(*args)
instances = Instance.all
to_be_deleted = instances.filter { |i|
# Mark instances older than 2 days for deletion
(Time.now.utc - i.created_at) / 2.days >= 1
}
Instance.where(id: to_be_deleted.map { |m| m.id }).destroy_all
print "#{to_be_deleted.length} instances destroyed\n"
end
end

View file

@ -1,6 +1,6 @@
class Instance < ApplicationRecord
has_many :pops
has_many :fairies
has_many :pops, dependent: :destroy
has_many :fairies, dependent: :destroy
validates :zone, inclusion: { in: %w[anemos pagos pyros hydatos] }
end

View file

@ -21,8 +21,14 @@ default: &default
development:
primary:
<<: *default
database: ecoffee_development
queue:
<<: *default
database: ecoffee_development_queue
migrations_paths: db/queue_migrate
# The specified database role being used to connect to PostgreSQL.
# To create additional roles in PostgreSQL see `$ createuser --help`.

View file

@ -54,6 +54,9 @@ Rails.application.configure do
# Highlight code that enqueued background job in logs.
config.active_job.verbose_enqueue_logs = true
config.active_job.queue_adapter = :solid_queue
config.active_job.verbose_enqueue_logs = true
config.solid_queue.connects_to = { database: { writing: :queue } }
# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true

View file

@ -1,10 +1,8 @@
# production:
# periodic_cleanup:
# class: CleanSoftDeletedRecordsJob
# queue: background
# args: [ 1000, { batch_size: 500 } ]
# schedule: every hour
# periodic_command:
# command: "SoftDeletedRecord.due.delete_all"
# priority: 2
# schedule: at 5am every day
production:
periodic_cleanup:
class: InstanceCleanupJob
schedule: every 2 days at 10:00am
development:
periodic_cleanup:
class: InstanceCleanupJob
schedule: every 30 seconds

View file

@ -1,4 +1,19 @@
ActiveRecord::Schema[7.1].define(version: 1) do
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 1) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
create_table "solid_queue_blocked_executions", force: :cascade do |t|
t.bigint "job_id", null: false
t.string "queue_name", null: false

View file

@ -14,6 +14,7 @@ PORT = '8080'
[processes]
app = './bin/rails server'
jobs = "./bin/jobs start"
[deploy]
release_command = "./bin/rails db:prepare"

View file

@ -0,0 +1,7 @@
require "test_helper"
class InstanceCleanupJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end