Skip to content

Commit

Permalink
fix flow condition should check before job start
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Mar 4, 2021
1 parent 5e4289d commit 5b2251c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ private void setupJobYamlAndSteps(Job job, String yml) {
stepService.init(job);
localTaskService.init(job);

FlowNode root = YmlParser.load(yml);
FlowNode root = ymlManager.parse(yml);

job.setCurrentPathFromNodes(root);
job.getContext().merge(root.getEnvironments(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

import com.flowci.core.job.domain.Job;
import com.flowci.core.job.domain.JobYml;
import com.flowci.tree.FlowNode;
import com.flowci.tree.NodeTree;

/**
* @author yang
*/
public interface YmlManager {

FlowNode parse(String yml);


JobYml get(Job job);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public class YmlManagerImpl implements YmlManager {
@Autowired
private JobYmlDao jobYmlDao;

@Override
public FlowNode parse(String yml) {
return YmlParser.load(yml);
}

@Override
public JobYml get(Job job) {
Optional<JobYml> optional = jobYmlDao.findById(job.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.flowci.core.agent.event.AgentStatusEvent;
import com.flowci.core.agent.event.OnCmdOutEvent;
import com.flowci.core.common.config.AppProperties;
import com.flowci.core.common.manager.ConditionManager;
import com.flowci.core.common.manager.SpringEventManager;
import com.flowci.core.common.rabbit.RabbitOperations;
import com.flowci.core.flow.domain.Flow;
Expand All @@ -35,6 +36,8 @@
import com.flowci.core.job.event.CreateNewJobEvent;
import com.flowci.core.job.event.TtyStatusUpdateEvent;
import com.flowci.core.job.manager.JobActionManager;
import com.flowci.core.job.manager.YmlManager;
import com.flowci.tree.FlowNode;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
Expand Down Expand Up @@ -64,6 +67,12 @@ public class JobEventServiceImpl implements JobEventService {
@Autowired
private JobActionManager jobActionManager;

@Autowired
private ConditionManager conditionManager;

@Autowired
private YmlManager ymlManager;

@Autowired
private TaskExecutor appTaskExecutor;

Expand Down Expand Up @@ -101,8 +110,22 @@ public void onFlowDeleted(FlowDeletedEvent event) {
public void startNewJob(CreateNewJobEvent event) {
appTaskExecutor.execute(() -> {
try {
FlowNode root = ymlManager.parse(event.getYml());
boolean canCreateJob = true;

if (root.hasCondition()) {
root.getEnvironments().merge(event.getInput());
canCreateJob = conditionManager.run(root.getCondition(), root.getEnvironments());
}

if (!canCreateJob) {
log.info("Unable to create job of flow {} since condition not match", event.getFlow().getName());
return;
}

Job job = jobService.create(event.getFlow(), event.getYml(), event.getTrigger(), event.getInput());
jobService.start(job);

} catch (Throwable e) {
log.warn(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.flowci.exception.StatusException;
import com.flowci.store.FileManager;
import com.flowci.tree.FlowNode;
import com.flowci.tree.YmlParser;
import com.flowci.util.StringHelper;
import com.google.common.collect.Maps;
import lombok.extern.log4j.Log4j2;
Expand Down Expand Up @@ -210,7 +209,7 @@ public Job rerun(Flow flow, Job job) {

// load yaml
JobYml yml = ymlManager.get(job);
FlowNode root = YmlParser.load(yml.getRaw());
FlowNode root = ymlManager.parse(yml.getRaw());

// reset
job.setTimeout(flow.getStepTimeout());
Expand Down

0 comments on commit 5b2251c

Please sign in to comment.