setGlobalData issue

Post Reply
loicaigon
Advanced member
Posts: 372
Joined: Wed Jul 10, 2013 10:22 am

setGlobalData issue

Post by loicaigon »

Hi,

In a jobArrived function, I want to use if some data has already been stored through setGlobalData => s.setGlobalData("scope","key","value",true);

if that value is empty when I read it thanks to s.getGlobalData("scope","key") then I am doing some API request to collect the value I am interested in and stored as shown above.

Problem is, value seems to never be stored while tracing shows that setGlobalData is correctly called. I really can't see what I am doing wrong because I RTFM'd the manuals several times and I am running out of ideas here.

Any ideas ?
freddyp
Advanced member
Posts: 1017
Joined: Thu Feb 09, 2012 3:53 pm

Re: setGlobalData issue

Post by freddyp »

I have never had any problems with global data. Are you using variables for the scope and key? If so, then my first idea would be that there is a discrepancy between the values at set time and get time.
You store the result of an API request. Assuming that it is a REST API call and you store the server response, then you will have a problem because the server response is a ByteArray, but in global data you can only store strings. So you have to convert it to a string. When you do that, however, the value stored in global data is not empty, so this is probably not the problem, I just wanted to make this point. It is like when you do a s.setGlobalData( "scope", "key", undefined). When you get this global data you get the string "undefined"!
Now I am out of ideas too.
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: setGlobalData issue

Post by Padawan »

I did some tests and I noticed that when I try to read the global data in the same script instance as I store it that it can't access it (Switch 2019). But the next script element is able to read it.

Code:

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var testScope = "testScope";
	var testTag = "testTag2";
	
	
	var currentValue = readGlobalData(s, testScope, testTag);
	job.log(-1, "Current value: " + currentValue);
	
	setGlobalData(s, testScope, testTag, "12345");
	
	newValue = readGlobalData(s, testScope, testTag);
	job.log(-1, "New value: " + currentValue);
	
	job.sendToSingle(job.getPath());
	
}

function readGlobalData(s, myScope, myTag) {
	s.lockGlobalData(myScope);
	var myValue = s.getGlobalData(myScope, myTag);
	s.unlockGlobalData();
	return myValue;
}

function setGlobalData(s, myScope, myTag, myValue) {
	s.lockGlobalData(myScope);
	var myValue = s.setGlobalData(myScope, myTag, myValue, true);
	s.unlockGlobalData();
	
}
Does this help?
freddyp
Advanced member
Posts: 1017
Joined: Thu Feb 09, 2012 3:53 pm

Re: setGlobalData issue

Post by freddyp »

I found a workaround but it is weird:

Code: Select all

	newValue = readGlobalData(s, testScope, testTag);
	job.log( -1, "New value 1: " + currentValue);
	job.log( -1, "New value 2: " + s.getGlobalData(testScope, testTag));
and you will see that the second log does give you the expected global data value. In other words, it seems to make a difference whether you call getGlobalData from jobArrived or from a function called from jobArrived.

I logged it for engineering to have a look at, but in the mean time you have a workaround.
freddyp
Advanced member
Posts: 1017
Joined: Thu Feb 09, 2012 3:53 pm

Re: setGlobalData issue

Post by freddyp »

I take everything back. it is a coding mistake. On line 13 you log currentValue instead of newValue.
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: setGlobalData issue

Post by Padawan »

I really hope that some day I will be able to stop make stupid mistakes like this. Usually I find them before I finish the project, but this one slipped thru :)

Thanks Freddy!

If this is not the issue, then I'm getting curious to the issue Loic is having.
loicaigon
Advanced member
Posts: 372
Joined: Wed Jul 10, 2013 10:22 am

Re: setGlobalData issue

Post by loicaigon »

Hi Freddy,

First of all, thanks a lot for the support and care. I was busy and couldn't come back sooner.

I can't get it to work even by outsourcing the storage operations as you offered. I thought it was due to testing inside the scripter but even loading the script in switch doesn't help.

I will try to move on but it's a side project so progress are slow.
Post Reply