Gateway event script sometimes fails to execute a function

I have a tag change gateway event script running.

if not initialChange:
    var_Furnace = '1W'
    tempReached = system.tag.read('somePath').value
    tempReachedAck = system.tag.read('somePath').value

    if tempReachedAck == 1:
        pass
    else:
        pairID = system.tag.read('somePath).value
        id1 = system.tag.read('somePath').value
        id2 = system.tag.read('somePath').value

        if pairID > 0:
            #THIS LINE SOMETIMES RUNS
            project.functions.tempReached(var_Furnace, id1, id2) 

            #THIS LINE ALWAYS RUNS
            system.tag.write('somePath', 1)

Sometimes the second the second to last line of code doesnā€™t run though (it appears to not run). I am doing a few database queries and updates but nothing too intensive. I use an info logger but nothing appears on my console log.

furnaceLogger = system.util.getLogger("Furnaces")

Is there a way to log some steps taking place during that function call? What should I check in my gateway to see if that function is actually executing?

I donā€™t have a complete answer based on the code as I have never used Gateway Event Tag Change Scripts, but I do know that the furnaceLogger.info() will log to the Gateway Status page rather than the console log.

Ok, I see.

Iā€™ve looked at the logs around the time I ran the last test and the function was indeed ā€œskippedā€ from what I can tell. There are several furnaces. Furnace #1 ran correctly, furnace #2 skipped the function, and furnace #3 ran correctly. I know the function was ā€œskippedā€ because in the function being called the first thing I do is log that I am accessing that function.

furnaceLogger.info('Executing function project.functions.tempReached(var_furnace, id1, id2)')

I then triggered the same tag manually after #1 and #3 ran and the logs show #2 correctly running.

Thoughts?

Iā€™ve added a return value on the function being called and write that info to a memory tag. I will try to replicate the issue again as I am currently at the customer site.

I would be curious to know which project hosts the project.functions.tempReached function and also what Project the Gateway uses for its scripts

I don't understand what you mean. How can I look this info up?

https://docs.inductiveautomation.com/display/DOC80/Project+Library#ProjectLibrary-GatewayScriptingProject

The Gateway Settings has a field for that. Iā€™m assuming youā€™re using 8.0

If youā€™re using 7.9, Iā€™m not exactly sure why that function wouldnā€™t be called

I donā€™t see how your project script would run. With how your code looks Iā€™m assuming your not using 8.0. On 7.9 a project script runs for a specific project but you stated you are running this on a gateway script. A gateway script can not call a project script because it is running on the gateway and is not linked to a specific project. It would need to be a global shared script. You would need to move the script to your global shared scripts and then change your path to call the script to reflect it.

@bpreston OP stated a tag change gateway event script which means it will have access to the project script library.

1 Like

My mistake, I was grouping all the gateway stuff into one in my head.

Even though it may be able to call a project script, how are the queries set up since he said its running queries. Some places you donā€™t call out the project name when calling a query, others you do. Calling it from a gateway timer script it does have to be called but he should also see an error in his logs if the script is failing unless he did something in the project script to prevent it.

A gateway event script -> timer does not require the project name either. Those scripts are project based so the project name is not required.

You would think that but I tested it before I replied because I knew I was bit by it before. If I do a simple project query that just calls a named query and leave the project out of the runNamedQuery function it works fine calling it from the script console but the gateway timer script will fail on the line that calls the project script. If I add in the project name then it will fail in the script console but the gateway timer script will run.

Yes runNamedQuery will require the project name, but runPrepQuery doesnā€™t.
This makes sense since runPrepQuery is a talking directly to a database where the named query is a project resource. So at the gateway level the project name makes sense here.

Have to love consistency. Personally I would prefer it if I could just put in the project name everywhere and have it run everywhere. There would be less chances of accidentally doing it the wrong way for where you call it.

Its the reason a lot of my scripts have something like this in them.

def queryScript(tBit=0):
	queryPath = "query"
	params = {}
	if tBit:
		data = system.db.runNamedQuery(queryPath,params)
	else:
		data = system.db.runNamedQuery("projectName",queryPath,params)

Then I can call it without anything passed across in places I need the project name passed in and I pass a 1 in when I call the script if I donā€™t need the project name.

1 Like

runNamedQuery()'s function signature was mis-designed, and in a way that prevents it from being fixed in any backward-compatible form. The ā€˜projectā€™ argument should have been after path and parameters, and optional in project contexts. Iā€™d love to see it deprecated, with the replacement as above. Shortened to .namedQuery(), perhaps. (I also think clients should be able to call Named Queries in other projects. Not currently possible, though it can be worked around with sendRequest.)

4 Likes

Iā€™ve got to agree with this. From a users perspective, runNamedQuery() is unnecessarily quirky.

Someone has posted this in ideas for anyone interested in seeing this modified.
https://inductiveautomation.canny.io/ignition-features-and-ideas/p/simplify-systemdbrunnamedquery-gatewayproject-scope

Unfortunately, I still canā€™t log into the ideas site. Itā€™s been quite a while since I was notified that ticket was in progressā€¦ /:

This is not true from what Iā€™ve seen this morning. Iā€™m still investigating the first issue I had with a gateway script but on a separate tag change gateway script the code can most definitely not call a project script.