Files
Automata/packages/editor-ui/src/components/WorkflowActivator.vue
Iván Ovejero 4c4082503c feat(core): Coordinate manual workflow activation and deactivation in multi-main scenario (#7643)
Followup to #7566 | Story: https://linear.app/n8n/issue/PAY-926

### Manual workflow activation and deactivation

In a multi-main scenario, if the user manually activates or deactivates
a workflow, the process (whether leader or follower) that handles the
PATCH request and updates its internal state should send a message into
the command channel, so that all other main processes update their
internal state accordingly:

- Add to `ActiveWorkflows` if activating
- Remove from `ActiveWorkflows` if deactivating
- Remove and re-add to `ActiveWorkflows` if the update did not change
activation status.

After updating their internal state, if activating or deactivating, the
recipient main processes should push a message to all connected
frontends so that these can update their stores and so reflect the value
in the UI.

### Workflow activation errors

On failure to activate a workflow, the main instance should record the
error in Redis - main instances should always pull activation errors
from Redis in a multi-main scenario.

### Leadership change

On leadership change...

- The old leader should stop pruning and the new leader should start
pruning.
- The old leader should remove trigger- and poller-based workflows and
the new leader should add them.
2023-11-17 15:58:50 +01:00

170 lines
4.8 KiB
Vue

<template>
<div class="workflow-activator">
<div :class="$style.activeStatusText" data-test-id="workflow-activator-status">
<n8n-text
v-if="workflowActive"
:color="couldNotBeStarted ? 'danger' : 'success'"
size="small"
bold
>
{{ $locale.baseText('workflowActivator.active') }}
</n8n-text>
<n8n-text v-else color="text-base" size="small" bold>
{{ $locale.baseText('workflowActivator.inactive') }}
</n8n-text>
</div>
<n8n-tooltip :disabled="!disabled" placement="bottom">
<template #content>
<div>{{ $locale.baseText('workflowActivator.thisWorkflowHasNoTriggerNodes') }}</div>
</template>
<el-switch
v-loading="updatingWorkflowActivation"
:modelValue="workflowActive"
@update:modelValue="activeChanged"
:title="
workflowActive
? $locale.baseText('workflowActivator.deactivateWorkflow')
: $locale.baseText('workflowActivator.activateWorkflow')
"
:disabled="disabled || updatingWorkflowActivation"
:active-color="getActiveColor"
inactive-color="#8899AA"
element-loading-spinner="el-icon-loading"
data-test-id="workflow-activate-switch"
>
</el-switch>
</n8n-tooltip>
<div class="could-not-be-started" v-if="couldNotBeStarted">
<n8n-tooltip placement="top">
<template #content>
<div
@click="displayActivationError"
v-html="$locale.baseText('workflowActivator.theWorkflowIsSetToBeActiveBut')"
></div>
</template>
<font-awesome-icon @click="displayActivationError" icon="exclamation-triangle" />
</n8n-tooltip>
</div>
</div>
</template>
<script lang="ts">
import { useToast } from '@/composables';
import { workflowActivate } from '@/mixins/workflowActivate';
import { useUIStore } from '@/stores/ui.store';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { mapStores } from 'pinia';
import { defineComponent } from 'vue';
import { getActivatableTriggerNodes } from '@/utils';
export default defineComponent({
name: 'WorkflowActivator',
props: ['workflowActive', 'workflowId'],
mixins: [workflowActivate],
setup(props) {
return {
...useToast(),
// eslint-disable-next-line @typescript-eslint/no-misused-promises
...workflowActivate.setup?.(props),
};
},
computed: {
...mapStores(useUIStore, useWorkflowsStore),
nodesIssuesExist(): boolean {
return this.workflowsStore.nodesIssuesExist;
},
isWorkflowActive(): boolean {
const activeWorkflows = this.workflowsStore.activeWorkflows;
return activeWorkflows.includes(this.workflowId);
},
couldNotBeStarted(): boolean {
return this.workflowActive === true && this.isWorkflowActive !== this.workflowActive;
},
getActiveColor(): string {
if (this.couldNotBeStarted) {
return '#ff4949';
}
return '#13ce66';
},
isCurrentWorkflow(): boolean {
return this.workflowsStore.workflowId === this.workflowId;
},
disabled(): boolean {
const isNewWorkflow = !this.workflowId;
if (isNewWorkflow || this.isCurrentWorkflow) {
return !this.workflowActive && !this.containsTrigger;
}
return false;
},
containsTrigger(): boolean {
const foundTriggers = getActivatableTriggerNodes(this.workflowsStore.workflowTriggerNodes);
return foundTriggers.length > 0;
},
},
methods: {
async activeChanged(newActiveState: boolean) {
return this.updateWorkflowActivation(this.workflowId, newActiveState);
},
async displayActivationError() {
let errorMessage: string;
try {
const errorData = await this.workflowsStore.getActivationError(this.workflowId);
if (errorData === undefined) {
errorMessage = this.$locale.baseText(
'workflowActivator.showMessage.displayActivationError.message.errorDataUndefined',
);
} else {
errorMessage = this.$locale.baseText(
'workflowActivator.showMessage.displayActivationError.message.errorDataNotUndefined',
{ interpolate: { message: errorData } },
);
}
} catch (error) {
errorMessage = this.$locale.baseText(
'workflowActivator.showMessage.displayActivationError.message.catchBlock',
);
}
this.showMessage({
title: this.$locale.baseText('workflowActivator.showMessage.displayActivationError.title'),
message: errorMessage,
type: 'warning',
duration: 0,
dangerouslyUseHTMLString: true,
});
},
},
});
</script>
<style lang="scss" module>
.activeStatusText {
width: 64px; // Required to avoid jumping when changing active state
padding-right: var(--spacing-2xs);
box-sizing: border-box;
display: inline-block;
text-align: right;
}
</style>
<style lang="scss" scoped>
.workflow-activator {
display: inline-flex;
flex-wrap: nowrap;
align-items: center;
:deep(.el-loading-spinner) {
margin-top: -10px;
}
}
.could-not-be-started {
display: inline-block;
color: var(--color-text-danger);
margin-left: 0.5em;
}
</style>