@@ -3,7 +3,11 @@ import { uniq } from 'lodash';
3
3
import { ToadScheduler , SimpleIntervalJob , AsyncTask } from 'toad-scheduler' ;
4
4
import { inject , injectable , multiInject } from 'inversify' ;
5
5
import { 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' ;
7
11
import { ConfigurationError } from '../utils/errors' ;
8
12
import { ICacheLayerLoader } from './cacheLayerLoader' ;
9
13
import { getLogger } from '../utils' ;
@@ -49,15 +53,13 @@ export class CacheLayerRefresher implements ICacheLayerRefresher {
49
53
// check if the index name is duplicated more than one API schemas
50
54
this . checkDuplicateIndex ( schemas ) ;
51
55
// traverse each cache table of each schema
52
- const activityLogger = this . getActivityLogger ( ) ;
53
56
await Promise . all (
54
57
schemas . map ( async ( schema ) => {
55
58
// skip the schema by return if not set the cache
56
59
if ( ! schema . cache ) return ;
57
- const { urlPath } = schema ;
58
60
return await Promise . all (
59
61
schema . cache . map ( async ( cache ) => {
60
- const { cacheTableName, profile, refreshTime, sql } = cache ;
62
+ const { cacheTableName, profile, refreshTime } = cache ;
61
63
// replace the '/' tp '_' to avoid the file path issue for templateSource
62
64
const templateName = schema . templateSource . replace ( '/' , '_' ) ;
63
65
// If refresh time is set, use the scheduler to schedule the load task for refresh
@@ -68,59 +70,14 @@ export class CacheLayerRefresher implements ICacheLayerRefresher {
68
70
const refreshJob = new SimpleIntervalJob (
69
71
{ milliseconds, runImmediately } ,
70
72
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 ) ;
95
74
} ) ,
96
75
{ preventOverrun : true , id : workerId }
97
76
) ;
98
77
// add the job to schedule cache refresh task
99
78
this . scheduler . addIntervalJob ( refreshJob ) ;
100
79
} 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 ) ;
124
81
}
125
82
} )
126
83
) ;
@@ -135,12 +92,42 @@ export class CacheLayerRefresher implements ICacheLayerRefresher {
135
92
this . scheduler . stop ( ) ;
136
93
}
137
94
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
+ }
142
128
143
- return activityLogger ;
129
+ private getActivityLoggers ( ) : IActivityLogger [ ] {
130
+ return this . activityLoggers . filter ( ( logger ) => logger . isEnabled ( ) ) ;
144
131
}
145
132
146
133
private checkDuplicateCacheTableName ( schemas : APISchema [ ] ) {
0 commit comments