Hi @athuldilip,
The Architect Scripting session is a singleton instance in Scripting and when code calls startWithClientIdAndSecret or startWithAuthToken, it will set the session state to running and, as you can see, we don't allow subsequent calls to either of the start methods on that session while the state is running:
if (session.status === enums.SESSION_STATUSES.running) {
// session is busy, calls to startWithClientIdAndSecret
// or startWithAuthToken will fail
}
First thing is if you're looking to call one of the start methods again is you'd set up the session like this:
// ***********************************************************
// Configure the session to end but not log out
// *if* you're re-using the same auth token on
// Scripting session start calls. Maybe?
// ***********************************************************
scriptSession.endMode = enums.SESSION_END_MODES.end;
// ***********************************************************
// We'll also tell the Scripting session singleton
// not to call process exit by setting the
// endTerminatesProcess property to false
// ***********************************************************
scriptSession.endTerminatesProcess = false;
It sounds like Scripting is running in a web service. I'm not sure of the exact setup but I'd recommend some way in your web service to synchronize calls to the startWithAuthToken method so you don't run in to the case of multiple threads all trying to call the start methods on the singleton session at the same time and running in to the problem you're seeing.
For example, you could create a queue where inbound requests to your web service queue up a job instead of calling one the session start methods directly. Now the queue will effectively enforce one at a time calls to the start methods. Or if you have some kind of locking available on incoming request threads, you could gate calls to the start methods by threads obtaining a lock and that lock will get released once the session's state is ended.
With either approach, how can you know when a session is ending? The ArchSession::startWithAuthToken and ArchSession::startWithClientIdAndSecret calls accept a callback function you can specify as the last parameter. This is called when a session's state is ending.
Since you no longer have the session configured to be endWithLogout and not terminate the process, it is this callback where Scripting is letting you perform any additional cleanup you want to perform, check the session's exit code or whatever. When you specify a callback function there, execution will look like this:
> Scripting logs a note of
setting session status to 'ended'
> Scripting logs a verbose note of
'calling session end callback function'
> Next Scripting invokes your callback function.
> When the callback function ends and execution returns to Scripting,
it will log another verbose note of:
session end callback function execution complete.
> Then it will log a note:
ending with exit code: <blah>
> Now is when you'd want to start up the next job that was queued
up, release the lock to let someone else do work, etc. etc.
Since Scripting is going to write a couple more traces after invoking your callback, I would not recommend invoking calls to the startWithAuthToken or startWithClientIdAndSecret inside of the callback method itself. Technically you'll see that the state of the session is ended at that point there but with the subsequent traces it's nice to let those happen IMO. For example, you could set some kind of small timer in that method that when it expires you then either release the lock that prevented other threads from executing and/or look at the queued jobs and start up the next job or whatever but after the callback function completes.
Obviously it's up to you how you'd want to synchronize calls to the start calls on the ArchSession instance. Hope the ending callback along with the other ArchSession endMode / endTerminatesProcess property settings help.
Jim