mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
add workers
This commit is contained in:
5
Common/Types/Sleep.ts
Normal file
5
Common/Types/Sleep.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export default class Sleep {
|
||||
public static async sleep(ms: number): Promise<void>{
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
@@ -320,6 +320,21 @@ export class BillingService {
|
||||
|
||||
await this.stripe.subscriptions.del(subscriptionId);
|
||||
}
|
||||
|
||||
|
||||
public static async getSubscriptionStatus(
|
||||
subscriptionId: string
|
||||
): Promise<string> {
|
||||
if (!this.isBillingEnabled()) {
|
||||
throw new BadDataException(
|
||||
'Billing is not enabled for this server.'
|
||||
);
|
||||
}
|
||||
|
||||
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
|
||||
|
||||
return subscription.status;
|
||||
}
|
||||
}
|
||||
|
||||
export default BillingService;
|
||||
|
||||
@@ -148,6 +148,20 @@ export default class Model extends TenantModel {
|
||||
})
|
||||
public paymentProviderCustomerId?: string = undefined;
|
||||
|
||||
@ColumnAccessControl({
|
||||
create: [],
|
||||
read: [Permission.ProjectOwner, Permission.ProjectAdmin],
|
||||
update: [],
|
||||
})
|
||||
@TableColumn({ type: TableColumnType.ShortText })
|
||||
@Column({
|
||||
type: ColumnType.ShortText,
|
||||
length: ColumnLength.ShortText,
|
||||
nullable: true,
|
||||
unique: false,
|
||||
})
|
||||
public paymentProviderSubscriptionStatus?: string = undefined;
|
||||
|
||||
@ColumnAccessControl({
|
||||
create: [],
|
||||
read: [Permission.ProjectOwner, Permission.ProjectAdmin],
|
||||
|
||||
60
Workers/Jobs/PaymentProvider/CheckSubscriptionStatus.ts
Normal file
60
Workers/Jobs/PaymentProvider/CheckSubscriptionStatus.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
import { EVERY_DAY } from '../../Utils/CronTime';
|
||||
import RunCron from '../../Utils/Cron';
|
||||
import ProjectService from 'CommonServer/Services/ProjectService';
|
||||
import LIMIT_MAX from 'Common/Types/Database/LimitMax';
|
||||
import Project from 'Model/Models/Project';
|
||||
import BillingService from 'CommonServer/Services/BillingService';
|
||||
import { IsBillingEnabled } from 'CommonServer/Config';
|
||||
import logger from 'CommonServer/Utils/Logger';
|
||||
import Sleep from 'Common/Types/Sleep';
|
||||
|
||||
RunCron('PaymentProvider:CheckSubscriptionStatus', EVERY_DAY, async () => {
|
||||
// get all projects.
|
||||
if (!IsBillingEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const projects: Array<Project> = await ProjectService.findBy({
|
||||
query: {},
|
||||
select: {
|
||||
_id: true,
|
||||
paymentProviderSubscriptionId: true,
|
||||
paymentProviderCustomerId: true,
|
||||
},
|
||||
limit: LIMIT_MAX,
|
||||
skip: 0,
|
||||
props: {
|
||||
isRoot: true,
|
||||
ignoreHooks: true
|
||||
}
|
||||
});
|
||||
|
||||
for (const project of projects) {
|
||||
try {
|
||||
if (project.paymentProviderSubscriptionId) {
|
||||
|
||||
// get subscription detail.
|
||||
const subscriptionState = await BillingService.getSubscriptionStatus(project.paymentProviderSubscriptionId as string);
|
||||
|
||||
await ProjectService.updateOneById({
|
||||
id: project.id!,
|
||||
data: {
|
||||
paymentProviderSubscriptionStatus: subscriptionState
|
||||
},
|
||||
props: {
|
||||
isRoot: true,
|
||||
ignoreHooks: true
|
||||
}
|
||||
});
|
||||
|
||||
// after every subscription fetch, sleep for a 5 seconds to avoid stripe rate limit.
|
||||
await Sleep.sleep(5000);
|
||||
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
@@ -1 +1,2 @@
|
||||
export const EVERY_MINUTE: string = '* * * * *';
|
||||
export const EVERY_DAY: string = '0 8 * * *';
|
||||
Reference in New Issue
Block a user