How can I persist a Pengines instance beyond a single query?

I’m using: SWI-Prolog version 8.0.2 on Ubuntu Linux 14.04.

Happily I now have my Node.JS client code working with my Pengines server. I have set up event handlers for all the available Pengine events (at least from what I can see):

  • onabort
  • oncreate
  • ondestroy
  • onfailure
  • onerror
  • onprompt
  • onstop
  • onsuccess

I was surprised to discover that after making a simple ask() call against my client side Pengines instance, that after the query succeeded, my ondestroy handler immediately fired. It seems that the Pengines instance automatically destroys itself after executing my ask() query?

  • If I’m right about this and it’s not due to some other error I’m making, then is there a way for me to leave the Pengines instance active until I want to shut it down?

  • If not, what could I have configured wrong that is leading to this behavior? I am not making an explicit call to the Pengine object’s destroy() method from my Javascript code.

  • Or is there something I need to do on the Prolog Pengine app side (i.e. - my Pengine app) to leave the instance active? Is that what the pengine_event_loop() method call is for, that I saw in one of the other examples? In other words, if you don’t add a Pengine event loop to your app then it immediately exits after it is called into?

My main architectural strategy will be to create an instance of a Pengines engine when the Node.JS server starts up. I will then use that instance for all queries to my Node.JS app that talks to the Pengines server. I won’t destroy the Pengines instance until I receive a shutdown notice from Node.JS (i.e. - receive a SIGNINT event from the Node.JS process object). That way, I can load up my Pengines instance with a lot of common code that is used by all my users without having to redo that initialization step with each user interaction, whoch would burden my users with a slow response time.

That is the default behavior. You need to pass option destroy: false if you want it to be not destroyed automatically.

The best set of examples would probably be in pengine js test-cases: https://github.com/SWI-Prolog/packages-pengines/blob/463c37310f30a081370f0cf282c5e4a05c5b84d4/test_js/tests.js#L448

If you are not actually needing separate execution contexts that pengines provides, then you could also use Node.js swi addon (native or stdio-based one). The addon is more query-oriented and can handle errors based on the given query, not only globally.

1 Like

Thanks! I’ll have a look.