enh: block 0 subscription quantity from running

This commit is contained in:
Ben Allfree 2025-01-10 20:06:47 +00:00
parent 4d4590eec1
commit 5ce94cc8df
3 changed files with 18 additions and 101 deletions

View File

@ -678,105 +678,12 @@ var HandleMetaUpdateAtBoot = (c) => {
// src/lib/handlers/mirror/api/HandleMirrorData.ts // src/lib/handlers/mirror/api/HandleMirrorData.ts
var HandleMirrorData = (c) => { var HandleMirrorData = (c) => {
const dao = $app.dao(); const users = $app.dao().findRecordsByExpr("verified_users", $dbx.exp("1=1"));
const log = mkLog(`POST:mirror:instance`); const instances = $app.dao().findRecordsByExpr(
log(`***TOP OF POST`); "instances",
let data = new DynamicModel({ $dbx.exp("instances.uid in (select id from verified_users)")
identifier: "", );
externalMachineId: "", return c.json(200, { users, instances });
region: ""
});
log(`***before bind`);
c.bind(data);
log(`***after bind`);
data = JSON.parse(JSON.stringify(data));
const { identifier, externalMachineId, region } = data;
log(`***vars`, JSON.stringify({ identifier, externalMachineId, region }));
if (!identifier) {
throw new BadRequestError(`Identifier is required`);
}
if (!externalMachineId) {
throw new BadRequestError(`External Machine ID is required`);
}
if (!region) {
throw new BadRequestError(`Region is required`);
}
const safeDo = (fn) => {
try {
return fn();
} catch (e) {
log(`***error`, e);
return void 0;
}
};
const subdomain = identifier.split(".")[0];
let instance;
dao.runInTransaction((tx) => {
const foundInstance = (() => {
log(`***looking for instance by id`, subdomain);
const byId = safeDo(() => tx.findRecordById("instances", subdomain));
if (byId) return byId;
log(`***looking for instance by subdomain`, subdomain);
const bySubdomain = safeDo(
() => tx.findRecordsByExpr(
"instances",
$dbx.exp("subdomain = {:subdomain}", { subdomain })
)[0]
);
if (bySubdomain) return bySubdomain;
log(`***looking for instance by cname`, identifier);
const byCname = safeDo(
() => tx.findRecordsByExpr(
"instances",
$dbx.exp("cname = {:cname}", { cname: identifier })
)[0]
);
if (byCname) return byCname;
return void 0;
})();
log(`***found instance`, foundInstance);
if (!foundInstance) return;
log(`***running machine check`);
if (!foundInstance.getString("machine")) {
log(
`***no machine is assigned, looking up requesting machine by external id`,
externalMachineId
);
const existingMachine = safeDo(
() => tx.findRecordsByExpr(
"machines",
$dbx.exp("externalId = {:externalMachineId}", {
externalMachineId
})
)[0]
);
if (existingMachine) {
log(`***found machine`, existingMachine);
foundInstance.set("machine", existingMachine.getId());
tx.saveRecord(foundInstance);
log(`***saved instance with assigned machine`, foundInstance);
} else {
log(`***no machine found, creating new machine`);
const collection = tx.findCollectionByNameOrId("machines");
const newMachine = new Record(collection, {
externalId: externalMachineId,
region
});
tx.saveRecord(newMachine);
log(`***saved new machine id: ${newMachine.getId()}`, newMachine);
foundInstance.set("machine", newMachine.getId());
tx.saveRecord(foundInstance);
log(`***saved instance with assigned machine`, foundInstance);
}
}
log(`***got here`);
instance = foundInstance;
});
log(`***instance`, instance);
if (!instance) {
throw new NotFoundError(`Instance not found`);
}
return c.json(200, instance);
}; };
// src/lib/util/mkNotificationProcessor.ts // src/lib/util/mkNotificationProcessor.ts

View File

@ -61,8 +61,8 @@ onAfterBootstrap((e) => {
// src/lib/handlers/mirror/hooks.ts // src/lib/handlers/mirror/hooks.ts
routerAdd( routerAdd(
"POST", "GET",
"/api/mirror/instance", "/api/mirror",
(c) => { (c) => {
return require(`${__hooks}/mothership`).HandleMirrorData(c); return require(`${__hooks}/mothership`).HandleMirrorData(c);
}, },

View File

@ -345,6 +345,16 @@ export const instanceService = mkSingleton(
throw new Error(instance.suspension) throw new Error(instance.suspension)
} }
/*
Active instance check
*/
dbg(`Checking for active instances`)
if (owner.subscription_quantity === 0) {
throw new Error(
`Instances will not run until you <a href=${APP_URL(`access`)}>upgrade</a>`,
)
}
/* /*
power check power check
*/ */