@@ -3,7 +3,11 @@ import { uniq } from 'lodash';
33import { ToadScheduler , SimpleIntervalJob , AsyncTask } from 'toad-scheduler' ;
44import { inject , injectable , multiInject } from 'inversify' ;
55import { TYPES } from '@vulcan-sql/core/types' ;
6- import { APISchema , IActivityLogger } from '@vulcan-sql/core/models' ;
6+ import {
7+ APISchema ,
8+ CacheLayerInfo ,
9+ IActivityLogger ,
10+ } from '@vulcan-sql/core/models' ;
711import { ConfigurationError } from '../utils/errors' ;
812import { ICacheLayerLoader } from './cacheLayerLoader' ;
913import { getLogger } from '../utils' ;
@@ -49,15 +53,13 @@ export class CacheLayerRefresher implements ICacheLayerRefresher {
4953 // check if the index name is duplicated more than one API schemas
5054 this . checkDuplicateIndex ( schemas ) ;
5155 // traverse each cache table of each schema
52- const activityLogger = this . getActivityLogger ( ) ;
5356 await Promise . all (
5457 schemas . map ( async ( schema ) => {
5558 // skip the schema by return if not set the cache
5659 if ( ! schema . cache ) return ;
57- const { urlPath } = schema ;
5860 return await Promise . all (
5961 schema . cache . map ( async ( cache ) => {
60- const { cacheTableName, profile, refreshTime, sql } = cache ;
62+ const { cacheTableName, profile, refreshTime } = cache ;
6163 // replace the '/' tp '_' to avoid the file path issue for templateSource
6264 const templateName = schema . templateSource . replace ( '/' , '_' ) ;
6365 // If refresh time is set, use the scheduler to schedule the load task for refresh
@@ -68,59 +70,14 @@ export class CacheLayerRefresher implements ICacheLayerRefresher {
6870 const refreshJob = new SimpleIntervalJob (
6971 { milliseconds, runImmediately } ,
7072 new AsyncTask ( workerId , async ( ) => {
71- // load data the to cache storage
72- let refreshResult = RefreshResult . SUCCESS ;
73- const now = moment . utc ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
74- try {
75- // get the current time in format of UTC
76- await this . cacheLoader . load ( templateName , cache ) ;
77- } catch ( error : any ) {
78- refreshResult = RefreshResult . FAILED ;
79- this . logger . debug ( `Failed to refresh cache: ${ error } ` ) ;
80- } finally {
81- // send activity log
82- const content = {
83- logTime : now ,
84- urlPath,
85- sql,
86- refreshResult,
87- } ;
88- if ( activityLogger )
89- activityLogger . log ( content ) . catch ( ( err : any ) => {
90- this . logger . debug (
91- `Failed to log activity after refreshing cache: ${ err } `
92- ) ;
93- } ) ;
94- }
73+ await this . sendActivityLogAfterLoad ( schema , cache ) ;
9574 } ) ,
9675 { preventOverrun : true , id : workerId }
9776 ) ;
9877 // add the job to schedule cache refresh task
9978 this . scheduler . addIntervalJob ( refreshJob ) ;
10079 } else {
101- let refreshResult = RefreshResult . SUCCESS ;
102- const now = moment . utc ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
103- try {
104- // get the current time in format of UTC
105- await this . cacheLoader . load ( templateName , cache ) ;
106- } catch ( error : any ) {
107- refreshResult = RefreshResult . FAILED ;
108- this . logger . debug ( `Failed to refresh cache: ${ error } ` ) ;
109- } finally {
110- // send activity log
111- const content = {
112- logTime : now ,
113- urlPath,
114- sql,
115- refreshResult,
116- } ;
117- if ( activityLogger )
118- activityLogger . log ( content ) . catch ( ( err : any ) => {
119- this . logger . debug (
120- `Failed to log activity after refreshing cache: ${ err } `
121- ) ;
122- } ) ;
123- }
80+ await this . sendActivityLogAfterLoad ( schema , cache ) ;
12481 }
12582 } )
12683 ) ;
@@ -135,12 +92,42 @@ export class CacheLayerRefresher implements ICacheLayerRefresher {
13592 this . scheduler . stop ( ) ;
13693 }
13794
138- private getActivityLogger ( ) : IActivityLogger | undefined {
139- const activityLogger = this . activityLoggers . find ( ( logger ) =>
140- logger . isEnabled ( )
141- ) ;
95+ private async sendActivityLogAfterLoad (
96+ schema : APISchema ,
97+ cache : CacheLayerInfo
98+ ) {
99+ const { urlPath } = schema ;
100+ const { sql } = cache ;
101+ // if fn is not a function, return
102+ let refreshResult = RefreshResult . SUCCESS ;
103+ const now = moment . utc ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
104+ const templateName = schema . templateSource . replace ( '/' , '_' ) ;
105+ try {
106+ // get the current time in format of UTC
107+ await this . cacheLoader . load ( templateName , cache ) ;
108+ } catch ( error : any ) {
109+ refreshResult = RefreshResult . FAILED ;
110+ this . logger . debug ( `Failed to refresh cache: ${ error } ` ) ;
111+ } finally {
112+ // send activity log
113+ const content = {
114+ logTime : now ,
115+ urlPath,
116+ sql,
117+ refreshResult,
118+ } ;
119+ const activityLoggers = this . getActivityLoggers ( ) ;
120+ for ( const activityLogger of activityLoggers )
121+ activityLogger . log ( content ) . catch ( ( err : any ) => {
122+ this . logger . debug (
123+ `Failed to log activity after refreshing cache: ${ err } `
124+ ) ;
125+ } ) ;
126+ }
127+ }
142128
143- return activityLogger ;
129+ private getActivityLoggers ( ) : IActivityLogger [ ] {
130+ return this . activityLoggers . filter ( ( logger ) => logger . isEnabled ( ) ) ;
144131 }
145132
146133 private checkDuplicateCacheTableName ( schemas : APISchema [ ] ) {
0 commit comments