adds auto discovering new function versions to FunctionLogs action when tailing

This commit is contained in:
Egor Kislitsyn 2016-02-04 13:12:42 +07:00
parent d4bf6c9ad2
commit ec40e36e65

View File

@ -143,19 +143,16 @@ module.exports = function(SPlugin, serverlessPath) {
const lambdaName = this.Lambda.sGetLambdaName(func.getProject().name, func.getComponent().name, func.getModule().name, func.name);
this.evt.data.logGroupName = '/aws/lambda/' + lambdaName;
// get functon version
let params = {
FunctionName: lambdaName,
Name: this.evt.options.stage
};
return this.Lambda.getAliasPromised(params)
.then(reply => this.evt.data.version = reply.FunctionVersion);
this.evt.data.lambdaName = lambdaName;
}
_getLogStreamNames() {
return this.CWL.sGetLogStreams(this.evt.data.logGroupName, 50)
return this.Lambda.getAliasPromised({
FunctionName: this.evt.data.lambdaName,
Name: this.evt.options.stage
})
.then(reply => this.evt.data.version = reply.FunctionVersion)
.then(() => this.CWL.sGetLogStreams(this.evt.data.logGroupName, 50))
.then(reply => reply.logStreams)
.then( logStreams => {
if (logStreams.length === 0) return BbPromise.reject(new SError('No existing streams for the function'));
@ -188,6 +185,14 @@ module.exports = function(SPlugin, serverlessPath) {
return this._getLogStreamNames()
.then( logStreamNames => {
if (!logStreamNames.length) {
if (this.evt.options.tail) {
return setTimeout((()=> this._showLogs()), this.evt.options.pollInterval);
} else {
return BbPromise.reject(new SError('No existing streams for the function'));
}
}
let params = {
logGroupName: this.evt.data.logGroupName,
interleaved: true,
@ -198,33 +203,33 @@ module.exports = function(SPlugin, serverlessPath) {
if (this.evt.options.filter) params.filterPattern = this.evt.options.filter;
if (this.evt.data.nextToken) params.nextToken = this.evt.data.nextToken;
return this.CWL.filterLogEventsAsync(params);
})
.then(results => {
return this.CWL.filterLogEventsAsync(params)
.then(results => {
if (this.S.config.interactive) {
results.events.forEach(e => {
process.stdout.write(this._formatLogEvent(e));
if (this.S.config.interactive && results.events) {
results.events.forEach(e => {
process.stdout.write(this._formatLogEvent(e));
});
}
if (results.nextToken) {
this.evt.data.nextToken = results.nextToken;
} else {
delete this.evt.data.nextToken;
}
if (this.evt.options.tail) {
if (results.events && results.events.length) {
this.evt.data.startTime = _.last(results.events).timestamp + 1;
}
return setTimeout((()=> this._showLogs()), this.evt.options.pollInterval);
}
else {
return this.evt.data.results = results.events;
}
});
}
if (results.nextToken) {
this.evt.data.nextToken = results.nextToken;
} else {
delete this.evt.data.nextToken;
}
if (this.evt.options.tail) {
if (results.events.length) {
this.evt.data.startTime = _.last(results.events).timestamp + 1;
}
return setTimeout((()=> this._showLogs()), this.evt.options.pollInterval);
}
else {
return this.evt.data.results = results.events;
}
})
});
}
}