@@ -37,13 +37,8 @@ exports.NativeRealm = class NativeRealm {
3737 this . _log = log ;
3838 }
3939
40- async setPassword ( username , password = this . _elasticPassword , { attempt = 1 } = { } ) {
41- await this . _autoRetry ( async ( ) => {
42- this . _log . info (
43- ( attempt > 1 ? `attempt ${ attempt } : ` : '' ) +
44- `setting ${ chalk . bold ( username ) } password to ${ chalk . bold ( password ) } `
45- ) ;
46-
40+ async setPassword ( username , password = this . _elasticPassword , retryOpts = { } ) {
41+ await this . _autoRetry ( retryOpts , async ( ) => {
4742 try {
4843 await this . _client . security . changePassword ( {
4944 username,
@@ -83,8 +78,8 @@ exports.NativeRealm = class NativeRealm {
8378 ) ;
8479 }
8580
86- async getReservedUsers ( ) {
87- return await this . _autoRetry ( async ( ) => {
81+ async getReservedUsers ( retryOpts = { } ) {
82+ return await this . _autoRetry ( retryOpts , async ( ) => {
8883 const resp = await this . _client . security . getUser ( ) ;
8984 const usernames = Object . keys ( resp . body ) . filter (
9085 ( user ) => resp . body [ user ] . metadata . _reserved === true
@@ -98,9 +93,9 @@ exports.NativeRealm = class NativeRealm {
9893 } ) ;
9994 }
10095
101- async isSecurityEnabled ( ) {
96+ async isSecurityEnabled ( retryOpts = { } ) {
10297 try {
103- return await this . _autoRetry ( async ( ) => {
98+ return await this . _autoRetry ( retryOpts , async ( ) => {
10499 const {
105100 body : { features } ,
106101 } = await this . _client . xpack . info ( { categories : 'features' } ) ;
@@ -115,18 +110,25 @@ exports.NativeRealm = class NativeRealm {
115110 }
116111 }
117112
118- async _autoRetry ( fn , attempt = 1 ) {
113+ async _autoRetry ( opts , fn ) {
114+ const { attempt = 1 , maxAttempts = 3 } = opts ;
115+
119116 try {
120117 return await fn ( attempt ) ;
121118 } catch ( error ) {
122- if ( attempt >= 3 ) {
119+ if ( attempt >= maxAttempts ) {
123120 throw error ;
124121 }
125122
126123 const sec = 1.5 * attempt ;
127124 this . _log . warning ( `assuming ES isn't initialized completely, trying again in ${ sec } seconds` ) ;
128125 await new Promise ( ( resolve ) => setTimeout ( resolve , sec * 1000 ) ) ;
129- return await this . _autoRetry ( fn , attempt + 1 ) ;
126+
127+ const nextOpts = {
128+ ...opts ,
129+ attempt : attempt + 1 ,
130+ } ;
131+ return await this . _autoRetry ( nextOpts , fn ) ;
130132 }
131133 }
132134} ;
0 commit comments