Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

using Promise.all instead of for/await in checkLeftEvents

Blue Mousec1415c47c48f
diff --git a/src/task-manager/task-manager.module.ts b/src/task-manager/task-manager.module.ts
index f1cda7173..8f1785a8a 100644
--- a/src/task-manager/task-manager.module.ts
+++ b/src/task-manager/task-manager.module.ts
@@ -7,6 +7,7 @@ import { UserModule } from '../user/user.module'
import { ProjectModule } from '../project/project.module'
import { AnalyticsModule } from '../analytics/analytics.module'
import { ActionTokensModule } from '../action-tokens/action-tokens.module'
+import { AppLoggerModule } from '../logger/logger.module'
@Module({
imports: [
@@ -17,6 +18,7 @@ import { ActionTokensModule } from '../action-tokens/action-tokens.module'
AlertModule,
forwardRef(() => AnalyticsModule),
ExtensionsModule,
+ AppLoggerModule,
],
providers: [TaskManagerService],
exports: [TaskManagerService],
diff --git a/src/task-manager/task-manager.service.spec.ts b/src/task-manager/task-manager.service.spec.ts
index 7cf25bd82..4f4834093 100644
--- a/src/task-manager/task-manager.service.spec.ts
+++ b/src/task-manager/task-manager.service.spec.ts
@@ -10,12 +10,10 @@ import { Project } from 'src/project/entity/project.entity'
import { ProjectService } from 'src/project/project.service'
import { User } from 'src/user/entities/user.entity'
import { UserService } from 'src/user/user.service'
-import { Util } from 'src/Util/Util'
import { TaskManagerService } from './task-manager.service'
describe('TaskManagerService', () => {
let service: TaskManagerService
- const util = new Util()
const USER_REPOSITORY_TOKEN = getRepositoryToken(User)
const ACRIONTOKENS_REPOSITORY_TOKEN = getRepositoryToken(ActionToken)
const PROJECT_REPOSITORY_TOKEN = getRepositoryToken(Project)
@@ -77,10 +75,6 @@ describe('TaskManagerService', () => {
expect(service.monthlyReportsHandler()).rejects.not.toBeNull()
expect(service.monthlyReportsHandler).toBeDefined()
})
- it("should be defined saveLogData and don't return null", () => {
- expect(service.saveCapthcaLogData()).not.toBeNull()
- expect(service.saveCapthcaLogData).toBeDefined()
- })
it("should be defined weeklyReportsHandler and don't return null", () => {
expect(service.weeklyReportsHandler()).rejects.not.toBeNull()
expect(service.weeklyReportsHandler).toBeDefined()
diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.ts
index f0b24d9fd..1ad9bef4f 100644
--- a/src/task-manager/task-manager.service.ts
+++ b/src/task-manager/task-manager.service.ts
@@ -50,6 +50,7 @@ import {
REDIS_LOG_CAPTCHA_CACHE_KEY,
} from '../common/constants'
import { getRandomTip } from '../common/utils'
+import { AppLoggerService } from '../logger/logger.service'
dayjs.extend(utc)
@@ -92,6 +93,7 @@ export class TaskManagerService {
private readonly alertService: AlertService,
@InjectBot() private bot: Telegraf<TelegrafContext>,
private readonly extensionsService: ExtensionsService,
+ private readonly logger: AppLoggerService,
) {}
@Cron(CronExpression.EVERY_MINUTE)
@@ -173,11 +175,11 @@ export class TaskManagerService {
url: 'https://swetrix.com/billing',
}
- for (let i = 0; i < _size(users); ++i) {
- const { id, email, planCode, projects } = users[i]
+ const promises = _map(users, async (user) => {
+ const { id, email, planCode, projects } = user
if (_isEmpty(projects) || _isNull(projects)) {
- continue
+ return
}
const maxEventsCount = ACCOUNT_PLANS[planCode].monthlyUsageLimit || 0
@@ -196,7 +198,13 @@ export class TaskManagerService {
evWarningSentOn: dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),
})
}
- }
+ })
+
+ await Promise
+ .all(promises)
+ .catch(reason => {
+ this.logger.error(`[CRON WORKER](checkLeftEvents) Error occured: ${reason}`)
+ })
}
@Cron(CronExpression.EVERY_2_HOURS)
@@ -780,64 +788,64 @@ export class TaskManagerService {
}
}
- // @Cron('0 * * * *')
- // async handleNewExtensions() {
- // const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000)
- // const extensions = await this.extensionsService.find({
- // where: {
- // createdAt: MoreThan(twoWeeksAgo),
- // },
- // })
- // for (const extension of extensions) {
- // if (!extension.tags.includes('New')) {
- // extension.tags.push('New')
- // await this.extensionsService.save(extension)
- // }
- // }
- // const oldExtensions = await this.extensionsService.find({
- // where: {
- // createdAt: LessThan(twoWeeksAgo),
- // tags: Like('%New%'),
- // },
- // })
- // for (const extension of oldExtensions) {
- // extension.tags = extension.tags.filter(tag => tag !== 'New')
- // await this.extensionsService.save(extension)
- // }
- // }
-
- // @Cron('0 * * * *')
- // async handleTrendingExtensions() {
- // const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000)
- // const extensions = await this.extensionsService.find({
- // where: {
- // createdAt: MoreThan(twoWeeksAgo),
- // },
- // })
- // for (const extension of extensions) {
- // const currentInstalls =
- // await this.extensionsService.getExtensionInstallCount(extension.id)
- // const twoWeeksBeforeInstalls =
- // await this.extensionsService.getExtensionInstallCount(
- // extension.id,
- // twoWeeksAgo,
- // )
- // if (
- // currentInstalls > twoWeeksBeforeInstalls * 2 &&
- // currentInstalls > 0.9 * (await this.getAverageInstalls(extensions))
- // ) {
- // if (!extension.tags.includes('Trending')) {
- // extension.tags.push('Trending')
- // await this.extensionsService.save(extension)
- // }
- // } else {
- // if (extension.tags.includes('Trending')) {
- // extension.tags = extension.tags.filter(tag => tag !== 'Trending')
- // await this.extensionsService.save(extension)
- // }
- // }
- // }
- // }
+ @Cron('0 * * * *')
+ async handleNewExtensions() {
+ const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000)
+ const extensions = await this.extensionsService.find({
+ where: {
+ createdAt: MoreThan(twoWeeksAgo),
+ },
+ })
+ for (const extension of extensions) {
+ if (!extension.tags.includes('New')) {
+ extension.tags.push('New')
+ await this.extensionsService.save(extension)
+ }
+ }
+ const oldExtensions = await this.extensionsService.find({
+ where: {
+ createdAt: LessThan(twoWeeksAgo),
+ tags: Like('%New%'),
+ },
+ })
+ for (const extension of oldExtensions) {
+ extension.tags = extension.tags.filter(tag => tag !== 'New')
+ await this.extensionsService.save(extension)
+ }
+ }
+
+ @Cron('0 * * * *')
+ async handleTrendingExtensions() {
+ const twoWeeksAgo = new Date(Date.now() - 14 * 24 * 60 * 60 * 1000)
+ const extensions = await this.extensionsService.find({
+ where: {
+ createdAt: MoreThan(twoWeeksAgo),
+ },
+ })
+ for (const extension of extensions) {
+ const currentInstalls =
+ await this.extensionsService.getExtensionInstallCount(extension.id)
+ const twoWeeksBeforeInstalls =
+ await this.extensionsService.getExtensionInstallCount(
+ extension.id,
+ twoWeeksAgo,
+ )
+ if (
+ currentInstalls > twoWeeksBeforeInstalls * 2 &&
+ currentInstalls > 0.9 * (await this.getAverageInstalls(extensions))
+ ) {
+ if (!extension.tags.includes('Trending')) {
+ extension.tags.push('Trending')
+ await this.extensionsService.save(extension)
+ }
+ } else {
+ if (extension.tags.includes('Trending')) {
+ extension.tags = extension.tags.filter(tag => tag !== 'Trending')
+ await this.extensionsService.save(extension)
+ }
+ }
+ }
+ }
private async getAverageInstalls(
extensions: Extension[],