@@ -5,7 +5,7 @@ import * as common from '../utils/common'
5
5
import constConfig from '../constants' ;
6
6
import { getRedisClient } from '../utils/redis'
7
7
import { AppError } from '../app-error'
8
- import * as config from '../config'
8
+ import config from '../config'
9
9
import log4js from 'log4js'
10
10
import Sequelize from 'sequelize'
11
11
@@ -37,35 +37,28 @@ export const clearUpdateCheckCache = async function (deploymentKey: string, appV
37
37
}
38
38
39
39
export const updateCheckFromCache = async function ( deploymentKey : string , appVersion : string , label : string , packageHash : string , clientUniqueId : number ) {
40
- var updateCheckCache = _ . get ( config , ' common.updateCheckCache' , false ) ;
41
- if ( updateCheckCache === false ) {
40
+ const updateCheckCache = config . common . updateCheckCache
41
+ if ( ! updateCheckCache ) {
42
42
return updateCheck ( deploymentKey , appVersion , label , packageHash ) ;
43
43
}
44
- let redisCacheKey = getUpdateCheckCacheKey ( deploymentKey , appVersion , label , packageHash ) ;
44
+ const redisCacheKey = getUpdateCheckCacheKey ( deploymentKey , appVersion , label , packageHash ) ;
45
45
const client = await getRedisClient ( )
46
+ try {
47
+ const data = await client . get ( redisCacheKey )
48
+ log . debug ( 'updateCheckFromCache read from cache' ) ;
49
+ if ( data ) {
50
+ const obj = JSON . parse ( data ) ;
51
+ return obj as UpdateCheckResponse
52
+ }
46
53
47
- return client . get ( redisCacheKey )
48
- . then ( ( data ) => {
49
- if ( data ) {
50
- try {
51
- log . debug ( 'updateCheckFromCache read from catch' ) ;
52
- var obj = JSON . parse ( data ) ;
53
- return obj ;
54
- } catch ( e ) {
55
- }
56
- }
57
- return updateCheck ( deploymentKey , appVersion , label , packageHash , clientUniqueId )
58
- . then ( ( rs ) => {
59
- try {
60
- log . debug ( 'updateCheckFromCache read from db' ) ;
61
- var strRs = JSON . stringify ( rs ) ;
62
- client . setEx ( redisCacheKey , EXPIRED , strRs ) ;
63
- } catch ( e ) {
64
- }
65
- return rs ;
66
- } ) ;
67
- } )
68
- . finally ( ( ) => client . quit ( ) ) ;
54
+ const rs = await updateCheck ( deploymentKey , appVersion , label , packageHash , clientUniqueId )
55
+ log . debug ( 'updateCheckFromCache read from db' ) ;
56
+ const strRs = JSON . stringify ( rs ) ;
57
+ client . setEx ( redisCacheKey , EXPIRED , strRs ) ;
58
+ return rs
59
+ } finally {
60
+ client . quit ( )
61
+ }
69
62
}
70
63
71
64
export const getChosenManCacheKey = function ( packageId : number , rollout : number , clientUniqueId : number ) {
@@ -85,41 +78,57 @@ export const chosenMan = async function (packageId: number, rollout: number, cli
85
78
if ( rollout >= 100 ) {
86
79
return Promise . resolve ( true ) ;
87
80
}
88
- var rolloutClientUniqueIdCache = _ . get ( config , ' common.rolloutClientUniqueIdCache' , false ) ;
89
- if ( rolloutClientUniqueIdCache === false ) {
81
+ const rolloutClientUniqueIdCache = config . common . rolloutClientUniqueIdCache
82
+ if ( ! rolloutClientUniqueIdCache ) {
90
83
return random ( rollout ) ;
91
84
} else {
92
85
const client = await getRedisClient ( )
93
86
const redisCacheKey = getChosenManCacheKey ( packageId , rollout , clientUniqueId ) ;
94
- return client . get ( redisCacheKey )
95
- . then ( ( data ) => {
96
- if ( Number ( data ) == 1 ) {
97
- return true ;
98
- } else if ( Number ( data ) == 2 ) {
99
- return false ;
100
- } else {
101
- return random ( rollout )
102
- . then ( ( r ) => {
103
- return client . setEx ( redisCacheKey as any , 60 * 60 * 24 * 7 , r ? '1' : '2' )
104
- . then ( ( ) => {
105
- return r ;
106
- } ) ;
107
- } ) ;
108
- }
109
- } )
110
- . finally ( ( ) => client . quit ( ) ) ;
87
+
88
+ try {
89
+ const data = await client . get ( redisCacheKey )
90
+
91
+ if ( Number ( data ) == 1 ) {
92
+ return true ;
93
+ } else if ( Number ( data ) == 2 ) {
94
+ return false ;
95
+ } else {
96
+ const r = await random ( rollout )
97
+ await client . setEx ( redisCacheKey , 60 * 60 * 24 * 7 , r ? '1' : '2' )
98
+ return r ;
99
+ }
100
+
101
+ } catch ( error ) {
102
+ client . quit ( )
103
+ }
104
+ return
111
105
}
112
106
}
113
107
114
- export const updateCheck = function ( deploymentKey : string , appVersion : string , label : string , packageHash : string , clientUniqueId ?: number ) {
108
+ type UpdateCheckResponse = {
109
+ packageId : number ;
110
+ downloadURL : string ;
111
+ downloadUrl : string ;
112
+ description : string ;
113
+ isAvailable : boolean ;
114
+ isMandatory : boolean ;
115
+ appVersion : string ;
116
+ packageHash : string ;
117
+ label : string ;
118
+ packageSize : number ;
119
+ updateAppVersion : boolean ;
120
+ shouldRunBinaryVersion : boolean ;
121
+ rollout : number ;
122
+ }
123
+ export const updateCheck = ( deploymentKey : string , appVersion : string , label : string , packageHash : string , clientUniqueId ?: number ) : Promise < UpdateCheckResponse > => {
115
124
var rs = {
116
125
packageId : 0 ,
117
126
downloadURL : "" ,
118
127
downloadUrl : "" ,
119
128
description : "" ,
120
129
isAvailable : false ,
121
130
isMandatory : false ,
122
- appVersion : appVersion ,
131
+ appVersion,
123
132
packageHash : "" ,
124
133
label : "" ,
125
134
packageSize : 0 ,
@@ -133,13 +142,14 @@ export const updateCheck = function (deploymentKey: string, appVersion: string,
133
142
}
134
143
return models . Deployments . findOne ( { where : { deployment_key : deploymentKey } } )
135
144
. then ( ( dep ) => {
136
- if ( _ . isEmpty ( dep ) ) {
145
+ if ( ! dep ) {
137
146
throw new AppError ( 'Not found deployment, check deployment key is right.' ) ;
138
147
}
139
- var version = common . parseVersion ( appVersion ) ;
148
+ const version = common . parseVersion ( appVersion ) ;
149
+ console . log ( '🚀 ~ file: client-manager.ts ~ line 149 ~ .then ~ version' , version )
140
150
return models . DeploymentsVersions . findAll ( {
141
151
where : {
142
- deployment_id : dep ? .id ,
152
+ deployment_id : dep . id ,
143
153
min_version : { [ Sequelize . Op . lte ] : version } ,
144
154
max_version : { [ Sequelize . Op . gt ] : version }
145
155
}
0 commit comments