}}

Examples

Enterprise DC/OS Preview Updated: May 17, 2017

These examples provide common usage scenarios for jobs.

Prerequisite:

Creating a Simple Job

This JSON creates a simple job with no schedule.

  1. Create a JSON file with the following contents.
    {
      "id": "my-job",
      "description": "A job that sleeps",
      "run": {
        "cmd": "sleep 1000",
        "cpus": 0.01,
        "mem": 32,
        "disk": 0
      }
    }
    
  2. Add the job from the DC/OS CLI.
    dcos job add <my-job>.json
    

    Alternatively, add the job using the API.

    curl -X POST -H "Content-Type: application/json" -H "Authorization: token=$(dcos config show core.dcos_acs_token)" $(dcos config show core.dcos_url)/service/metronome/v1/jobs -d@/Users/<your-username>/<myjob>.json
    

Creating a Job with a Schedule

Note: This example JSON only works when you add the job from the DC/OS CLI or the GUI. Use the example below to create a scheduled job via the API.

  1. Create a JSON file with the following contents.
    {
        "id": "my-scheduled-job",
        "description": "A job that sleeps on a schedule",
        "run": {
            "cmd": "sleep 20000",
            "cpus": 0.01,
            "mem": 32,
            "disk": 0
        },
        "schedules": [
            {
                "id": "sleep-nightly",
                "enabled": true,
                "cron": "20 0 * * *",
                "concurrencyPolicy": "ALLOW"
            }
        ]
    }
    
  2. Add the job.
    dcos job add <my-scheduled-job>.json
    

Creating a Job and Associating a Schedule using the API

  1. Add a job without a schedule using the instructions above.

  2. Create JSON file with the following contents. This is the schedule for your job.

    {
        "concurrencyPolicy": "ALLOW",
        "cron": "20 0 * * *",
        "enabled": true,
        "id": "nightly",
        "nextRunAt": "2016-07-26T00:20:00.000+0000",
        "startingDeadlineSeconds": 900,
        "timezone": "UTC"
    }
    
  3. Add the schedule and associate it with the job.
    Via the DC/OS CLI:

    dcos job schedule add <job-id> <schedule-file>.json
    

    Via the API

    curl -X POST -H "Content-Type: application/json" -H "Authorization: token=$(dcos config show core.dcos_acs_token)" $(dcos config show core.dcos_url)/service/metronome/v1/jobs/<job-id>/schedules -d@/Users/<your-username>/<schedule-file>.json
    

Note: You can associate a schedule with more than one job.

Creating a Partitioned Jobs Environment

In this example, a partitioned jobs environment is created with the DC/OS GUI. This allows you to restrict user access per job, or per job group. The jobs are created in a jobs group named batch, which is a child of a jobs group named dev.

├── dev
    ├── batch
        ├── job1
        ├── job2

The jobs groups are then assigned permissions to users Cory and Alice to restrict access.

Prerequisites:

  • DC/OS is installed with security mode permissive or strict.
  • You must be logged in as a superuser.
  1. Log into the DC/OS GUI as a user with the superuser permission.

  2. Create the partitioned jobs.

    1. Select Jobs and click CREATE A JOB.
    2. In the ID field, type dev.batch.job1.
    3. In the Command field, type sleep 1000 (or another valid shell command) and click CREATE A JOB.

      This creates a job in this directory structure in DC/OS: Jobs > dev > batch > job1.

    4. Click the + icon in the top right corner to create another job.

    5. In the ID field, type dev.batch.job2.

    6. In the Command field, type sleep 1000 (or another valid shell command) and click CREATE A JOB. You should have two jobs:

  3. Run the jobs.

    1. Click Jobs > dev > batch > job1 and click Run Now.

    2. Click Jobs > dev > batch > job2 and click Run Now.

  4. Assign permissions to the jobs.

    1. Select Organization > Users and create new users named Cory and Alice.

    2. Select the user Cory grant access to job1.

    3. From the Permissions tab, click ADD PERMISSION and toggle the INSERT PERMISSION STRING button to manually enter the permissions.

    4. Copy and paste the permissions in the Permissions Strings field. Specify your job group (dev/batch), job name (job1), and action (read). Actions can be either create, read, update, delete, or full. To permit more than one operation, use a comma to separate them, for example: dcos:service:metronome:metronome:jobs:/dev/batch/job1 read,update.

      Important: Your security mode must be either permissive or strict.

      dcos:adminrouter:service:metronome full
      dcos:service:metronome:metronome:jobs:dev/batch/job1 read
      dcos:adminrouter:ops:mesos full
      dcos:adminrouter:ops:slave full
      dcos:mesos:master:framework:role:* read
      dcos:mesos:master:executor:app_id:/dev/batch/job1 read
      dcos:mesos:master:task:app_id:/dev/batch/job1 read
      dcos:mesos:agent:framework:role:* read
      dcos:mesos:agent:executor:app_id:/dev/batch/job1 read
      dcos:mesos:agent:task:app_id:/dev/batch/job1 read
      dcos:mesos:agent:sandbox:app_id:/dev/batch/job1 read
      
    5. Click ADD PERMISSIONS and then Close.
    6. Repeat these steps for user Alice, replacing job1 with job2 in the permissions.
  5. Log out and log back in as your new user to verify the permissions. The user should now have the designated level of access to dev/batch/job1 and dev/batch/job2 inside the Jobs tab. For example, if you log in as Alice, you should only see jobs2: