fix(plugin-workflow): use promise to request (#1426)

This commit is contained in:
Junyi 2023-02-04 16:03:25 +08:00 committed by GitHub
parent a11c4bbe48
commit 3115134f3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,12 +15,7 @@ export type RequestConfig = Pick<AxiosRequestConfig, 'url' | 'method' | 'params'
ignoreFail: boolean; ignoreFail: boolean;
}; };
export default class implements Instruction { async function request(config) {
constructor(public plugin) {}
request = async (node: FlowNodeModel, job, processor: Processor) => {
const config = processor.getParsedValue(node.config) as RequestConfig;
// default headers // default headers
const { url, method = 'POST', data, timeout = 5000 } = config; const { url, method = 'POST', data, timeout = 5000 } = config;
const headers = (config.headers ?? []).reduce((result, header) => { const headers = (config.headers ?? []).reduce((result, header) => {
@ -34,8 +29,7 @@ export default class implements Instruction {
// TODO(feat): only support JSON type for now, should support others in future // TODO(feat): only support JSON type for now, should support others in future
headers['Content-Type'] = 'application/json'; headers['Content-Type'] = 'application/json';
try { return axios.request({
const response = await axios.request({
url, url,
method, method,
headers, headers,
@ -43,34 +37,43 @@ export default class implements Instruction {
data, data,
timeout, timeout,
}); });
job.set({
status: JOB_STATUS.RESOLVED,
result: response.data
});
} catch (error) {
job.set({
status: JOB_STATUS.REJECTED,
result: error.isAxiosError ? error.toJSON() : error.message
});
}
return this.plugin.resume(job);
}; };
async run(node, input, processor) { export default class implements Instruction {
constructor(public plugin) {}
async run(node: FlowNodeModel, input, processor: Processor) {
const job = await processor.saveJob({ const job = await processor.saveJob({
status: JOB_STATUS.PENDING, status: JOB_STATUS.PENDING,
nodeId: node.id, nodeId: node.id,
}); });
setTimeout(() => { const config = processor.getParsedValue(node.config) as RequestConfig;
this.request(node, job, processor);
request(config)
.then(response => {
job.set({
status: JOB_STATUS.RESOLVED,
result: response.data
}); });
})
.catch(error => {
job.set({
status: JOB_STATUS.REJECTED,
result: error.isAxiosError ? error.toJSON() : error.message
});
})
.finally(() => {
this.plugin.app.logger.info(`[Workflow] request (#${node.id}) response received, status: ${job.get('status')}`);
this.plugin.resume(job);
});
this.plugin.app.logger.info(`[Workflow] request (#${node.id}) sent to "${config.url}", waiting for response...`);
return job; return job;
} }
async resume(node, job, processor) { async resume(node: FlowNodeModel, job, processor: Processor) {
const { ignoreFail } = node.config as RequestConfig; const { ignoreFail } = node.config as RequestConfig;
if (ignoreFail) { if (ignoreFail) {
job.set('status', JOB_STATUS.RESOLVED); job.set('status', JOB_STATUS.RESOLVED);