github.com/manicqin/nomad@v0.9.5/ui/mirage/factories/job.js (about)

     1  import { assign } from '@ember/polyfills';
     2  import { Factory, trait } from 'ember-cli-mirage';
     3  import faker from 'nomad-ui/mirage/faker';
     4  import { provide, provider, pickOne } from '../utils';
     5  import { DATACENTERS } from '../common';
     6  
     7  const JOB_PREFIXES = provide(5, faker.hacker.abbreviation);
     8  const JOB_TYPES = ['service', 'batch', 'system'];
     9  const JOB_STATUSES = ['pending', 'running', 'dead'];
    10  
    11  export default Factory.extend({
    12    id: i =>
    13      `${faker.helpers.randomize(
    14        JOB_PREFIXES
    15      )}-${faker.hacker.noun().dasherize()}-${i}`.toLowerCase(),
    16  
    17    name() {
    18      return this.id;
    19    },
    20  
    21    groupsCount: () => faker.random.number({ min: 1, max: 2 }),
    22  
    23    region: () => 'global',
    24    type: () => faker.helpers.randomize(JOB_TYPES),
    25    priority: () => faker.random.number(100),
    26    all_at_once: faker.random.boolean,
    27    status: () => faker.helpers.randomize(JOB_STATUSES),
    28    datacenters: () =>
    29      faker.helpers.shuffle(DATACENTERS).slice(0, faker.random.number({ min: 1, max: 4 })),
    30  
    31    childrenCount: () => faker.random.number({ min: 1, max: 2 }),
    32  
    33    periodic: trait({
    34      type: 'batch',
    35      periodic: true,
    36      // periodic details object
    37      // serializer update for bool vs details object
    38      periodicDetails: () => ({
    39        Enabled: true,
    40        ProhibitOverlap: true,
    41        Spec: '*/5 * * * * *',
    42        SpecType: 'cron',
    43        TimeZone: 'UTC',
    44      }),
    45    }),
    46  
    47    parameterized: trait({
    48      type: 'batch',
    49      parameterized: true,
    50      // parameterized details object
    51      // serializer update for bool vs details object
    52      parameterizedDetails: () => ({
    53        MetaOptional: null,
    54        MetaRequired: null,
    55        Payload: faker.random.boolean() ? 'required' : null,
    56      }),
    57    }),
    58  
    59    periodicChild: trait({
    60      // Periodic children need a parent job,
    61      // It is the Periodic job's responsibility to create
    62      // periodicChild jobs and provide a parent job.
    63      type: 'batch',
    64    }),
    65  
    66    parameterizedChild: trait({
    67      // Parameterized children need a parent job,
    68      // It is the Parameterized job's responsibility to create
    69      // parameterizedChild jobs and provide a parent job.
    70      type: 'batch',
    71      parameterized: true,
    72      dispatched: true,
    73      payload: window.btoa(faker.lorem.sentence()),
    74    }),
    75  
    76    createIndex: i => i,
    77    modifyIndex: () => faker.random.number({ min: 10, max: 2000 }),
    78  
    79    // Directive used to control sub-resources
    80  
    81    // When false, no allocations are made
    82    createAllocations: true,
    83  
    84    // When true, deployments for the job will never have a 'running' status
    85    noActiveDeployment: false,
    86  
    87    // When true, deployments for the job will always have a 'running' status
    88    activeDeployment: false,
    89  
    90    // When true, the job will have no versions or deployments (and in turn no latest deployment)
    91    noDeployments: false,
    92  
    93    // When true, an evaluation with a high modify index and placement failures is created
    94    failedPlacements: false,
    95  
    96    // When true, no evaluations have failed placements
    97    noFailedPlacements: false,
    98  
    99    // When true, allocations for this job will fail and reschedule, randomly succeeding or not
   100    withRescheduling: false,
   101  
   102    // When true, task groups will have services
   103    withGroupServices: false,
   104  
   105    // When true, only task groups and allocations are made
   106    shallow: false,
   107  
   108    afterCreate(job, server) {
   109      if (!job.namespaceId) {
   110        const namespace = server.db.namespaces.length ? pickOne(server.db.namespaces).id : null;
   111        job.update({
   112          namespace,
   113          namespaceId: namespace,
   114        });
   115      } else {
   116        job.update({
   117          namespace: job.namespaceId,
   118        });
   119      }
   120  
   121      const groups = server.createList('task-group', job.groupsCount, {
   122        job,
   123        createAllocations: job.createAllocations,
   124        withRescheduling: job.withRescheduling,
   125        withServices: job.withGroupServices,
   126        shallow: job.shallow,
   127      });
   128  
   129      job.update({
   130        taskGroupIds: groups.mapBy('id'),
   131        task_group_ids: groups.mapBy('id'),
   132      });
   133  
   134      const hasChildren = job.periodic || (job.parameterized && !job.parentId);
   135      const jobSummary = server.create('job-summary', hasChildren ? 'withChildren' : 'withSummary', {
   136        groupNames: groups.mapBy('name'),
   137        job,
   138        job_id: job.id,
   139        JobID: job.id,
   140        namespace: job.namespace,
   141      });
   142  
   143      job.update({
   144        jobSummaryId: jobSummary.id,
   145        job_summary_id: jobSummary.id,
   146      });
   147  
   148      if (!job.noDeployments) {
   149        Array(faker.random.number({ min: 1, max: 3 }))
   150          .fill(null)
   151          .map((_, index) => {
   152            return server.create('job-version', {
   153              job,
   154              namespace: job.namespace,
   155              version: index,
   156              noActiveDeployment: job.noActiveDeployment,
   157              activeDeployment: job.activeDeployment,
   158            });
   159          });
   160      }
   161  
   162      if (!job.shallow) {
   163        const knownEvaluationProperties = {
   164          job,
   165          namespace: job.namespace,
   166        };
   167        server.createList(
   168          'evaluation',
   169          faker.random.number({ min: 1, max: 5 }),
   170          knownEvaluationProperties
   171        );
   172        if (!job.noFailedPlacements) {
   173          server.createList(
   174            'evaluation',
   175            faker.random.number(3),
   176            'withPlacementFailures',
   177            knownEvaluationProperties
   178          );
   179        }
   180  
   181        if (job.failedPlacements) {
   182          server.create(
   183            'evaluation',
   184            'withPlacementFailures',
   185            assign(knownEvaluationProperties, {
   186              modifyIndex: 4000,
   187            })
   188          );
   189        }
   190      }
   191  
   192      if (job.periodic) {
   193        // Create periodicChild jobs
   194        server.createList('job', job.childrenCount, 'periodicChild', {
   195          parentId: job.id,
   196          namespaceId: job.namespaceId,
   197          namespace: job.namespace,
   198          createAllocations: job.createAllocations,
   199          shallow: job.shallow,
   200        });
   201      }
   202  
   203      if (job.parameterized && !job.parentId) {
   204        // Create parameterizedChild jobs
   205        server.createList('job', job.childrenCount, 'parameterizedChild', {
   206          parentId: job.id,
   207          namespaceId: job.namespaceId,
   208          namespace: job.namespace,
   209          createAllocations: job.createAllocations,
   210          shallow: job.shallow,
   211        });
   212      }
   213    },
   214  });