So basically I am trying to write script whose execution will depend on if the same script was run in last half an hour. If the script was run in previous half an hour, a call should no be placed inside the script, otherwise it should. I tried to do it with session get/set info and Default Session Timeout* parameter set to 30 mins, but I am getting an error that the sesion is null and I can't get pass this step.
Script is HTTP triggered and I want to make sure it is run only once in half an hour, no matter if it gets triggered more often than that.
Any ideas on how to accomplish this?
Thanks for any suggestions,
I am not quite sure I can follow you. Sessions are not bound to a contact and in this case we don't really care if they are active or idle.
This way: sessions are like temporary databases. You can store key-value pairs in these temporary databases. Since they are temporary, they disappear in a certain time. This must be handled in your application gracefully, however, one should not build an application around it. In your case, you should not really fiddle around with the default session expiry time.
I tossed together a script that does the following:
1. asks the system: hey, is there a session named "simpleDB"? Or, more preciselly, is there a session with the following mapping ID: "simpleDB"? If yes, we assume we can refer to it in a variable aptly named "session". If not, we will create a new session, store it in the variable named "session" (Get Session step)
2. next, we ask the system: is there a row in our database with the ID of "nanosLastTime"? Or, more precisely, is there a context attribute named "nanosLastTime" in the variable referred as "session"? Assign its value to a local variable named "nanosLastTime". (Get Session Info step).
3. let's see the outcome of the previous step: is it null (meaning there's no such session attribute)? If it's null, let's assign the value of the "nanosLastTime" local variable: System.nanoTime(), then map it to the session "session" and then save it (the first If step and its True branch). If it's not null, just continue to the next step.
4. Take the value of System.nanoTime() and assign it to the "nanosNow" variable.
5. Is (nanosNow - nanosLastTime) < desiredDelta, meaning did not the specified number of nanoseconds passed since the actual database record? If yes (true), then that's it, the script ends. If not (false), then let's just continue to the next step.
6. Update and save the session "simpleDB".
7. Do something useful.
Now some details:
- why this nanoTime construct: it's safe and it's just for this purpose, measuring the passed time between two moments. This is supposed to be safe even when the system clock is adjusted;
- why not use a mechanism to check whether the session mapped with an ID exist and use the default session timeout? Think universal.