Skip to content

Commit

Permalink
feat(core/cluster): Allow task matchers to be configured from deck-cu…
Browse files Browse the repository at this point in the history
…stomized (#4909)

If a custom stage is created, this allows the stage name and matcher to be registered with a global TaskMatcher service
  • Loading branch information
christopherthielen authored and anotherchrisberry committed Feb 26, 2018
1 parent 4c435f7 commit 01096fe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app/scripts/modules/core/src/cluster/cluster.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { forOwn, get, groupBy, has, head, keys, values } from 'lodash';

import { Api, API_SERVICE } from '../api/api.service';
import { NAMING_SERVICE, NamingService } from 'core/naming/naming.service';
import { taskMatches } from './task.matcher';
import { taskMatcher } from './task.matcher';
import { IServerGroup } from 'core/domain';
import { Application } from 'core/application/application.model';
import { ICluster, IClusterSummary } from '../domain/ICluster';
Expand Down Expand Up @@ -169,7 +169,7 @@ export class ClusterService {
serverGroup.runningTasks.length = 0;
}
runningTasks.forEach(function(task) {
if (taskMatches(task, serverGroup)) {
if (taskMatcher.taskMatches(task, serverGroup)) {
serverGroup.runningTasks.push(task);
}
});
Expand Down
92 changes: 65 additions & 27 deletions app/scripts/modules/core/src/cluster/task.matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,70 @@ export interface ITaskMatcher {
(task: ITask, serverGroup: IServerGroup): boolean;
}

/**
* Match running tasks for an application to a specific server group.
* This allows the tasks to be displayed in the server group details, or
* as a popover on the server group header.
*/
export class TaskMatcher {
private customMatchers: { [type: string]: ITaskMatcher } = {
createcopylastasg: createcopylastasgMatcher,
createdeploy: createdeployMatcher,
rollbackServerGroup: rollbackServerGroupTaskMatcher,
};

private instanceIdMatchers = [
'deregisterinstancesfromloadbalancer',
'disableinstances',
'disableinstancesindiscovery',
'enableinstancesindiscovery',
'rebootinstances',
'registerinstanceswithloadbalancer',
'terminateinstances',
];

private baseTaskMatchers = [
'destroyasg',
'destroyservergroup',
'disableasg',
'disablegoogleservergroup',
'disableservergroup',
'enableasg',
'enablegoogleservergroup',
'enableservergroup',
'resizeasg',
'resizeservergroup',
'resumeasgprocessesdescription',
];

public addMatcher(stageName: string, matcher: ITaskMatcher | 'instanceIdMatchers' | 'baseTaskMatchers') {
if (typeof matcher === 'function') {
this.customMatchers[stageName] = matcher;
} else if (matcher === 'instanceIdMatchers') {
this.instanceIdMatchers.push(stageName);
} else if (matcher === 'baseTaskMatchers') {
this.baseTaskMatchers.push(stageName);
}
}

public taskMatches(task: ITask, serverGroup: IServerGroup) {
const matchers: { [type: string]: ITaskMatcher } = Object.assign({}, this.customMatchers);
this.instanceIdMatchers.forEach(m => matchers[m] = instanceIdsTaskMatcher);
this.baseTaskMatchers.forEach(m => matchers[m] = baseTaskMatcher);

const notificationType: string = has(task, 'execution.stages') ?
task.execution.stages[0].context['notification.type'] ?
task.execution.stages[0].context['notification.type'] :
task.execution.stages[0].type : // TODO: good grief
task.getValueFor('notification.type');

if (notificationType && matchers[notificationType]) {
return matchers[notificationType](task, serverGroup);
}
return false;
}
}

function createcopylastasgMatcher(task: ITask, serverGroup: IServerGroup): boolean {
const source: any = task.getValueFor('source'),
targetAccount: string = task.getValueFor('deploy.account.name'),
Expand Down Expand Up @@ -61,30 +125,4 @@ function rollbackServerGroupTaskMatcher(task: ITask, serverGroup: IServerGroup):
return false;
}

export function taskMatches(task: ITask, serverGroup: IServerGroup) {
const matchers: { [type: string]: ITaskMatcher } = {
createcopylastasg: createcopylastasgMatcher,
createdeploy: createdeployMatcher,
rollbackServerGroup: rollbackServerGroupTaskMatcher,
};

const instanceIdMatchers = ['enableinstancesindiscovery', 'disableinstancesindiscovery', 'disableinstances',
'registerinstanceswithloadbalancer', 'deregisterinstancesfromloadbalancer', 'terminateinstances', 'rebootinstances'];
instanceIdMatchers.forEach(m => matchers[m] = instanceIdsTaskMatcher);

const baseTaskMatchers = ['resizeasg', 'resizeservergroup', 'disableasg', 'disableservergroup', 'destroyasg',
'destroyservergroup', 'enableasg', 'enableservergroup', 'enablegoogleservergroup', 'disablegoogleservergroup',
'resumeasgprocessesdescription'];
baseTaskMatchers.forEach(m => matchers[m] = baseTaskMatcher);

const notificationType: string = has(task, 'execution.stages') ?
task.execution.stages[0].context['notification.type'] ?
task.execution.stages[0].context['notification.type'] :
task.execution.stages[0].type : // TODO: good grief
task.getValueFor('notification.type');

if (notificationType && matchers[notificationType]) {
return matchers[notificationType](task, serverGroup);
}
return false;
}
export const taskMatcher = new TaskMatcher();

0 comments on commit 01096fe

Please sign in to comment.