Skip to content
Advertisement

‘failed to create second instance’ error in npm themeparks module in Azure Function

I am trying to use the npm package themeparks to extract information for a database. I want to call themeparks every half hour.

Perhaps someone with experience in using themeparks can help me.

When I test an Azure function using themeparks I get this error.

2020-12-13T22:20:14.430 [Error] Executed ‘Functions.HttpTrigger3’ (Failed, Id=59ad2b28-6c19-4655-9edb-16b407b949af, Duration=4ms)Result: FailureException: Error:Failed to create second instance of “WaltDisneyWorldMagicKingdom” object.Please only create one instance of each location and re-use it.Stack: Error:Failed to create second instance of “WaltDisneyWorldMagicKingdom” object.Please only create one instance of each location and re-use it.at new Location (C:homenode_modulesthemeparksliblocation.js:77:13)at new Park (C:homenode_modulesthemeparkslibpark.js:57:5)at new HostedPark (C:homenode_modulesthemeparkslibhostedPark.js:7:5)at new WaltDisneyWorldMagicKingdom (C:homenode_modulesthemeparkslibdisneywaltdisneyworldmagickingdom.js:21:5)at module.exports (C:homesitewwwrootHttpTrigger3index.js:6:20)at WorkerChannel.invocationRequest (C:Program Files (x86)SiteExtensionsFunctions3.0.15185workersnodeworker-bundle.js:18546:26)at ClientDuplexStream. (C:Program Files (x86)SiteExtensionsFunctions3.0.15185workersnodeworker-bundle.js:18343:30)at ClientDuplexStream.emit (events.js:315:20)at addChunk (_stream_readable.js:295:12)at readableAddChunk (_stream_readable.js:271:9)

the message indicates that I have created an instance of the themeparks module in a previous execution of the Azure function and not released it. I’d like to know how to release the object in a timely manner if that is the problem.

Some background on the problem. When I test run the function from the Azure Portal, it runs cleanly the first time, and I get the message when I run the Function a second time. When I wait 15 minutes or so to rerun the function from the portal, I do not get the error message. But the file is written to Azure Storage even when I get the error. If I run the function from an Azure logic app, I get an internal server error and the file does not get written.

This is code I used to recreate the problem. It is based on the sample code included in the npm documentation.

module.exports = async function (context, req) {

const Themeparks = require("themeparks");
let myData = "W";

const DisneyWait = new Themeparks.Parks.WaltDisneyWorldMagicKingdom();
let rides = await DisneyWait.GetWaitTimes();

rides.forEach(ride => {
    myData = myData + "!!!" + ride.name ;
});

context.bindings.outputBlob = myData;

}

I appreciate any help in my learning how themeparks works. I want to respect the api writers suggestion to conserve resources by only creating one instance. If there is a way to create the object and keep it available for future executions, that would be helpful also. If any themepark users have other suggestions on running themeparks on a timer, I’d love to hear your experiences. I am new to Javascript and Azure Functions, so I may be making some very basic errors in my attempts to get themeparks working.

Thank you.

Advertisement

Answer

Please move the line of code const DisneyWait = new Themeparks.Parks.WaltDisneyWorldMagicKingdom(); outside of module.exports = async function (context, req).

The update code should be like:

const Themeparks = require("themeparks");
const DisneyWait = new Themeparks.Parks.WaltDisneyWorldMagicKingdom();

module.exports = async function (context, req) {

let myData = "W";

let rides = await DisneyWait.GetWaitTimes();

rides.forEach(ride => {
    myData = myData + "!!!" + ride.name ;
});

context.bindings.outputBlob = myData;

}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement