github.com/hernad/nomad@v1.6.112/ui/app/routes/allocations/allocation.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import Route from '@ember/routing/route'; 7 import { inject as service } from '@ember/service'; 8 import { collect } from '@ember/object/computed'; 9 import { 10 watchRecord, 11 watchNonStoreRecords, 12 } from 'nomad-ui/utils/properties/watch'; 13 import WithWatchers from 'nomad-ui/mixins/with-watchers'; 14 import notifyError from 'nomad-ui/utils/notify-error'; 15 export default class AllocationRoute extends Route.extend(WithWatchers) { 16 @service notifications; 17 @service router; 18 @service store; 19 20 startWatchers(controller, model) { 21 if (model) { 22 controller.set('watcher', this.watch.perform(model)); 23 24 const anyGroupServicesAreNomad = !!model.taskGroup?.services?.filterBy( 25 'provider', 26 'nomad' 27 ).length; 28 29 const anyTaskServicesAreNomad = model.states 30 .mapBy('task.services') 31 .compact() 32 .map((fragmentClass) => fragmentClass.mapBy('provider')) 33 .flat() 34 .any((provider) => provider === 'nomad'); 35 36 // Conditionally Long Poll /checks endpoint if alloc has nomad services 37 if (anyGroupServicesAreNomad || anyTaskServicesAreNomad) { 38 controller.set( 39 'watchHealthChecks', 40 this.watchHealthChecks.perform(model, 'getServiceHealth', 2000) 41 ); 42 } 43 } 44 } 45 46 async model() { 47 // Preload the job for the allocation since it's required for the breadcrumb trail 48 try { 49 const [allocation] = await Promise.all([ 50 super.model(...arguments), 51 this.store.findAll('namespace'), 52 ]); 53 if (allocation.isPartial) { 54 await allocation.reload(); 55 } 56 const jobId = allocation.belongsTo('job').id(); 57 await this.store.findRecord('job', jobId); 58 return allocation; 59 } catch (e) { 60 const [allocId, transition] = arguments; 61 if (e?.errors[0]?.detail === 'alloc not found' && !!transition.from) { 62 this.notifications.add({ 63 title: `Error: Not Found`, 64 message: `Allocation of id: ${allocId} was not found.`, 65 color: 'critical', 66 sticky: true, 67 }); 68 this.goBackToReferrer(transition.from.name); 69 } else { 70 notifyError(this)(e); 71 } 72 } 73 } 74 75 goBackToReferrer(referringRoute) { 76 this.router.transitionTo(referringRoute); 77 } 78 79 @watchRecord('allocation') watch; 80 @watchNonStoreRecords('allocation') watchHealthChecks; 81 82 @collect('watch', 'watchHealthChecks') watchers; 83 }