serverless-mysql icon indicating copy to clipboard operation
serverless-mysql copied to clipboard

local function hangs

Open srchulo opened this issue 5 years ago • 4 comments
trafficstars

When I follow this example:

// Require and initialize outside of your main handler
const mysql = require('serverless-mysql')({
  config: {
    host     : process.env.ENDPOINT,
    database : process.env.DATABASE,
    user     : process.env.USERNAME,
    password : process.env.PASSWORD
  }
})
 
// Main handler function
exports.handler = async (event, context) => {
  // Run your query
  let results = await mysql.query('SELECT * FROM table')
 
  // Run clean up function
  await mysql.end()
 
  // Return the results
  return results
}

By invoking my function locally:

sls invoke local -f myFunction

results gets returned by the handler, but then it just hangs forever and the sls command never returns (command prompt doesn't come back). I need to hit ctrl c to end it.

srchulo avatar Jun 13 '20 23:06 srchulo

I've tried setting:

context.callbackWaitsForEmptyEventLoop = false

And it still doesn't work, but I don't think that I should need to set that since I'm using an async function?

srchulo avatar Jun 14 '20 01:06 srchulo

If I do mysql.quit() instead of end, my function doesn't hang, but obviously that's not great :)

srchulo avatar Jun 14 '20 01:06 srchulo

From this link, it looks like maybe this is expected behavior:

https://github.com/serverless/serverless/issues/470#issuecomment-205372006

And maybe it should be covered in the documentation?

srchulo avatar Jun 14 '20 01:06 srchulo

@srchulo did you get this all sorted out? I'm trying to get serverless-localstack working with serverless-mysql and I just can't get it to work. I believe that issue you reference provides some insight but I just can't develop with this plugin locally as it doesn't work unless just invoking functions (and even still, I'd have to change the code for staging/production).

davidrhoderick avatar Apr 25 '21 13:04 davidrhoderick