Skip to content

Instantly share code, notes, and snippets.

@dangvanduc90
Last active January 24, 2019 12:40
Show Gist options
  • Save dangvanduc90/4c716cce70aa716517dd83bcc96c4b74 to your computer and use it in GitHub Desktop.
Save dangvanduc90/4c716cce70aa716517dd83bcc96c4b74 to your computer and use it in GitHub Desktop.
let app = require('express')();
let http = require('http').Server(app);
import redis from 'redis';
import bodyParser from 'body-parser';
let port = process.env.PORT || 3001;
let client = redis.createClient({
host: DB_HOST_REDIS, // các bạn thay thông số redis database ở đây
port: DB_PORT_REDIS, // các bạn thay thông số redis database ở đây
db: DB_NAME_REDIS // các bạn thay thông số redis database ở đây
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(port, function(){
console.log('Listening on http://localhost:' + port);
});
client.on("error", function (err) {
console.log("Error " + err);
});
client.on("connect", function () {
doWork()
});
// function này chạy liên tục
function doWork() {
client.keys('email_ticket_redis:*', function(err, result){ // với email_ticket_redis là key bên PHP insert vào redis
if (err) return sendLog(createLog(err))
if (result.length > 0) {
// luôn luôn phải có try catch để nếu có lỗi thì cũng ko stop vòng lặp của chúng ta
try {
} catch (err) {
sendLog(createLog(err))
}
setImmediate(() => {
doWork();
});
} else {
setTimeout(() => {
doWork();
},1000)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment