github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/ui/mirage/factories/allocation.js (about)

     1  import Ember from 'ember';
     2  import moment from 'moment';
     3  import { Factory, faker, trait } from 'ember-cli-mirage';
     4  import { provide, pickOne } from '../utils';
     5  
     6  const UUIDS = provide(100, faker.random.uuid.bind(faker.random));
     7  const CLIENT_STATUSES = ['pending', 'running', 'complete', 'failed', 'lost'];
     8  const DESIRED_STATUSES = ['run', 'stop', 'evict'];
     9  const REF_TIME = new Date();
    10  
    11  export default Factory.extend({
    12    id: i => (i >= 100 ? `${UUIDS[i % 100]}-${i}` : UUIDS[i]),
    13  
    14    jobVersion: () => faker.random.number(10),
    15  
    16    modifyIndex: () => faker.random.number({ min: 10, max: 2000 }),
    17    modifyTime: () => faker.date.past(2 / 365, REF_TIME) * 1000000,
    18  
    19    createIndex: () => faker.random.number({ min: 10, max: 2000 }),
    20    createTime() {
    21      return faker.date.past(2 / 365, new Date(this.modifyTime / 1000000)) * 1000000;
    22    },
    23  
    24    namespace: null,
    25  
    26    clientStatus: faker.list.random(...CLIENT_STATUSES),
    27    desiredStatus: faker.list.random(...DESIRED_STATUSES),
    28  
    29    withTaskWithPorts: trait({
    30      afterCreate(allocation, server) {
    31        const taskGroup = server.db.taskGroups.findBy({ name: allocation.taskGroup });
    32        const resources = taskGroup.taskIds.map(id =>
    33          server.create(
    34            'task-resources',
    35            {
    36              allocation,
    37              name: server.db.tasks.find(id).name,
    38            },
    39            'withReservedPorts'
    40          )
    41        );
    42  
    43        allocation.update({ taskResourcesIds: resources.mapBy('id') });
    44      },
    45    }),
    46  
    47    withoutTaskWithPorts: trait({
    48      afterCreate(allocation, server) {
    49        const taskGroup = server.db.taskGroups.findBy({ name: allocation.taskGroup });
    50        const resources = taskGroup.taskIds.map(id =>
    51          server.create(
    52            'task-resources',
    53            {
    54              allocation,
    55              name: server.db.tasks.find(id).name,
    56            },
    57            'withoutReservedPorts'
    58          )
    59        );
    60  
    61        allocation.update({ taskResourcesIds: resources.mapBy('id') });
    62      },
    63    }),
    64  
    65    rescheduleAttempts: 0,
    66    rescheduleSuccess: false,
    67  
    68    rescheduled: trait({
    69      // Create another allocation carrying the events of this as well as the reschduleSuccess state.
    70      // Pass along rescheduleAttempts after decrementing.
    71      // After rescheduleAttempts hits zero, a final allocation is made with no nextAllocation and
    72      // a clientStatus of failed or running, depending on rescheduleSuccess
    73      afterCreate(allocation, server) {
    74        const attempts = allocation.rescheduleAttempts - 1;
    75        const previousEvents =
    76          (allocation.rescheduleTracker && allocation.rescheduleTracker.Events) || [];
    77  
    78        let rescheduleTime;
    79        if (previousEvents.length) {
    80          const lastEvent = previousEvents[previousEvents.length - 1];
    81          rescheduleTime = moment(lastEvent.RescheduleTime / 1000000).add(5, 'minutes');
    82        } else {
    83          rescheduleTime = faker.date.past(2 / 365, REF_TIME);
    84        }
    85  
    86        rescheduleTime *= 1000000;
    87  
    88        const rescheduleTracker = {
    89          Events: previousEvents.concat([
    90            {
    91              PrevAllocID: allocation.id,
    92              PrevNodeID: null, //allocation.node.id,
    93              RescheduleTime: rescheduleTime,
    94            },
    95          ]),
    96        };
    97  
    98        let nextAllocation;
    99        if (attempts > 0) {
   100          nextAllocation = server.create('allocation', 'rescheduled', {
   101            rescheduleAttempts: Math.max(attempts, 0),
   102            rescheduleSuccess: allocation.rescheduleSuccess,
   103            previousAllocation: allocation.id,
   104            clientStatus: 'failed',
   105            rescheduleTracker,
   106            followupEvalId: server.create('evaluation', {
   107              waitUntil: rescheduleTime,
   108            }).id,
   109          });
   110        } else {
   111          nextAllocation = server.create('allocation', {
   112            previousAllocation: allocation.id,
   113            clientStatus: allocation.rescheduleSuccess ? 'running' : 'failed',
   114            rescheduleTracker,
   115          });
   116        }
   117  
   118        allocation.update({ nextAllocation: nextAllocation.id, clientStatus: 'failed' });
   119      },
   120    }),
   121  
   122    afterCreate(allocation, server) {
   123      Ember.assert(
   124        '[Mirage] No jobs! make sure jobs are created before allocations',
   125        server.db.jobs.length
   126      );
   127      Ember.assert(
   128        '[Mirage] No nodes! make sure nodes are created before allocations',
   129        server.db.nodes.length
   130      );
   131  
   132      const job = allocation.jobId ? server.db.jobs.find(allocation.jobId) : pickOne(server.db.jobs);
   133      const namespace = allocation.namespace || job.namespace;
   134      const node = allocation.nodeId
   135        ? server.db.nodes.find(allocation.nodeId)
   136        : pickOne(server.db.nodes);
   137      const taskGroup = allocation.taskGroup
   138        ? server.db.taskGroups.findBy({ name: allocation.taskGroup })
   139        : pickOne(server.db.taskGroups.where({ jobId: job.id }));
   140  
   141      const states = taskGroup.taskIds.map(id =>
   142        server.create('task-state', {
   143          allocation,
   144          name: server.db.tasks.find(id).name,
   145        })
   146      );
   147  
   148      const resources = taskGroup.taskIds.map(id =>
   149        server.create('task-resources', {
   150          allocation,
   151          name: server.db.tasks.find(id).name,
   152        })
   153      );
   154  
   155      allocation.update({
   156        namespace,
   157        jobId: job.id,
   158        nodeId: node.id,
   159        taskStateIds: states.mapBy('id'),
   160        task_state_ids: states.mapBy('id'),
   161        taskResourcesIds: resources.mapBy('id'),
   162        taskGroup: taskGroup.name,
   163        name: allocation.name || `${taskGroup.name}.[${faker.random.number(10)}]`,
   164      });
   165  
   166      // Each allocation has a corresponding allocation stats running on some client.
   167      // Create that record, even though it's not a relationship.
   168      server.create('client-allocation-stats', {
   169        id: allocation.id,
   170        _tasks: states.mapBy('name'),
   171      });
   172    },
   173  });