@koalarx/nest 1.14.3 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,13 +2,15 @@ import { ILoggingService } from '../../../services/logging/ilogging.service';
2
2
  import { IRedLockService } from '../../../services/redlock/ired-lock.service';
3
3
  import { RequestResult } from '../../request-overflow/request-result';
4
4
  export type CronJobResponse = RequestResult<Error, null>;
5
+ export interface CronJobSettings {
6
+ isActive: boolean;
7
+ timeInMinutes: number;
8
+ }
5
9
  export declare abstract class CronJobHandlerBase {
6
10
  private readonly redlockService;
7
11
  private readonly loggingService;
8
- private readonly _timeout;
9
12
  constructor(redlockService: IRedLockService, loggingService: ILoggingService);
10
13
  protected abstract run(): Promise<CronJobResponse>;
11
- protected abstract isActive(): Promise<boolean>;
12
- protected abstract defineTimeInMinutes(): number;
14
+ protected abstract settings(): Promise<CronJobSettings>;
13
15
  start(): Promise<void>;
14
16
  }
@@ -6,17 +6,17 @@ const koala_global_vars_1 = require("../../koala-global-vars");
6
6
  class CronJobHandlerBase {
7
7
  redlockService;
8
8
  loggingService;
9
- _timeout;
10
9
  constructor(redlockService, loggingService) {
11
10
  this.redlockService = redlockService;
12
11
  this.loggingService = loggingService;
13
- this._timeout = this.defineTimeInMinutes() * 60 * 1000;
14
12
  }
15
13
  async start() {
16
14
  const name = this.constructor.name;
17
15
  while (true) {
18
- if (await this.isActive()) {
19
- const ttlSecondsLock = this._timeout / 1000;
16
+ const settings = await this.settings();
17
+ const timeout = settings.timeInMinutes * 60 * 1000;
18
+ if (settings.isActive) {
19
+ const ttlSecondsLock = timeout / 1000;
20
20
  const acquiredLock = await this.redlockService.acquiredLock(name, ttlSecondsLock);
21
21
  if (acquiredLock) {
22
22
  const error = await this.run()
@@ -41,7 +41,7 @@ class CronJobHandlerBase {
41
41
  }
42
42
  }
43
43
  }
44
- await (0, KlDelay_1.delay)(this._timeout);
44
+ await (0, KlDelay_1.delay)(timeout);
45
45
  await this.redlockService.releaseLock(name);
46
46
  }
47
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "1.14.3",
3
+ "version": "1.15.0",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,7 +14,7 @@
14
14
  "homepage": "https://github.com/igordrangel/koala-nest#readme",
15
15
  "types": "./koala-nest.d.ts",
16
16
  "peerDependencies": {
17
- "@koalarx/utils": "^4.2.0",
17
+ "@koalarx/utils": "^4.2.1",
18
18
  "@nestjs/common": "^11.0.12",
19
19
  "@nestjs/config": "^4.0.1",
20
20
  "@nestjs/core": "^11.0.12",
@@ -1,4 +1,5 @@
1
1
  export declare abstract class IRedisService {
2
+ abstract isConnected: boolean;
2
3
  abstract getCache(key: string): Promise<string | null>;
3
4
  abstract setCache(key: string, ttlSecondsCache: number, payload: any): Promise<void>;
4
5
  abstract deleteCache(key: string): Promise<void>;
@@ -4,6 +4,7 @@ import { IRedisService } from './iredis.service';
4
4
  export declare class RedisService implements IRedisService, OnModuleDestroy {
5
5
  private readonly redisClient;
6
6
  private readonly environment;
7
+ get isConnected(): boolean;
7
8
  constructor(env: EnvService);
8
9
  onModuleDestroy(): void;
9
10
  getCache(key: string): Promise<string | null>;
@@ -17,6 +17,9 @@ const env_service_1 = require("../../env/env.service");
17
17
  let RedisService = class RedisService {
18
18
  redisClient;
19
19
  environment;
20
+ get isConnected() {
21
+ return !!this.redisClient;
22
+ }
20
23
  constructor(env) {
21
24
  if (!env_config_1.EnvConfig.isEnvTest) {
22
25
  const redisUrl = env.get('REDIS_CONNECTION_STRING');
@@ -19,7 +19,7 @@ let RedLockService = class RedLockService {
19
19
  this.redisService = redisService;
20
20
  }
21
21
  async acquiredLock(key, ttlSecondsLock) {
22
- if (env_config_1.EnvConfig.isEnvTest) {
22
+ if (env_config_1.EnvConfig.isEnvTest || !this.redisService.isConnected) {
23
23
  return true;
24
24
  }
25
25
  const lockKey = this.getLockKey(key);
@@ -31,7 +31,7 @@ let RedLockService = class RedLockService {
31
31
  return true;
32
32
  }
33
33
  async releaseLock(key) {
34
- if (!env_config_1.EnvConfig.isEnvTest) {
34
+ if (!env_config_1.EnvConfig.isEnvTest && this.redisService.isConnected) {
35
35
  await this.redisService.deleteCache(this.getLockKey(key));
36
36
  }
37
37
  }