All files / src/Program/services pgboss.ts

85.55% Statements 77/90
90.9% Branches 10/11
79.31% Functions 23/29
86.2% Lines 75/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 27241x                                 41x   41x                         41x   41x     41x 41x       41x                     41x               3x   5x   5x             5x 5x     5x 1x 1x   4x                                         41x 4x   41x 6x         41x 3x 3x 3x 3x 3x 3x     3x   26x   3x 3x 3x       3x   3x           3x   3x   3x   1x 1x   5x           2x   2x       3x   4x   4x     3x     4x   5x               3x 3x   3x         3x       1x 1x           3x 3x             3x 3x       3x 3x           3x 4x   4x 4x   4x         1x   1x       3x   3x 2x   2x     3x       3x                                                  
import {
  BlockEntity,
  CurrentPoolMetricsEntity,
  PgConnectionConfig,
  PoolDelistedEntity,
  PoolMetadataEntity,
  PoolRegistrationEntity,
  PoolRetirementEntity,
  PoolRewardsEntity,
  StakePoolEntity,
  createDataSource,
  createPgBoss,
  isRecoverableTypeormError
} from '@cardano-sdk/projection-typeorm';
import { CommonProgramOptions, PosgresProgramOptions } from '../options';
import { DataSource } from 'typeorm';
import { HealthCheckResponse } from '@cardano-sdk/core';
import { HttpService } from '../../Http/HttpService';
import { Logger } from 'ts-log';
import {
  Observable,
  Subject,
  Subscription,
  catchError,
  concat,
  finalize,
  from,
  merge,
  share,
  switchMap,
  tap
} from 'rxjs';
import { PgBossQueue, WorkerHandler, queueHandlers } from '../../PgBoss';
import { Pool } from 'pg';
import { Router } from 'express';
import { ScheduleConfig } from '../../util/schedule';
import { StakePoolMetadataProgramOptions } from '../options/stakePoolMetadata';
import { contextLogger } from '@cardano-sdk/util';
import { retryBackoff } from 'backoff-rxjs';
import PgBoss from 'pg-boss';
 
/** The entities required by the job handlers */
export const pgBossEntities: Function[] = [
  CurrentPoolMetricsEntity,
  BlockEntity,
  PoolDelistedEntity,
  PoolMetadataEntity,
  PoolRegistrationEntity,
  PoolRetirementEntity,
  PoolRewardsEntity,
  StakePoolEntity
];
 
export const createPgBossDataSource = (connectionConfig$: Observable<PgConnectionConfig>, logger: Logger) =>
  // TODO: use createObservableDataSource from projection-typeorm package.
  // A challenge in doing that is that we call subscriber.error on retryable errors in order to reconnect.
  // Doing that with createObservableDataSource will 'destroy' the data source that's currently used,
  // so pg-boss is then unable to update job status and it stays 'active', not available for the newly
  // recreated worker to be picked up.
  // TODO: this raises another question - what happens when database connection drops while working on a job?
  // Will it stay 'active' forever, or will pg-boss eventually update it due to some sort of timeout?
  connectionConfig$.pipe(
    switchMap((connectionConfig) =>
      from(
        (async () => {
          const dataSource = createDataSource({
            connectionConfig,
            entities: pgBossEntities,
            extensions: { pgBoss: true },
            logger,
            options: { migrationsRun: false }
          });
          await dataSource.initialize();
          const pgbossSchema = await dataSource.query(
            "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'pgboss'"
          );
          if (pgbossSchema.length === 0) {
            await dataSource.destroy();
            throw new Error('Database schema is not ready. Please make sure projector is running.');
          }
          return dataSource;
        })()
      )
    )
  );
 
export type PgBossWorkerArgs = CommonProgramOptions &
  StakePoolMetadataProgramOptions &
  PosgresProgramOptions<'DbSync'> &
  PosgresProgramOptions<'StakePool'> & {
    parallelJobs: number;
    queues: PgBossQueue[];
    schedules: Array<ScheduleConfig>;
  };
 
export interface PgBossServiceDependencies {
  connectionConfig$: Observable<PgConnectionConfig>;
  db: Pool;
  logger: Logger;
}
 
const hasDriverError = (error: unknown): error is { driverError: unknown } =>
  error instanceof Object && 'driverError' in error;
 
const isRecoverableError = (error: unknown) =>
  isRecoverableTypeormError(error) ||
  (hasDriverError(error) &&
    error.driverError instanceof Error &&
    error.driverError.message === 'invalid message format');
 
export class PgBossHttpService extends HttpService {
  #config: PgBossWorkerArgs;
  #dataSource$: Observable<DataSource>;
  #db: Pool;
  #subscription?: Subscription;
  #health: HealthCheckResponse = { ok: false, reason: 'PgBossHttpService not started' };
  onUnrecoverableError$ = new Subject<unknown>();
 
