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