mirror of https://github.com/ghostfolio/ghostfolio
11 changed files with 182 additions and 11 deletions
@ -0,0 +1,3 @@ |
|||||
|
export interface IPortfolioSnapshotQueueJob { |
||||
|
userId: string; |
||||
|
} |
@ -0,0 +1,36 @@ |
|||||
|
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; |
||||
|
import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module'; |
||||
|
import { ExchangeRateDataModule } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.module'; |
||||
|
import { MarketDataModule } from '@ghostfolio/api/services/market-data/market-data.module'; |
||||
|
import { PortfolioSnapshotService } from '@ghostfolio/api/services/portfolio-snapshot/portfolio-snapshot.service'; |
||||
|
import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module'; |
||||
|
import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; |
||||
|
import { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile/symbol-profile.module'; |
||||
|
import { PORTFOLIO_SNAPSHOT_QUEUE } from '@ghostfolio/common/config'; |
||||
|
|
||||
|
import { BullModule } from '@nestjs/bull'; |
||||
|
import { Module } from '@nestjs/common'; |
||||
|
|
||||
|
import { PortfolioSnapshotProcessor } from './portfolio-snapshot.processor'; |
||||
|
|
||||
|
@Module({ |
||||
|
imports: [ |
||||
|
BullModule.registerQueue({ |
||||
|
// limiter: {
|
||||
|
// duration: ms('4 seconds'),
|
||||
|
// max: 1
|
||||
|
// },
|
||||
|
name: PORTFOLIO_SNAPSHOT_QUEUE |
||||
|
}), |
||||
|
ConfigurationModule, |
||||
|
DataProviderModule, |
||||
|
ExchangeRateDataModule, |
||||
|
MarketDataModule, |
||||
|
PrismaModule, |
||||
|
PropertyModule, |
||||
|
SymbolProfileModule |
||||
|
], |
||||
|
providers: [PortfolioSnapshotProcessor, PortfolioSnapshotService], |
||||
|
exports: [BullModule, PortfolioSnapshotService] |
||||
|
}) |
||||
|
export class PortfolioSnapshotQueueModule {} |
@ -0,0 +1,42 @@ |
|||||
|
import { |
||||
|
PORTFOLIO_PROCESS_JOB_NAME, |
||||
|
PORTFOLIO_SNAPSHOT_QUEUE |
||||
|
} from '@ghostfolio/common/config'; |
||||
|
|
||||
|
import { Process, Processor } from '@nestjs/bull'; |
||||
|
import { Injectable, Logger } from '@nestjs/common'; |
||||
|
import { Job } from 'bull'; |
||||
|
import ms from 'ms'; |
||||
|
import { setTimeout } from 'timers/promises'; |
||||
|
|
||||
|
import { IPortfolioSnapshotQueueJob } from './interfaces/portfolio-snapshot-queue-job.interface'; |
||||
|
|
||||
|
@Injectable() |
||||
|
@Processor(PORTFOLIO_SNAPSHOT_QUEUE) |
||||
|
export class PortfolioSnapshotProcessor { |
||||
|
public constructor() {} |
||||
|
|
||||
|
@Process({ concurrency: 1, name: PORTFOLIO_PROCESS_JOB_NAME }) |
||||
|
public async calculatePortfolioSnapshot( |
||||
|
job: Job<IPortfolioSnapshotQueueJob> |
||||
|
) { |
||||
|
try { |
||||
|
Logger.log( |
||||
|
`Portfolio snapshot calculation of user ${job.data.userId} has been started`, |
||||
|
`PortfolioProcessor (${PORTFOLIO_PROCESS_JOB_NAME})` |
||||
|
); |
||||
|
|
||||
|
// TODO: Do something
|
||||
|
await setTimeout(ms('1 second')); |
||||
|
|
||||
|
Logger.log( |
||||
|
`Portfolio snapshot calculation of user ${job.data.userId} has been completed`, |
||||
|
`PortfolioProcessor (${PORTFOLIO_PROCESS_JOB_NAME})` |
||||
|
); |
||||
|
} catch (error) { |
||||
|
Logger.error(error, `PortfolioProcessor (${PORTFOLIO_PROCESS_JOB_NAME})`); |
||||
|
|
||||
|
throw new Error(error); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,33 @@ |
|||||
|
import { PORTFOLIO_SNAPSHOT_QUEUE } from '@ghostfolio/common/config'; |
||||
|
|
||||
|
import { InjectQueue } from '@nestjs/bull'; |
||||
|
import { Injectable } from '@nestjs/common'; |
||||
|
import { JobOptions, Queue } from 'bull'; |
||||
|
|
||||
|
import { IPortfolioSnapshotQueueJob } from './interfaces/portfolio-snapshot-queue-job.interface'; |
||||
|
|
||||
|
@Injectable() |
||||
|
export class PortfolioSnapshotService { |
||||
|
public constructor( |
||||
|
@InjectQueue(PORTFOLIO_SNAPSHOT_QUEUE) |
||||
|
private readonly portfolioSnapshotQueue: Queue |
||||
|
) {} |
||||
|
|
||||
|
public async addJobToQueue({ |
||||
|
data, |
||||
|
name, |
||||
|
opts |
||||
|
}: { |
||||
|
data: IPortfolioSnapshotQueueJob; |
||||
|
name: string; |
||||
|
opts?: JobOptions; |
||||
|
}) { |
||||
|
return this.portfolioSnapshotQueue.add(name, data, opts); |
||||
|
} |
||||
|
|
||||
|
// public async addJobsToQueue(
|
||||
|
// jobs: { data: IPortfolioSnapshotQueueJob; name: string; opts?: JobOptions }[]
|
||||
|
// ) {
|
||||
|
// return this.portfolioSnapshotQueue.addBulk(jobs);
|
||||
|
// }
|
||||
|
} |
Loading…
Reference in new issue