Skip to content

Commit f50848d

Browse files
authored
Merge pull request #15 from haitd/master
Avoid issue `ResourceConflictException` (#1)
2 parents e881057 + 4e02ff1 commit f50848d

1 file changed

Lines changed: 65 additions & 47 deletions

File tree

simplify.js

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -490,21 +490,55 @@ const createOrUpdateFunction = function (options) {
490490
consoleWithMessage(`${opName}-UpdateFunctionConfig`, `${functionConfig.FunctionName.truncate(50)}`, err);
491491
const unusedProps = ["Code", "Publish", "Tags"]
492492
unusedProps.forEach(function (k) { delete params[k] })
493-
adaptor.updateFunctionConfiguration(params, function (err) {
493+
adaptor.updateFunctionConfiguration(params, function (err, data) {
494494
if (err) {
495495
reject(err)
496496
} else {
497-
consoleWithMessage(`${opName}-UpdateFunctionConfig`, `${CDONE}(OK)${CRESET}`);
498-
tryToUpdateFunctionCode(options, resolve, reject);
497+
adaptor.waitFor('functionUpdated', { FunctionName: data.FunctionArn }, function(err, data) {
498+
if (err) {
499+
reject(err);
500+
} else {
501+
consoleWithMessage(`${opName}-UpdateFunctionConfig`, `${CDONE}(OK)${CRESET}`);
502+
adaptor.updateFunctionCode({
503+
FunctionName: functionConfig.FunctionName,
504+
S3Bucket: bucketName,
505+
S3Key: bucketKey
506+
}, function (err, data) {
507+
if (err) {
508+
consoleWithMessage(`${opName}-UpdateFunctionCode`, `${CERROR}(ERROR)${CRESET} ${err}`);
509+
reject(err)
510+
} else {
511+
adaptor.waitFor('functionUpdated', { FunctionName: data.FunctionArn }, function(err, data) {
512+
if (err) {
513+
reject(err);
514+
} else {
515+
consoleWithMessage(`${opName}-UpdateFunctionCode`, `${CDONE}(OK)${CRESET}`);
516+
resolve(data)
517+
}
518+
});
519+
}
520+
});
521+
}
522+
});
499523
}
500524
});
501525
} else {
502526
var index = 0
503527
function retryCreateFunction() {
504528
consoleWithMessage(`${opName}-CreateFunction`, `${functionConfig.FunctionName.truncate(50)}`);
505529
adaptor.createFunction(params, function (err, data) {
506-
if (++index > creationTimeout || !err) {
507-
index > creationTimeout ? reject(`Create Function Timeout with (Error): ${err}`) : resolve({ ...data })
530+
if (++index > creationTimeout) {
531+
reject(`Create Function Timeout with (Error): ${err}`)
532+
} else if (!err) {
533+
resolve({ ...data });
534+
adaptor.waitFor('functionActive', { FunctionName: data.FunctionArn }, function(err, data) {
535+
if (err) {
536+
reject(err);
537+
} else {
538+
consoleWithMessage(`${opName}-CreateFunction`, `${CDONE}(OK)${CRESET}`);
539+
resolve(data)
540+
}
541+
});
508542
} else {
509543
setTimeout(() => retryCreateFunction(), 1000)
510544
}
@@ -513,8 +547,14 @@ const createOrUpdateFunction = function (options) {
513547
retryCreateFunction()
514548
}
515549
} else {
516-
consoleWithMessage(`${opName}-CreateFunction`, `${CDONE}(OK)${CRESET}`);
517-
resolve(data)
550+
adaptor.waitFor('functionActive', { FunctionName: data.FunctionArn }, function(err, data) {
551+
if (err) {
552+
reject(err);
553+
} else {
554+
consoleWithMessage(`${opName}-CreateFunction`, `${CDONE}(OK)${CRESET}`);
555+
resolve(data)
556+
}
557+
});
518558
}
519559
})
520560
})
@@ -526,15 +566,27 @@ const updateFunctionConfiguration = function (options) {
526566
return new Promise(function (resolve, reject) {
527567
const unusedProps = ["Code", "Publish", "Tags"]
528568
unusedProps.forEach(function (k) { delete functionConfig[k] })
529-
adaptor.updateFunctionConfiguration({ ...functionConfig }, function (err, data) {
569+
adaptor.waitFor('functionActive', { FunctionName: functionConfig.FunctionName }, function(err, data) {
530570
if (err) {
531-
consoleWithMessage(`${opName}-UpdateFunctionConfig`, `${CERROR}(ERROR)${CRESET} ${err}`);
532-
reject(err)
571+
reject(err);
533572
} else {
534-
consoleWithMessage(`${opName}-UpdateFunctionConfig`, `${CDONE}(OK)${CRESET}`);
535-
resolve(data)
573+
adaptor.updateFunctionConfiguration({ ...functionConfig }, function (err, data) {
574+
if (err) {
575+
consoleWithMessage(`${opName}-UpdateFunctionConfig`, `${CERROR}(ERROR)${CRESET} ${err}`);
576+
reject(err)
577+
} else {
578+
adaptor.waitFor('functionUpdated', { FunctionName: data.FunctionArn }, function(err, data) {
579+
if (err) {
580+
reject(err);
581+
} else {
582+
consoleWithMessage(`${opName}-UpdateFunctionConfig`, `${CDONE}(OK)${CRESET}`);
583+
resolve(data)
584+
}
585+
});
586+
}
587+
})
536588
}
537-
})
589+
});
538590
})
539591
}
540592

@@ -565,40 +617,6 @@ const publishFunctionVersion = function (options) {
565617
})
566618
}
567619

568-
const tryToUpdateFunctionCode = function(options, resolve, reject, retry = 3) {
569-
var { adaptor, opName, bucketName, bucketKey, functionConfig } = options;
570-
opName = opName || `createOrUpdateFunction`;
571-
let retryTime = 0;
572-
getFunctionConfiguration(options).then((result) => {
573-
if (result.State !== 'Pending') {
574-
adaptor.updateFunctionCode({
575-
FunctionName: functionConfig.FunctionName,
576-
S3Bucket: bucketName,
577-
S3Key: bucketKey
578-
}, function (err, data) {
579-
if (err) {
580-
consoleWithMessage(`${opName}`, `UpdateFunctionCode: ${CERROR}(ERROR)${CRESET} ${err}`);
581-
reject(err)
582-
} else {
583-
consoleWithMessage(`${opName}`, `UpdateFunctionCode: ${CDONE}(OK)${CRESET}`);
584-
resolve(data)
585-
}
586-
})
587-
} else {
588-
retryTime += 1;
589-
if (retryTime < retry) {
590-
setTimeout(() => tryToUpdateFunctionCode(options, resolve, reject), 3000);
591-
} else {
592-
consoleWithMessage(`${opName}`, `UpdateFunctionCode: ${CERROR}(ERROR TIMEOUT)${CRESET}`);
593-
reject(result);
594-
}
595-
}
596-
}).catch((err) => {
597-
consoleWithMessage(`${opName}`, `UpdateFunctionCode: ${CERROR}(ERROR)${CRESET}`);
598-
reject(err)
599-
})
600-
}
601-
602620
const createFunctionLayerVersion = function (options) {
603621
var { adaptor, opName, bucketName, bucketKey, functionConfig, layerConfig } = options
604622
opName = opName || `createFunctionLayerVersion`

0 commit comments

Comments
 (0)