An official website of the United States government US flag signifying that this is a United States federal government website

Running one-off tasks

Running one-off tasks

There are several options for running tasks in

This table shows a handful of common operations, with the recommended way to perform them in each environment.

What are you trying to do? East/West GovCloud
Inspect an app instance to figure out what’s wrong Not possible, unfortunately. cf ssh
Work with one of your service instances cf-ssh cf ssh with port forwarding
Run a non-interactive process that does a task (such as generating a report, cleaning up garbage, mailing people, processing some data, etc.) Short-lived app Cloud Foundry Tasks

Deploy a short-lived app

Know before you deploy

  • This requires a “complete” application manifest (which needs to include all of your service bindings, etc.) — if you don’t have one yet, generate one from an existing app:

    cf create-app-manifest <APP_NAME> -p manifest.yml
  • This will spin up an instance of your environment uploading your local code.

    • Note that this may not be in sync with your live app.
    • You can leverage this fact to include files that your one-off task might need, such as a data file for importing.

How to deploy

The idea here is that we are going to deploy a new application, but running the one-off task, rather than starting a server or whatever it would normally do. Note that this will not work for any command that is interactive.

  1. Make a copy of your application manifest:

    cp manifest.yml task_manifest.yml
  2. Modify the task_manifest.yml:

    • Change the name value to be task-runner (or something descriptive).
    • Remove the following attributes, if present:
      • domain
      • domains
      • host
      • hosts
      • instances
      • random-route
    • Set the command attribute to be the following:

      command: (<your command> && echo SUCCESS || echo FAIL) && sleep infinity
  3. Deploy the one-off app, and view the output:

When deploying the one-off tasks, it’s important to disable health-checks and routes in order to prevent deployment issues during the buildpack phase and having multiple applications with the same mapped route respectively. For more information on these options, see the --no-route and --health-check-type documentation. In order to keep changes to your copied manifest at a minimum, you can provide these configuration options directly on the command-line.

cf push -f task_manifest.yml --health-check-type none --no-route
cf logs --recent task-runner
  1. If needed, use cf files to collect any artifacts.
  2. Run cf delete task-runner to clean it up. If you don’t do this, your short-lived app may automatically run itself again in the future. sometimes automatically restarts apps as part of routine operations (such as platform updates), which can include restarting this kind of app if it hasn’t been deleted.