Calling Project Library Script in a scheduled gateway event

I would like to use a gateway scheduled event to look for changes in a db. The scheduled event will run code every minute. When I see a change I would like to use scripts that I have in the Project Library scripts section. I would like to call those scripts from the gateway event script because I have already written all the scripts that work with the DB and the PLC. I cannot figure out how to call them or if it is even possible.

Also, is this the best way to look for a change in a DB?

I am using Ignition 8.3

Thanks

Not only is it possible, but putting your logic entirely in the project library is highly recommended. The event script becomes a one-liner:

someLibraryPackage.someLibraryScript.someFunction()

(With argument passing for event types that have per-event metadata.)

When you get to the first dot, the editor's auto-complete should try to help you.

2 Likes

I get the following errors when I try to run it. Any suggestions on what this error means?

If I run the Main.mainScripts() from a perspective button it works.

Pictures are great for context but please also post copies of code as preformatted text. It helps to speed up the troubleshooting process by others, see Wiki - how to post code on this forum. You can edit your post to add it via the pencil icon in the bottom right.

Please also post the full error stack trace (if one exists) as preformatted text as well, the full trace will have important information to help troubleshoot.

Based on your existing logic and logging, I'd say the failure is somewhere in your mainScript function, likely in one of the other functions that you are calling. If you are touching a DB, you are likely getting a java db error which won't be caught as a python Exception.

Add

except java.lang.Throwable as t:
    logger.warn("A java exception occured", t)

to your try block, this should catch any java errors.

Additionally, I would avoid having import traceback that late in the script (or in a function at all). Move the gateway script logic to a project library script and call it with a single line from the scheduled script. Import traceback at the top of the project library script outside of the function.

1 Like

Thanks for the information. And I will add the exception code.

I found the error to be cause by using system.perspective.print() statements in my other code, and maybe that is why it was fine when I called it in a perspective View but not a gateway script.

imports should strictly only ever be used at the top of script libraries. Google jython import lock contention for context

1 Like