Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140762

What can cause a timeout when running sequelize in Node.js?

$
0
0

In my Node.js (v10.15.0) application, I use the sequelize package (v4.44.3) to connect to remote MySQL database. This application works in a Docker container. After running the program for a long time, I started getting a timeout on my REST request. The problem is solved only by reloading the Docker container itself. Unfortunately, the problem is repeated again and again after a few days of the program. What could be causing this problem?

MySQL.js:

const Sequelize = require('sequelize');

const Op = Sequelize.Op;
const operatorsAliases = {
    $eq: Op.eq,
    $ne: Op.ne,
    $gte: Op.gte,
    $gt: Op.gt,
    $lte: Op.lte,
    $lt: Op.lt,
    $not: Op.not,
    $in: Op.in,
    $notIn: Op.notIn,
    $is: Op.is,
    $like: Op.like,
    $notLike: Op.notLike,
    $iLike: Op.iLike,
    $notILike: Op.notILike,
    $regexp: Op.regexp,
    $notRegexp: Op.notRegexp,
    $iRegexp: Op.iRegexp,
    $notIRegexp: Op.notIRegexp,
    $between: Op.between,
    $notBetween: Op.notBetween,
    $overlap: Op.overlap,
    $contains: Op.contains,
    $contained: Op.contained,
    $adjacent: Op.adjacent,
    $strictLeft: Op.strictLeft,
    $strictRight: Op.strictRight,
    $noExtendRight: Op.noExtendRight,
    $noExtendLeft: Op.noExtendLeft,
    $and: Op.and,
    $or: Op.or,
    $any: Op.any,
    $all: Op.all,
    $values: Op.values,
    $col: Op.col
};

const sequelize = new Sequelize(
    process.env.MySQL_DATABASE_NAME,
    process.env.MySQL_USER,
    process.env.MySQL_PASSWORD,
    {
        host: process.env.MySQL_HOST,
        port: process.env.MySQL_PORT,
        dialect: 'mysql',
        operatorsAliases: operatorsAliases,
        pool: {
            max: 15,
            min: 5,
            idle: 20000,
            evict: 15000,
            acquire: 30000
        }
    }
);

sequelize.authenticate().then(() => {
    console.log('Connection to remote MySQL database has been established successfully.');
}).catch(err => {
    console.error('Unable to connect to remote MySQL database:', err);
});

module.exports = sequelize;

app.js:

const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
require('dotenv').config();

const locationsRouter = require('./routes/locations');

const app = express();

app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());

app.use('/api/locations', locationsRouter);

module.exports = app;

locations.js:

const express = require('express');
const router = express.Router();
const sequelize = require('../configurations/MySQL');
const Sequelize = require('sequelize');
const passport = require('passport');
require('../configurations/Password')(passport);

router.post('/search_location', passport.authenticate('jwt', {session: false}, null), function(req, res) {
    const token = getToken(req.headers);
    if (token) {
        sequelize.query("SQL_STATEMENT",
        {
            replacements: {
                latitude : parseInt(req.body.latitude),
                longitude: parseInt(req.body.longitude)
            },
            type: Sequelize.QueryTypes.SELECT
        }).then((locations) => {
            res.status(200).send(locations)
        }).catch((error) => {
            console.log(error);
            res.status(500).send(error);
        });
    } else {
        return res.status(401).send({
            status: false,
            description: "Access denied."
        });
    }
});

Viewing all articles
Browse latest Browse all 140762

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>