↓ Skip to main content
An ink and watercolour drawing of two glass jars on a workbench under two brass taps fixed to a wooden board. The left tap is closed with one last drop hanging from it, the left jar is empty, and a folded cloth lies beside it. The right tap is open, and a thin stream fills the right jar.

What a Script Costs: Performance, Clean-up and Leaks

Maximo Automation Scripts - This article is part of a series.
Part 17: This Article
A script runs in the same thread as the business logic it extends. While it runs, the user, the import or the cron task waits for it. What a script costs is its run time times how often it runs, and the launch point decides how often. This article shows both halves, and the clean-up a script owes for the sets it opens itself.

This article closes Part 4. We checked IBM’s pages against the behaviour of Manage in the MAS 9.2 series, and we ran the doubtful call on our test system. Where the pages and Manage disagree, this page follows Manage.

How often a launch point runs
#

Launch point and eventRuns
Object, initializefor every record loaded into a set, on the List tab too, whether a field is shown or not
Attribute, initialize valueonce per record, on the first read of the field
Attribute, validate or actionevery time a value is set on the field, by a user or by code
Object, save eventson every save, from an application, an integration, a workflow or another script

Two rows deserve a second look.

Object init is the expensive one. A set that reads 500 records runs the script 500 times. For a value that one field shows, IBM’s pages recommend the attribute event Initialize Value. It runs only when someone reads the field, and only once per record.

Attribute scripts run once per value set. The script never sees keystrokes. It runs when a value is set on the field, and code sets values far more often than users do: an import, an integration or a script loop sets the field once per record, and the script runs each time. A script that sets a value on a related record runs that record’s attribute scripts too, inside the same call.

Where costly work belongs
#

Where a costly step belongs Where a costly step belongs
Where a costly step belongs

The check for a changed field is short:

if mbo.isModified("STATUS"):
    check_status_rules(mbo)

check_status_rules stands for your own code. On a save that leaves the status alone, the check is all the script costs.

A List tab check that works
#

IBM’s pages suggest running a costly object init only for a record opened on the Main tab, and skipping it on the List tab. Their sample calls UIContext.isFromListTab() on the class. That call fails: the method belongs to the current context object. On our test system it failed with a TypeError, “expected 1 args; got 0”.

The working form asks the current context:

from psdi.common.context import UIContext

ctx = UIContext.getCurrentContext()
if ctx is not None and not ctx.isFromListTab():
    load_details(mbo)

load_details stands for your own code. Outside a request from an application, the current context is None. We saw that in a script called through the REST API. So this check also skips the costly part for REST, integration and escalation work. If that work needs it, test for None on its own.

The clean-up a script owes
#

A set that the script opens itself, with service.getMboSet, belongs to the script. The script has to close it with cleanup(), in a finally block, so that an error cannot skip it:

s = service.getMboSet("ASSET", userInfo)
try:
    s.setWhere("siteid = 'BEDFORD'")
    count_assets(s)
finally:
    s.cleanup()

count_assets stands for your own code. cleanup() resets the set, which closes it, and drops its transaction. This also closes the database statement that a set can keep open after a partial read, as article 16 shows for isEmpty().

Two more points:

  • Database connections. A script that takes a database connection must free it again, in the same way. Manage even ships a check for this, which compares the calls that take a connection with the calls that free one.
  • A safety net exists. With the system property mxe.script.autocleanup set to true, Manage cleans up every set of this kind that the script still holds in a variable at the end of the run, when the set still has records. It logs a warning with a stack trace to the database connection logger. It covers only scripts that run from the engine’s compiled cache, and a set that no variable holds at the end is missed. So the property finds forgotten clean-ups, and it replaces none. The default is false, and neither IBM page names it.

Article 31 of this series covers the warnings framework, which also counts the clean-up calls.

What IBM’s pages get wrong here
#

TopicIBM’s pagesWhat Manage does
The List tab sampleUIContext.isFromListTab() on the classfails with a TypeError; call it on UIContext.getCurrentContext(), which is None outside an application
The clean-up safety netnot namedmxe.script.autocleanup cleans up root sets still held in a variable after the run

IBM’s advice to clean up every set from service.getMboSet in a finally block is right. So is the advice to prefer Initialize Value over object init.

Article 33 of this series collects the corrections that matter most.

What comes next
#

This ends Part 4. Part 5 is about integration scripts: the exits of the integration framework and the names Manage looks for.

IBM’s own pages are the reference to keep next to this series:

The complete list of corrections, with the evidence for each one, is in our member area. Members get access on request, so get in touch if you want it. If you want help with the scripts in your own Manage, our services page shows how we work.

IBM and Maximo are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide.

Maximo Automation Scripts - This article is part of a series.
Part 17: This Article