Closed StCyr closed 7 years ago
I have two concerns about this type of approach:
The only way I can see of avoiding such race conditions is to program additional logic into the script that is running python-otrs to begin with. The 'silent reauthenticate' that you are suggesting becomes potentially undesirable if you are developing special logic to handle this in certain cases.
As a suggestion, instead of running your ticket search for new and open tickets under the 'try' block, why not just run a simple test-case search that uses very low CPU in OTRS (ex. searching for ticket with ticket number given, where you know that ticket will always exist). If that single operation fails due to to session expiration then you know you have to reauthenticate. That is the way you would need to do it anyway to help avoid race conditions because if a larger script ran, silently reauthenticated the session on first operation, and only wrote the updated session at the very end of the script, then another script could easily run in the interim which would use the old session and create a second new session needlessly.
Fully agree with @mjducharme. My reaction wouldn't have been this elaborate (thanks again ;), yet the summary would have been along the lines of "Please keep this logic in your own script".
You need some additional logic anyway (for instance to read and write the temp files). As far as I can see, passing an optional session_id to the client and would barely reduce your script, while reducing predictability of the library: should a session be created when a session_id is specified, yet there is no valid session (which is not what what normally happens when creating a client object...)? And if so, how should the session_id be returned to the initials script that initiates the client object?
Furthermore, it would not be sufficient in case of a long (permanently?) running script which would require a wrapper to precede each request with a check if the session is still valid (or keep track of "time since last request", also specify a value with the duration before a session times out and hope there's no other actions elsewhere interfering with your sessions, etc.).
So in short, it would add extra complexity which will only work for part of the scripts users could be coming up while the additional logic in their scripts would stay almost the same...
You could, as @mjducharme suggests, use a light weight check and modify your except to only catch the expected error in case of no session_id.
Hi,
Thanks for your ( extensive :-) ) feedback. It all make sense.
Cyrille
Hi,
If you try to use an existing session_id (that you previously stored in a temporay file, for example), it will work OK unless the corresponding session_id doesn't exist (eg: has been removed) in the OTRS you're polling. In this case your SOAP operations will fail miserably.
python-otrs should be able to handle the case where a session_id fails to authenticate the SOAP operation and fall back to create a new session_id using regular login/password credentials.
Without such capability, python-otrs is not very fit for execution in cronjobs.
Here's an illustrative example of code I made to use python-otrs within a cronjob without creating hundreds of sessions in my OTRS instance every day: