Add LICENSE_KEY refresh job for self hosted instances

This commit is contained in:
carlosmonastyrski
2025-04-02 14:57:34 -03:00
parent 13485cecbb
commit d82b06c72b
2 changed files with 24 additions and 5 deletions
@@ -5,6 +5,7 @@
// TODO(akhilmhdh): With tony find out the api structure and fill it here // TODO(akhilmhdh): With tony find out the api structure and fill it here
import { ForbiddenError } from "@casl/ability"; import { ForbiddenError } from "@casl/ability";
import { CronJob } from "cron";
import { Knex } from "knex"; import { Knex } from "knex";
import { TKeyStoreFactory } from "@app/keystore/keystore"; import { TKeyStoreFactory } from "@app/keystore/keystore";
@@ -85,6 +86,13 @@ export const licenseServiceFactory = ({
appCfg.LICENSE_KEY || "" appCfg.LICENSE_KEY || ""
); );
const syncLicenseKeyOnPremFeatures = async () => {
const {
data: { currentPlan }
} = await licenseServerOnPremApi.request.get<{ currentPlan: TFeatureSet }>("/api/license/v1/plan");
onPremFeatures = currentPlan;
};
const init = async () => { const init = async () => {
try { try {
if (appCfg.LICENSE_SERVER_KEY) { if (appCfg.LICENSE_SERVER_KEY) {
@@ -98,10 +106,7 @@ export const licenseServiceFactory = ({
if (appCfg.LICENSE_KEY) { if (appCfg.LICENSE_KEY) {
const token = await licenseServerOnPremApi.refreshLicense(); const token = await licenseServerOnPremApi.refreshLicense();
if (token) { if (token) {
const { await syncLicenseKeyOnPremFeatures();
data: { currentPlan }
} = await licenseServerOnPremApi.request.get<{ currentPlan: TFeatureSet }>("/api/license/v1/plan");
onPremFeatures = currentPlan;
instanceType = InstanceType.EnterpriseOnPrem; instanceType = InstanceType.EnterpriseOnPrem;
logger.info(`Instance type: ${InstanceType.EnterpriseOnPrem}`); logger.info(`Instance type: ${InstanceType.EnterpriseOnPrem}`);
isValidLicense = true; isValidLicense = true;
@@ -147,6 +152,15 @@ export const licenseServiceFactory = ({
} }
}; };
const initializeBackgroundSync = async () => {
if (appCfg.LICENSE_KEY) {
logger.info("Setting up background sync process for refresh onPremFeatures");
const job = new CronJob("*/10 * * * *", syncLicenseKeyOnPremFeatures);
job.start();
return job;
}
};
const getPlan = async (orgId: string, projectId?: string) => { const getPlan = async (orgId: string, projectId?: string) => {
logger.info(`getPlan: attempting to fetch plan for [orgId=${orgId}] [projectId=${projectId}]`); logger.info(`getPlan: attempting to fetch plan for [orgId=${orgId}] [projectId=${projectId}]`);
try { try {
@@ -662,6 +676,7 @@ export const licenseServiceFactory = ({
getOrgTaxInvoices, getOrgTaxInvoices,
getOrgTaxIds, getOrgTaxIds,
addOrgTaxId, addOrgTaxId,
delOrgTaxId delOrgTaxId,
initializeBackgroundSync
}; };
}; };
+4
View File
@@ -1607,6 +1607,10 @@ export const registerRoutes = async (
if (rateLimitSyncJob) { if (rateLimitSyncJob) {
cronJobs.push(rateLimitSyncJob); cronJobs.push(rateLimitSyncJob);
} }
const licenseSyncJob = await licenseService.initializeBackgroundSync();
if (licenseSyncJob) {
cronJobs.push(licenseSyncJob);
}
} }
server.decorate<FastifyZodProvider["store"]>("store", { server.decorate<FastifyZodProvider["store"]>("store", {