admin管理员组文章数量:1289529
I was experiencing difficulties when invoking same lambda function continuously by using setInterval function.
lambda function
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
function funUpdateCommand(event,context,callback){
var mongoUrl='mongodb://**.**.**.**:*****/DBname';
// var mongoUrl='mongodb://127.0.0.1:27017/DBname';
MongoClient.connect(mongoUrl, function(err, db) {
if(err) throw err;
var collection = db.collection('device');
var interval = setInterval(function() {
collection.find({"deviceCommandmand":"getAudio","deviceCommand.timestamp":{ $lte: new Date((new Date)*1 - 60000*2)}}).toArray(function(err, results) {
if(err){
console.log(err);
}else{
for(var i=0;i<results.length;i++){
collection.update({_id:results[i]._id},{$set:{"deviceCommandmand":" "}},function(err, results) {
});
}
}
});
}, 5000);
});
context.succeed("Successfully uploaded");
}
exports.handler=funUpdateCommand;
I was experiencing difficulties when invoking same lambda function continuously by using setInterval function.
lambda function
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
function funUpdateCommand(event,context,callback){
var mongoUrl='mongodb://**.**.**.**:*****/DBname';
// var mongoUrl='mongodb://127.0.0.1:27017/DBname';
MongoClient.connect(mongoUrl, function(err, db) {
if(err) throw err;
var collection = db.collection('device');
var interval = setInterval(function() {
collection.find({"deviceCommand.mand":"getAudio","deviceCommand.timestamp":{ $lte: new Date((new Date)*1 - 60000*2)}}).toArray(function(err, results) {
if(err){
console.log(err);
}else{
for(var i=0;i<results.length;i++){
collection.update({_id:results[i]._id},{$set:{"deviceCommand.mand":" "}},function(err, results) {
});
}
}
});
}, 5000);
});
context.succeed("Successfully uploaded");
}
exports.handler=funUpdateCommand;
I am trying to update some of the documents in my mongoDB,i need to run the aws lambda function as a continuous background job ,but when using setInterval it returns timeout error .
How can i continuously run my aws lambda function using setInterval?
Share Improve this question edited Nov 14, 2016 at 16:54 Mark B 201k27 gold badges330 silver badges326 bronze badges asked Nov 14, 2016 at 13:44 SabreenaSabreena 6492 gold badges13 silver badges25 bronze badges3 Answers
Reset to default 5You can not keep lambda running forever. Lambda functions life time is limited with 300 seconds, after 300 seconds, your function dies. You can invoke same lambda function with cron expressions using CloudWatch Events.
You can learn more about lambda limits from here.
Without ruling out other potential connection issues and timeouts, it's worth discussing the fact that Lambda functions are not meant to run for long periods of time. Amazon have design the platform to be called on demand, and not to run like a micro-service or stay alive for a long time.
You can read it from the horse's mouth here:
Q: How long can an AWS Lambda function execute?
All calls made to AWS Lambda must plete execution within 300 seconds. The default timeout is 3 seconds, but you can set the timeout to any value between 1 and 300 seconds.
You could schedule your function to run periodically, connect, do work, and disconnect.
@taskiner is correct. CloudWatch doesn't support triggers more frequent than once per minute and Lambda is run length is limited to 300 seconds. You do have a couple options though:
setTimeout:
You could however configure CloudWatch to trigger your lambda every minute and then you could, based on the time use setTimeout
to run your code at the next 30 second interval, repeat it once and then exit.
Off the top of my head:
var THRITY_SECONDS = 30000;
module.exports.handler = function(context, event, callback) {
scheduleNext(runTask, function(){
scheduleNext(runTask, callback);
});
};
function scheduleNext(task, callback) {
var now = new Date().getTime();
var nextInterval = Math.ceil((new Date().getTime() / THIRTY_SECONDS) * THRITY_SECONDS);
var delay - nextInterval - now;
setTimeout(function() {
task(callback);
, delay);
}
function runTask(callback) {
// your code executes here
callback();
}
Just be advised that if the function runs for 40 seconds (starts at 1:01:20 PM, executes runTask
at 1:01:30 and 1:02:00) you'll be charged for 30 seconds of pute time. You'll have to do the cost analysis yourself and pare it with something like the cost of running a t2.micro instance (spot instances could help reduce the cost here).
External cron: Another option would be if you had server resources elsewhere that were stable you could have them run a cron job which uses the AWS CLI to trigger your function on the schedule you define.
Of course if you're doing this, why not just run the task on said server?
Event trigger Lambdas run on event triggers and there are several supported services that will trigger your lambda when data bees available (s3 bucket/object changes, DynamoDB changes, etc). Depending on your use case (looks like you are querying mongodb for new data to process) maybe you can write an event to DynamoDB and connect your lambda to the table's stream, or if you're saving your audio files to s3 then configure your lambda to listen for new files. This is where you will see the cost benefits of using lambda because it will only run when there is work to do.
本文标签: javascriptHow to use setInterval in aws lambda functionStack Overflow
版权声明:本文标题:javascript - How to use setInterval in aws lambda function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741452454a2379563.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论