github.com/hernad/nomad@v1.6.112/ui/app/components/job-page/parts/title.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  // @ts-check
     7  import Component from '@ember/component';
     8  import { task } from 'ember-concurrency';
     9  import { inject as service } from '@ember/service';
    10  import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
    11  import { tagName } from '@ember-decorators/component';
    12  import classic from 'ember-classic-decorator';
    13  
    14  @classic
    15  @tagName('')
    16  export default class Title extends Component {
    17    @service router;
    18    @service notifications;
    19  
    20    job = null;
    21    title = null;
    22  
    23    handleError() {}
    24  
    25    /**
    26     * @param {boolean} withNotifications - Whether to show a toast on success, as when triggered by keyboard shortcut
    27     */
    28    @task(function* (withNotifications = false) {
    29      try {
    30        const job = this.job;
    31        yield job.stop();
    32        // Eagerly update the job status to avoid flickering
    33        job.set('status', 'dead');
    34        if (withNotifications) {
    35          this.notifications.add({
    36            title: 'Job Stopped',
    37            message: `${job.name} has been stopped`,
    38            color: 'success',
    39          });
    40        }
    41      } catch (err) {
    42        this.handleError({
    43          title: 'Could Not Stop Job',
    44          description: messageFromAdapterError(err, 'stop jobs'),
    45        });
    46      }
    47    })
    48    stopJob;
    49  
    50    @task(function* () {
    51      try {
    52        const job = this.job;
    53        yield job.purge();
    54        this.notifications.add({
    55          title: 'Job Purged',
    56          message: `You have purged ${job.name}`,
    57          color: 'success',
    58        });
    59        this.router.transitionTo('jobs');
    60      } catch (err) {
    61        this.handleError({
    62          title: 'Error purging job',
    63          description: messageFromAdapterError(err, 'purge jobs'),
    64        });
    65      }
    66    })
    67    purgeJob;
    68  
    69    /**
    70     * @param {boolean} withNotifications - Whether to show a toast on success, as when triggered by keyboard shortcut
    71     */
    72    @task(function* (withNotifications = false) {
    73      const job = this.job;
    74      const definition = yield job.fetchRawDefinition();
    75  
    76      delete definition.Stop;
    77      job.set('_newDefinition', JSON.stringify(definition));
    78  
    79      try {
    80        yield job.parse();
    81        yield job.update();
    82        // Eagerly update the job status to avoid flickering
    83        job.set('status', 'running');
    84        if (withNotifications) {
    85          this.notifications.add({
    86            title: 'Job Started',
    87            message: `${job.name} has started`,
    88            color: 'success',
    89          });
    90        }
    91      } catch (err) {
    92        this.handleError({
    93          title: 'Could Not Start Job',
    94          description: messageFromAdapterError(err, 'start jobs'),
    95        });
    96      }
    97    })
    98    startJob;
    99  }