Exit

All ThreatConnect Exchange Apps should deliberately exit with the appropriate exit code on successful or failed execution. The TcEx Framework provides the exit() method and exit_code property to handle exit codes. All Apps should end with the exit() method.

The exit() method supports and optional msg parameter. If provided, the msg value will be logged and written to the message_tc file as the App exit message.

Important

Providing a proper exit code for Playbook Apps is important for execution of downstream Apps. Supported exit codes for Playbook Apps are 0 (success) and 1 (failure).

Note

The exit method automatically logs the exit code using the Logging functionality of the TcEx Framework.

Setting the Exit Code

Some failures do not warrant an immediate exit. In these cases, the exit_code property allows an exit code to be set for when the exit() method is called. This allows the App to continue execution and still notify the ThreatConnect platform that a failure occurred.

Job App Example

 1tcex = TcEx()
 2
 3<...snipped>
 4try:
 5    with open(datafile, 'r') as fh:
 6        data = fh.read()
 7except:
 8    tcex.exit_code = 3
 9
10<snipped...>
11
12tcex.exit(msg='My App message')

Playbook App Example

 1tcex = TcEx()
 2
 3<...snipped>
 4try:
 5    with open(datafile, 'r') as fh:
 6        data = fh.read()
 7except:
 8    tcex.exit_code = 1
 9
10<snipped...>
11
12tcex.playbook.exit(msg='My App message')

Immediate Exit on Failure

Certain failures require that the App exit immediately. In these cases, calling the exit() method while passing the exit code will immediately halt execution of the App and notify the ThreatConnect platform of a failure.

Job App Example

 1tcex = TcEx()
 2
 3<...snipped>
 4try:
 5    with open(datafile, 'r') as fh:
 6        data = fh.read()
 7except:
 8    tcex.exit(1, 'App failed')
 9
10<snipped...>

Playbook App Example

 1tcex = TcEx()
 2
 3<...snipped>
 4try:
 5    with open(datafile, 'r') as fh:
 6        data = fh.read()
 7except:
 8    tcex.playbook.exit(1, 'App failed')
 9
10<snipped...>

Job App Exit Codes

  • 0 - Successful execution of App

  • 1 - Failed execution of App

  • 3 - Partial failure of App

Playbook App Exit Codes

  • 0 - Successful execution of App

  • 1 - Failed execution of App