fix(plugin-workflow): ts errors
This commit is contained in:
parent
37be46aacb
commit
4f420c670f
@ -1,18 +1,12 @@
|
|||||||
import {
|
import { Database, Model } from '@nocobase/database';
|
||||||
Model,
|
|
||||||
BelongsToGetAssociationMixin,
|
|
||||||
HasManyGetAssociationsMixin,
|
|
||||||
Transaction
|
|
||||||
} from 'sequelize';
|
|
||||||
import parse from 'json-templates';
|
import parse from 'json-templates';
|
||||||
|
import { BelongsToGetAssociationMixin, HasManyGetAssociationsMixin, Transaction } from 'sequelize';
|
||||||
import Database from '@nocobase/database';
|
|
||||||
|
|
||||||
import { EXECUTION_STATUS, JOB_STATUS } from '../constants';
|
import { EXECUTION_STATUS, JOB_STATUS } from '../constants';
|
||||||
import { getInstruction } from '../instructions';
|
import { getInstruction } from '../instructions';
|
||||||
import WorkflowModel from './Workflow';
|
|
||||||
import FlowNodeModel from './FlowNode';
|
import FlowNodeModel from './FlowNode';
|
||||||
import JobModel from './Job';
|
import JobModel from './Job';
|
||||||
|
import WorkflowModel from './Workflow';
|
||||||
|
|
||||||
|
|
||||||
export interface ExecutionOptions {
|
export interface ExecutionOptions {
|
||||||
transaction?: Transaction;
|
transaction?: Transaction;
|
||||||
@ -54,11 +48,11 @@ export default class ExecutionModel extends Model {
|
|||||||
makeNodes(nodes = []) {
|
makeNodes(nodes = []) {
|
||||||
this.nodes = nodes;
|
this.nodes = nodes;
|
||||||
|
|
||||||
nodes.forEach(node => {
|
nodes.forEach((node) => {
|
||||||
this.nodesMap.set(node.id, node);
|
this.nodesMap.set(node.id, node);
|
||||||
});
|
});
|
||||||
|
|
||||||
nodes.forEach(node => {
|
nodes.forEach((node) => {
|
||||||
if (node.upstreamId) {
|
if (node.upstreamId) {
|
||||||
node.upstream = this.nodesMap.get(node.upstreamId);
|
node.upstream = this.nodesMap.get(node.upstreamId);
|
||||||
}
|
}
|
||||||
@ -70,7 +64,7 @@ export default class ExecutionModel extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
makeJobs(jobs: Array<JobModel>) {
|
makeJobs(jobs: Array<JobModel>) {
|
||||||
jobs.forEach(job => {
|
jobs.forEach((job) => {
|
||||||
this.jobsMap.set(job.id, job);
|
this.jobsMap.set(job.id, job);
|
||||||
// TODO: should consider cycle, and from previous job
|
// TODO: should consider cycle, and from previous job
|
||||||
this.jobsMapByNodeId[job.nodeId] = job.result;
|
this.jobsMapByNodeId[job.nodeId] = job.result;
|
||||||
@ -79,7 +73,8 @@ export default class ExecutionModel extends Model {
|
|||||||
|
|
||||||
async prepare(options, commit = false) {
|
async prepare(options, commit = false) {
|
||||||
this.options = options || {};
|
this.options = options || {};
|
||||||
const { transaction = await (<typeof ExecutionModel>this.constructor).database.sequelize.transaction() } = this.options;
|
const { transaction = await (<typeof ExecutionModel>this.constructor).database.sequelize.transaction() } =
|
||||||
|
this.options;
|
||||||
this.transaction = transaction;
|
this.transaction = transaction;
|
||||||
|
|
||||||
if (!this.workflow) {
|
if (!this.workflow) {
|
||||||
@ -92,7 +87,7 @@ export default class ExecutionModel extends Model {
|
|||||||
|
|
||||||
const jobs = await this.getJobs({
|
const jobs = await this.getJobs({
|
||||||
order: [['id', 'ASC']],
|
order: [['id', 'ASC']],
|
||||||
transaction
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.makeJobs(jobs);
|
this.makeJobs(jobs);
|
||||||
@ -108,7 +103,7 @@ export default class ExecutionModel extends Model {
|
|||||||
}
|
}
|
||||||
await this.prepare(options);
|
await this.prepare(options);
|
||||||
if (this.nodes.length) {
|
if (this.nodes.length) {
|
||||||
const head = this.nodes.find(item => !item.upstream);
|
const head = this.nodes.find((item) => !item.upstream);
|
||||||
await this.exec(head, { result: this.context });
|
await this.exec(head, { result: this.context });
|
||||||
} else {
|
} else {
|
||||||
await this.exit(null);
|
await this.exit(null);
|
||||||
@ -144,7 +139,7 @@ export default class ExecutionModel extends Model {
|
|||||||
// for uncaught error, set to rejected
|
// for uncaught error, set to rejected
|
||||||
job = {
|
job = {
|
||||||
result: err instanceof Error ? err.toString() : err,
|
result: err instanceof Error ? err.toString() : err,
|
||||||
status: JOB_STATUS.REJECTED
|
status: JOB_STATUS.REJECTED,
|
||||||
};
|
};
|
||||||
// if previous job is from resuming
|
// if previous job is from resuming
|
||||||
if (prevJob && prevJob.nodeId === node.id) {
|
if (prevJob && prevJob.nodeId === node.id) {
|
||||||
@ -157,13 +152,13 @@ export default class ExecutionModel extends Model {
|
|||||||
// TODO(optimize): many checking of resuming or new could be improved
|
// TODO(optimize): many checking of resuming or new could be improved
|
||||||
// could be implemented separately in exec() / resume()
|
// could be implemented separately in exec() / resume()
|
||||||
if (job instanceof Model) {
|
if (job instanceof Model) {
|
||||||
savedJob = await job.save({ transaction: this.transaction }) as JobModel;
|
savedJob = (await job.save({ transaction: this.transaction })) as JobModel;
|
||||||
} else {
|
} else {
|
||||||
const upstreamId = prevJob instanceof Model ? prevJob.get('id') : null;
|
const upstreamId = prevJob instanceof Model ? prevJob.get('id') : null;
|
||||||
savedJob = await this.saveJob({
|
savedJob = await this.saveJob({
|
||||||
nodeId: node.id,
|
nodeId: node.id,
|
||||||
upstreamId,
|
upstreamId,
|
||||||
...job
|
...job,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,10 +209,13 @@ export default class ExecutionModel extends Model {
|
|||||||
async saveJob(payload) {
|
async saveJob(payload) {
|
||||||
const { database } = <typeof WorkflowModel>this.constructor;
|
const { database } = <typeof WorkflowModel>this.constructor;
|
||||||
const { model } = database.getCollection('jobs');
|
const { model } = database.getCollection('jobs');
|
||||||
const [job] = await model.upsert({
|
const [job] = (await model.upsert(
|
||||||
|
{
|
||||||
...payload,
|
...payload,
|
||||||
executionId: this.id
|
executionId: this.id,
|
||||||
}, { transaction: this.transaction }) as [JobModel, boolean | null];
|
},
|
||||||
|
{ transaction: this.transaction },
|
||||||
|
)) as [JobModel, boolean | null];
|
||||||
this.jobsMap.set(job.id, job);
|
this.jobsMap.set(job.id, job);
|
||||||
this.jobsMapByNodeId[job.nodeId] = job.result;
|
this.jobsMapByNodeId[job.nodeId] = job.result;
|
||||||
|
|
||||||
@ -256,7 +254,7 @@ export default class ExecutionModel extends Model {
|
|||||||
getParsedValue(value) {
|
getParsedValue(value) {
|
||||||
return parse(value)({
|
return parse(value)({
|
||||||
$context: this.context,
|
$context: this.context,
|
||||||
$jobsMapByNodeId: this.jobsMapByNodeId
|
$jobsMapByNodeId: this.jobsMapByNodeId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { Model, BelongsToGetAssociationMixin } from 'sequelize';
|
import { Database, Model } from '@nocobase/database';
|
||||||
import Database from '@nocobase/database';
|
import { BelongsToGetAssociationMixin } from 'sequelize';
|
||||||
|
|
||||||
import WorkflowModel from './Workflow';
|
import WorkflowModel from './Workflow';
|
||||||
|
|
||||||
|
|
||||||
export default class FlowNodeModel extends Model {
|
export default class FlowNodeModel extends Model {
|
||||||
declare static readonly database: Database;
|
declare static readonly database: Database;
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { Model, BelongsToGetAssociationMixin } from 'sequelize';
|
import { Model } from '@nocobase/database';
|
||||||
|
import { BelongsToGetAssociationMixin } from 'sequelize';
|
||||||
import FlowNodeModel from './FlowNode';
|
import FlowNodeModel from './FlowNode';
|
||||||
|
|
||||||
export default class JobModel extends Model {
|
export default class JobModel extends Model {
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import { Model, HasManyGetAssociationsMixin, HasManyCreateAssociationMixin } from 'sequelize';
|
import { Database, Model } from '@nocobase/database';
|
||||||
|
import { HasManyCreateAssociationMixin, HasManyGetAssociationsMixin } from 'sequelize';
|
||||||
import Database from '@nocobase/database';
|
|
||||||
|
|
||||||
import { get as getTrigger } from '../triggers';
|
|
||||||
import { EXECUTION_STATUS } from '../constants';
|
import { EXECUTION_STATUS } from '../constants';
|
||||||
|
import { get as getTrigger } from '../triggers';
|
||||||
import ExecutionModel from './Execution';
|
import ExecutionModel from './Execution';
|
||||||
import FlowNodeModel from './FlowNode';
|
import FlowNodeModel from './FlowNode';
|
||||||
|
|
||||||
@ -31,7 +29,7 @@ export default class WorkflowModel extends Model {
|
|||||||
static async mount() {
|
static async mount() {
|
||||||
const collection = this.database.getCollection('workflows');
|
const collection = this.database.getCollection('workflows');
|
||||||
const workflows = await collection.repository.find({
|
const workflows = await collection.repository.find({
|
||||||
filter: { enabled: true }
|
filter: { enabled: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
workflows.forEach((workflow: WorkflowModel) => {
|
workflows.forEach((workflow: WorkflowModel) => {
|
||||||
@ -65,7 +63,7 @@ export default class WorkflowModel extends Model {
|
|||||||
|
|
||||||
const execution = await this.createExecution({
|
const execution = await this.createExecution({
|
||||||
context,
|
context,
|
||||||
status: EXECUTION_STATUS.STARTED
|
status: EXECUTION_STATUS.STARTED,
|
||||||
});
|
});
|
||||||
|
|
||||||
execution.workflow = this;
|
execution.workflow = this;
|
||||||
|
Loading…
Reference in New Issue
Block a user