简体   繁体   English

Sequelize 在连接到 heroku postgres 数据库时卡住了

[英]Sequelize gets stuck connecting to heroku postgres database

there are tons of questions like this on SO but none of them really have the same problem. SO上有很多这样的问题,但没有一个真的有同样的问题。

I'm trying to connect to a Postgres DB with sequelize and locally it works with the DB hosted on heroku.我正在尝试使用 sequelize 连接到 Postgres 数据库,并在本地与 heroku 上托管的数据库一起使用。 It did also work a couple of days ago, but now it doesn't and looking at the diffs nothing changed in the way the app connects to the database.几天前它也确实有效,但现在没有了,并且查看差异在应用程序连接到数据库的方式上没有任何变化。

When I deploy the app, it gets stuck trying to connect to the DB, until the web worker throws a timeout because the app does not bind to a port.当我部署应用程序时,它在尝试连接到数据库时卡住了,直到网络工作者因为应用程序没有绑定到端口而引发超时。 This makes it hard to debug, since I don't have an error to work with.这使得调试变得困难,因为我没有要处理的错误。

  async connect(): Promise<void> {
    const dbUrl = process.env.DATABASE_URL as string

    if (!dbUrl) {
      throw new Error('Cannot connect to database, url not set!')
    }

    logger.info(`connecting to database ${dbUrl.replace(/:(\w+)@/, ':***@')}`)

    this.sequelize = new Sequelize(dbUrl, {
      dialect: 'postgres',
      protocol: 'postgres',
      logging: (sql, timing) => {
        logger.debug(`Executed SQL query '${sql}' in ${timing} ms`)
      },
      dialectOptions: {
        decimalNumbers: true,
        ssl: true
      }
    })
    
    // it gets stuck here
    await this.sequelize.authenticate({
      logging: (sql, timing) => {
        logger.debug(`authentication ${sql}, ${timing}`)
      }
    })
    logger.info('Database connection successful')

    logger.info('initializing models...')
    await this.initializeModels(this.sequelize)
    logger.info('synching database...')
    await this.sequelize.sync({
      alter: true,
      logging: (sql, timing) => {
        logger.debug(`Executed SQL query '${sql}' in ${timing} ms`)
      }
    })
  }

Here the log output:这里的日志输出:

ct 23 02:59:34 staging-monk-backend heroku/web.1 State changed from crashed to starting
Oct 23 02:59:40 staging-monk-backend heroku/web.1 Starting process with command `npm start`
Oct 23 02:59:43 staging-monk-backend app/web.1
Oct 23 02:59:43 staging-monk-backend app/web.1 > 8bit-bot-backend@1.0.0 start /app
Oct 23 02:59:43 staging-monk-backend app/web.1 > node build/main.js
Oct 23 02:59:43 staging-monk-backend app/web.1
Oct 23 02:59:44 staging-monk-backend app/web.1 2020-10-23T09:59:44.371Z [info] : NODE_ENV is production 
Oct 23 02:59:44 staging-monk-backend app/web.1 2020-10-23T09:59:44.375Z [info] : debug mode is off  
Oct 23 02:59:44 staging-monk-backend app/web.1 2020-10-23T09:59:44.375Z [info] : connecting to database postgres://rlbditxvnovvvq:***@ec2-54-246-87-132.eu-west-1.compute.amazonaws.com:5432/dd1v94insrdacc 
Oct 23 02:59:45 staging-monk-backend app/api Build succeeded
Oct 23 03:00:40 staging-monk-backend heroku/web.1 Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
Oct 23 03:00:40 staging-monk-backend heroku/web.1 Stopping process with SIGKILL
Oct 23 03:00:41 staging-monk-backend heroku/web.1 Process exited with status 137
Oct 23 03:00:41 staging-monk-backend heroku/web.1 State changed from starting to crashed

The Procfile: Procfile:

web:npm start

Versions版本

  • pg : 7.5.0 pg :7.5.0
  • sequelize : 6.3.5 sequelize :6.3.5

This issue pointed me in the right direction.这个问题为我指明了正确的方向。 The problem was the pg package version, after upgrading to 8.4.1 everything works again.问题是pg包版本,升级到 8.4.1 后一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM