Job Examples

ENTERPRISE

Examples of common usage scenarios for jobs.

These examples provide common usage scenarios for jobs.

Prerequisite:

Creating a Simple Job

This JSON file 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 UI.

  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 a 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
    

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 UI. 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:

  • You must be logged in as a superuser.
  1. Log into the DC/OS UI as a user with the superuser permission.

    Login

    Figure 1. DC/OS Enterprise login

  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.

      Create job

      Figure 2. New job screen

      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.

      Create another job

      Figure 3. 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:

      create job

      Figure 4. Jobs > dev > batch screen

  3. Run the jobs.

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

      Run job

      Figure 5. Run now menu

    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.

      Create user Cory

      Figure 6. Create a new user

    2. Select the user Cory and grant access to job1.

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

      Add permissions cory

      Figure 7. Add permissions for user ‘Cory’

    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.

      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:

    Alice job view

    Figure 8. Restricted view for ‘Alice’