Files
ubicloud/model/vm_pool.rb
Jeremy Evans 718370f4c9 Convert ResourceMethods to a plugin
Allow plugin to take an etc_type keyword argument for using the
TYPE_ETC ubid type, and remove the separate definitions in every
model that uses the TYPE_ETC ubid type.

This was the cleanest way to DRY things up.  You cannot extend
the models with a module to do this before including ResourceMethods,
because then ResourceMethods::ClassMethods will override it, and
you cannot extend the models with a module to do this after
including ResourceMethods, because the inclusion will not work
correctly due to the eager definition of @ubid_format.

Best reviewed without whitespace differences.
2025-06-04 04:55:45 +09:00

43 lines
1.2 KiB
Ruby

# frozen_string_literal: true
require_relative "../model"
class VmPool < Sequel::Model
one_to_one :strand, key: :id
one_to_many :vms, key: :pool_id
plugin ResourceMethods
include SemaphoreMethods
semaphore :destroy
def pick_vm
# Find an available VM in the "running" state and not associated with a GitHub runner,
# and lock it with FOR UPDATE SKIP LOCKED.
vms_dataset
.where(Sequel[:vm][:display_state] => "running")
.exclude(id: DB[:github_runner].exclude(vm_id: nil).select(:vm_id))
.for_update
.skip_locked
.first
end
end
# Table: vm_pool
# Columns:
# id | uuid | PRIMARY KEY
# size | integer | NOT NULL
# vm_size | text | NOT NULL
# boot_image | text | NOT NULL
# storage_size_gib | bigint | NOT NULL
# arch | arch | NOT NULL DEFAULT 'x64'::arch
# storage_encrypted | boolean | NOT NULL DEFAULT true
# storage_skip_sync | boolean | NOT NULL DEFAULT false
# location_id | uuid | NOT NULL
# Indexes:
# vm_pool_pkey | PRIMARY KEY btree (id)
# Foreign key constraints:
# vm_pool_location_id_fkey | (location_id) REFERENCES location(id)
# Referenced By:
# vm | vm_pool_id_fkey | (pool_id) REFERENCES vm_pool(id)