import { delay } from "awaiting" ; import { PostgreSQL } from "./types" ; export async function projects_that_need_to_be_started ( database , limit = 10 ) { const result = await database . async_query ( { query : `SELECT project_id FROM projects WHERE (run_quota ->> 'always_running' = 'true' OR (settings ->> 'always_running')::TEXT = '1') AND (state ->> 'state' IN ('archived', 'closed', 'opened') OR state IS NULL) LIMIT ${ limit }` , } ) ; const projects = [ ] ; for ( const row of result . rows ) { projects . push ( row . project_id ) ; } return projects ; } export async function init_start_always_running_projects ( database , interval_s = 15 ) { while ( true ) { try { for ( const project_id of await projects_that_need_to_be_started ( database ) ) { const compute_server = ( database as any ) . compute_server ; if ( compute_server == null ) continue ; const project = compute_server ( project_id ) ; project . start ( ) ; } } catch ( err ) { console . warn ( "init_start_always_running_projects" , err ) ; } await delay ( interval_s * 1000 ) ; } }