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 class Instance < ApplicationRecord
has_many :pops has_many :pops, dependent: :destroy
has_many :fairies has_many :fairies, dependent: :destroy
validates :zone, inclusion: { in: %w[anemos pagos pyros hydatos] } validates :zone, inclusion: { in: %w[anemos pagos pyros hydatos] }
end end

View file

@ -21,8 +21,14 @@ default: &default
development: development:
primary:
<<: *default <<: *default
database: ecoffee_development database: ecoffee_development
queue:
<<: *default
database: ecoffee_development_queue
migrations_paths: db/queue_migrate
# The specified database role being used to connect to PostgreSQL. # The specified database role being used to connect to PostgreSQL.
# To create additional roles in PostgreSQL see `$ createuser --help`. # 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. # Highlight code that enqueued background job in logs.
config.active_job.verbose_enqueue_logs = true 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. # Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true # config.i18n.raise_on_missing_translations = true

View file

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

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| create_table "solid_queue_blocked_executions", force: :cascade do |t|
t.bigint "job_id", null: false t.bigint "job_id", null: false
t.string "queue_name", null: false t.string "queue_name", null: false

View file

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