Scripts and scheduling
These scripts can be declared in the section of the service manifest:
Scripts can be invoked manually using the , the Foxx CLI or the .
Additionally the special setup
and teardown
lifecycle scripts can be invoked automatically by Foxx as part of a service’s lifecycle (see below).
When invoking a script any arguments will be exposed to the script as the argv
array property of the service context.
Any value exported by the script using will be the script’s return value. Please note that this data will be converted to JSON.
Any errors raised by the script will be handled depending on how the script was invoked:
Examples
The following script will use its argument to generate a personal greeting:
Scripts named setup
or are considered lifecycle scripts and will (by default) be invoked automatically by Foxx:
when a service is installed, upgraded or replaced, the new service’s
setup
script will be executed before it is mountedwhen a service is removed or replaced, the old service’s
teardown
script will be executed before it is unmounted
However it’s possible to override this behavior as needed.
The setup script is typically used to create collections a service needs, to define indexes or to initialize collections with necessary data like administrative accounts.
As the setup script may be executed more than once it should be treated as reentrant: running the setup script again should not result in any errors or duplicate data:
Teardown Script
The teardown script typically removes the collections and/or documents created by the service’s setup script.
In practice teardown scripts are rarely used due to the risk of catastrophic data loss when accidentally running the script while managing the service.
Depending on the amount of data managed by the service and the amount of work that needs to be done to prepare collections for the service, running a script on every upgrade can be very expensive.
An alternative approach is to perform incremental steps in separate migration scripts and run them manually after the service is installed.
A setup
script should always create all the collections a service uses but any additional steps like creating indexes, importing data fixtures or migrating existing data can safely be performed in separate scripts.