Scripts and scheduling

    These scripts can be declared in the section ofthe :

    Scripts can be invoked manually usingthe web interface,the orthe Foxx HTTP API.

    Additionally the special setup and teardown lifecycle scripts canbe 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 theargv array property of the .

    Any value exported by the script using will be the script’sreturn value. Please note that this data will be converted to JSON.

    Any errors raised by the script will be handled depending on howthe script was invoked:


    The following script will use its argument to generate a personal greeting:

    Scripts named setup or are considered lifecycle scripts andwill (by default) be invoked automatically by Foxx:

    • when a service is installed, upgraded or replaced, the new service’ssetup script will be executed before it is mounted

    • when a service is removed or replaced, the old service’s teardownscript 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 datalike administrative accounts.

    As the setup script may be executed more than once it should be treatedas reentrant: running the setup script again should not result in any errorsor duplicate data:

    Teardown Script

    The teardown script typically removes the collections and/ordocuments created by the service’s setup script.

    In practice teardown scripts are rarely used due to the risk ofcatastrophic data loss when accidentally running the scriptwhile managing the service.

    Depending on the amount of data managed by the service and the amount of workthat 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 separatemigration scripts and run them manually after the service is installed.

    A setup script should always create all the collections a service usesbut any additional steps like creating indexes, importing data fixtures ormigrating existing data can safely be performed in separate scripts.