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.

    1. {
    2. "id": "my-job",
    3. "description": "A job that sleeps",
    4. "run": {
    5. "cmd": "sleep 1000",
    6. "cpus": 0.01,
    7. "mem": 32,
    8. "disk": 0
    9. }
    10. }
  2. Add the job from the DC/OS CLI.

    1. dcos job add <my-job>.json

    Alternatively, add the job using the API.

    1. 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.

    1. {
    2. "id": "my-scheduled-job",
    3. "description": "A job that sleeps on a schedule",
    4. "run": {
    5. "cmd": "sleep 20000",
    6. "cpus": 0.01,
    7. "mem": 32,
    8. "disk": 0
    9. },
    10. "schedules": [
    11. {
    12. "id": "sleep-nightly",
    13. "enabled": true,
    14. "cron": "20 0 * * *",
    15. "concurrencyPolicy": "ALLOW"
    16. }
    17. ]
    18. }
  2. Add the job.

    1. 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.

    1. {
    2. "concurrencyPolicy": "ALLOW",
    3. "cron": "20 0 * * *",
    4. "enabled": true,
    5. "id": "nightly",
    6. "nextRunAt": "2016-07-26T00:20:00.000+0000",
    7. "startingDeadlineSeconds": 900,
    8. "timezone": "UTC"
    9. }
  3. Add the schedule and associate it with the job.

    Via the DC/OS CLI:

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

    Via the API:

    1. 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.

  1. ├── dev
  2. ├── batch
  3. ├── job1
  4. ├── 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.

      1. dcos:adminrouter:service:metronome full
      2. dcos:service:metronome:metronome:jobs:dev.batch.job1 read
      3. dcos:adminrouter:ops:mesos full
      4. dcos:adminrouter:ops:slave full
      5. dcos:mesos:master:framework:role:* read
      6. dcos:mesos:master:executor:app_id:/dev/batch/job1 read
      7. dcos:mesos:master:task:app_id:/dev/batch/job1 read
      8. dcos:mesos:agent:framework:role:* read
      9. dcos:mesos:agent:executor:app_id:/dev/batch/job1 read
      10. dcos:mesos:agent:task:app_id:/dev/batch/job1 read
      11. 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’

Creating a job which joins a container network

In order to have a job join a network, specify the “networks” field. The following job joins the dcos network, outputs its IP information, then sleeps:

  1. ```json
  2. {
  3. "id": "networking-job",
  4. "description": "A job that outputs network info and sleeps",
  5. "run": {
  6. "cmd": "ip a; sleep 60",
  7. "docker": {
  8. "image": "alpine"
  9. },
  10. "networks": [{"mode": "container", "name": "dcos"}],
  11. "cpus": 0.01,
  12. "mem": 32,
  13. "disk": 0
  14. }
  15. }
  16. ```

The field networks works identical to the networks field in Marathon.