  constructor(cfg: PgBossWorkerArgs, deps: PgBossServiceDependencies) {
    const { connectionConfig$, db, logger } = deps;
 
    super('pg-boss-service', { healthCheck: async () => this.#health }, Router(), __dirname, logger);
 
    this.#config = cfg;
    this.#db = db;
    this.#dataSource$ = createPgBossDataSource(connectionConfig$, this.logger);
  }
 
  protected async startImpl() {
    await super.startImpl();
 
    Iif (this.#subscription) {
      this.logger.warn('Unsubscribing from an observable which should never happen');
      this.#subscription.unsubscribe();
    }
 
    // Used for later use of firstValueFrom() to avoid it subscribes again
    const sharedWork$ = this.work().pipe(share());
 
    return new Promise<void>((resolve, reject) => {
      // Subscribe to work() to create the first DataSource and start pg-boss
      this.#subscription = sharedWork$.subscribe({
        error: (error) => {
          this.onUnrecoverableError$.next(error);
          reject(error);
        },
        next: () => resolve()
      });
    });
  }
 
  protected async shutdownImpl() {
    await super.shutdownImpl();
 
    this.#subscription?.unsubscribe();
  }
 
  private work() {
    return this.#dataSource$.pipe(
      switchMap((dataSource) => {
        const pgBoss = createPgBoss(null, this.logger, dataSource);
 
        return concat(
          from(pgBoss.start()),
          merge(
            ...this.#config.queues.map((queue) => this.createQueue(dataSource, pgBoss, queue, this.#db)),
            ...this.#config.schedules.map((schedule) => this.createSchedule(dataSource, pgBoss, schedule, this.#db))
          )
        ).pipe(finalize(() => pgBoss.stop().catch((error) => this.logger.warn('Error stopping pgBoss', error))));
      }),
      tap(() => (this.#health = { ok: true })),
      retryBackoff({
        // TODO: set this in the config
        initialInterval: 10,
        maxInterval: 5000,
        resetOnSuccess: true,
        // This ensures that if an error which can't be retried arrives here is handled as a FATAL error
        shouldRetry: (error: unknown) => {
          const retry = isRecoverableError(error);
          this.logger.debug('work() shouldRetry', retry, error);
 
          this.#health = {
            ok: false,
            reason: retry ? 'DataBase error: reconnecting...' : 'Fatal error! Shutting down'
          };
 
          return retry;
        }
      }),
      catchError((error) => {
        this.logger.error('Fatal worker error', error);
        throw error;
      })
    );
  }
 
  private createQueue(dataSource: DataSource, pgBoss: PgBoss, queue: PgBossQueue, db: Pool) {
    const logger = contextLogger(this.logger, queue);
    const handler = queueHandlers[queue]({
      dataSource,
      db,
      logger,
      ...this.#config
    });
 
    logger.info('Creating worker');
    return this.workQueue(handler, logger, pgBoss, queue);
  }
 
  private workQueue(handler: WorkerHandler, logger: Logger, pgBoss: PgBoss, queue: PgBossQueue) {
    return new Observable((subscriber) => {
      const workOption = {
        teamConcurrency: this.#config.parallelJobs,
        teamRefill: true,
        teamSize: this.#config.parallelJobs
      };
 
      const baseHandler = async (job: PgBoss.JobWithDoneCallback<unknown, unknown>) => {
        const { id, data } = job;
 
        try {
          logger.info(`Job ${id} started`, data);
 
          await handler(data);
 
          // Emit an unused value just to:
          // - make the tap() in work() to set the healthy state
          // - reset the retryBackoff in work() to reset the counter of its retry strategy
          subscriber.next(null);
 
          logger.info(`Job ${id} successfully completed`, data);
        } catch (error) {
          // pg-boss may retry the job eventually, but this is opaque,
          // so the error is logged here for more insight
          logger.error(`Job ${id} got error`, error);
 
          if (isRecoverableError(error)) {
            logger.info('The error is recoverable: re-creating DB connection');
            // Emit the error so retryBackoff in work() can do its job
            subscriber.error(error);
          }
 
          throw error;
        }
      };
 
      pgBoss.work(queue, workOption, baseHandler).catch((error: unknown) => {
        logger.error(`Error while starting worker for '${queue}'`, error);
        subscriber.error(error);
      });
    });
  }
 
  private createSchedule(
    _dataSource: DataSource,
    pgBoss: PgBoss,
    schedule: ScheduleConfig,
    _db: Pool
  ): Observable<unknown> {
    const { queue, cron, data, scheduleOptions } = schedule;
    const logger = contextLogger(this.logger, queue);
 
    return new Observable((subscriber) => {
      pgBoss.schedule(queue, cron, data, scheduleOptions).catch((error: unknown) => {
        logger.error('Error while scheduling', error);
        subscriber.error(error);
      });
      logger.info('Schedule created');
    });
  }
}