github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/ui/mirage/factories/task-group.js (about)

     1  import { Factory, faker } from 'ember-cli-mirage';
     2  
     3  const DISK_RESERVATIONS = [200, 500, 1000, 2000, 5000, 10000, 100000];
     4  
     5  export default Factory.extend({
     6    name: id => `${faker.hacker.noun().dasherize()}-g-${id}`,
     7    count: () => faker.random.number({ min: 1, max: 2 }),
     8  
     9    ephemeralDisk: () => ({
    10      Sticky: faker.random.boolean(),
    11      SizeMB: faker.random.arrayElement(DISK_RESERVATIONS),
    12      Migrate: faker.random.boolean(),
    13    }),
    14  
    15    // Directive used to control whether or not allocations are automatically
    16    // created.
    17    createAllocations: true,
    18  
    19    // Directived used to control whether or not the allocation should fail
    20    // and reschedule, creating reschedule events.
    21    withRescheduling: false,
    22  
    23    // When true, only creates allocations
    24    shallow: false,
    25  
    26    afterCreate(group, server) {
    27      let taskIds = [];
    28  
    29      if (!group.shallow) {
    30        const tasks = server.createList('task', group.count, {
    31          taskGroup: group,
    32        });
    33        taskIds = tasks.mapBy('id');
    34      }
    35  
    36      group.update({
    37        taskIds: taskIds,
    38        task_ids: taskIds,
    39      });
    40  
    41      if (group.createAllocations) {
    42        Array(group.count)
    43          .fill(null)
    44          .forEach((_, i) => {
    45            const props = {
    46              jobId: group.job.id,
    47              namespace: group.job.namespace,
    48              taskGroup: group.name,
    49              name: `${group.name}.[${i}]`,
    50              rescheduleSuccess: group.withRescheduling ? faker.random.boolean() : null,
    51              rescheduleAttempts: group.withRescheduling
    52                ? faker.random.number({ min: 1, max: 5 })
    53                : 0,
    54            };
    55  
    56            if (group.withRescheduling) {
    57              server.create('allocation', 'rescheduled', props);
    58            } else {
    59              server.create('allocation', props);
    60            }
    61          });
    62      }
    63    },
    64  });