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