UNPKG

@wepublish/api

Version:
109 lines (108 loc) 4.2 kB
import { PrismaClient } from '@prisma/client'; import { MailContext } from "../../../../mail-api/src"; import { PaymentsService } from "../../../../payment-api/src"; import { SubscriptionService } from '../subscription/subscription.service'; /** * Controller responsible for performing periodic jobs. A new controller * instance must be created for every run. */ export declare class PeriodicJobService { private readonly prismaService; private readonly mailContext; private readonly subscriptionController; private readonly payments; private subscriptionEventDictionary; private runningJob?; private readonly logger; private randomNumberRangeForConcurrency; constructor(prismaService: PrismaClient, mailContext: MailContext, subscriptionController: SubscriptionService, payments: PaymentsService); getJobLog(take: number, skip?: number): import(".prisma/client").Prisma.PrismaPromise<(import("@prisma/client/runtime/library").GetResult<{ id: string; createdAt: Date; modifiedAt: Date; date: Date; executionTime: Date; successfullyFinished: Date; finishedWithError: Date; tries: number; error: string; }, unknown> & {})[]>; /** * Run the periodic jobs. This makes sure that no two instances of the same * controller run their jobs at the same time and returns if they are. * @returns void */ concurrentExecute(): Promise<void>; /** * Runs all outstanding {@link getOutstandingRuns} runs by doing the following for each run: * - send custom mails * - create invoices * - charge due invoices * - deactivate overdue subscriptions * If any of the tasks fail, the entire job is marked as failed. */ execute(customRunDate?: Date): Promise<void>; private findAndDeactivateExpiredNotAutoRenewSubscription; private findAndDeactivateSubscriptions; private findAndChargeDueInvoices; private findAndCreateInvoices; private findAndSendCustomMails; private sendCustomMails; private createInvoice; private chargeInvoice; private deactivateSubscription; /** * Mark a job as re-trying at the current date. * @param runDate The original date of the job run. */ private retryFailedJob; /** * Mark a job as started at the current date. * @param runDate the original date of the job run. */ private markJobStarted; /** * Check if any job is already being processed. * @returns if there are any jobs running. */ private isAlreadyAJobRunning; /** * Mark a job as completed in the database. */ private markJobSuccessful; /** * Sleep for a random time between 0 and 300 seconds to ensure that two parallel processes * are not starting to process the queue at the same time. * @returns void */ private sleepForRandomIntervalToEnsureConcurrency; /** * Mark a job as failed in the database by incrementing the `tries` count and updating the failure timestamp. * @param error a description of the error */ private markJobFailed; /** * Calculate the runs in the past that have not completed yet. * - If the Controller is run for the first time, this returns just todays run. * - If the last run had an error, it returns the run when the error happened. * - If there was an execution pause, it returns all runs between the last successful run and the current day. * @returns An array of pending runs. */ private getOutstandingRuns; /** * Generate an array of dates between the two bounds * @param startDate The beginning date (inclusive) * @param endDate The ending date (exclusive) * @returns An array of Date objects */ private generateDateArray; /** * Send an email and store it in the Mail Log * @param action the event and template to send * @param user the recipient * @param isRetry whether this is a retried delivery * @param optionalData unknown * @param periodicJobRunDate the current date for the delivery */ private sendTemplateMail; }