77import com .flowci .core .common .manager .SpringEventManager ;
88import com .flowci .core .common .rabbit .RabbitOperations ;
99import com .flowci .core .job .dao .JobDao ;
10+ import com .flowci .core .job .domain .Executed ;
1011import com .flowci .core .job .domain .Job ;
1112import com .flowci .core .job .domain .Step ;
1213import com .flowci .core .job .event .JobReceivedEvent ;
2930import com .flowci .zookeeper .InterLock ;
3031import com .flowci .zookeeper .ZookeeperClient ;
3132import com .google .common .base .Strings ;
33+ import groovy .util .ScriptException ;
3234import lombok .Getter ;
3335import lombok .Setter ;
3436import lombok .experimental .Accessors ;
4244import java .nio .file .Files ;
4345import java .nio .file .Path ;
4446import java .nio .file .Paths ;
45- import java .util .Base64 ;
46- import java .util .List ;
47- import java .util .Objects ;
48- import java .util .Optional ;
47+ import java .util .*;
4948import java .util .function .Consumer ;
5049import java .util .function .Function ;
5150
@@ -120,6 +119,9 @@ public class JobActionManagerImpl implements JobActionManager {
120119 @ Autowired
121120 private RabbitOperations jobsQueueManager ;
122121
122+ @ Autowired
123+ private ConditionManager conditionManager ;
124+
123125 @ Autowired
124126 private LocalTaskService localTaskService ;
125127
@@ -328,7 +330,7 @@ public void accept(JobSmContext context) {
328330
329331 Sm .add (QueuedToRunning , new Action <JobSmContext >() {
330332 @ Override
331- public void accept (JobSmContext context ) {
333+ public void accept (JobSmContext context ) throws Exception {
332334 Job job = context .job ;
333335 eventManager .publish (new JobReceivedEvent (this , job ));
334336
@@ -387,7 +389,7 @@ public boolean canRun(JobSmContext context) {
387389 }
388390
389391 @ Override
390- public void accept (JobSmContext context ) {
392+ public void accept (JobSmContext context ) throws Exception {
391393 Job job = context .job ;
392394 log .debug ("Job {} is locked" , job .getId ());
393395
@@ -618,7 +620,7 @@ private SimpleSecret getSimpleSecret(String credentialName) {
618620 /**
619621 * Dispatch job to agent when Queue to Running
620622 */
621- private void dispatch (Job job , Agent agent ) {
623+ private void dispatch (Job job , Agent agent ) throws ScriptException {
622624 NodeTree tree = ymlManager .getTree (job );
623625 StepNode next = tree .next (NodePath .create (job .getCurrentPath ()));
624626
@@ -636,13 +638,30 @@ private void dispatch(Job job, Agent agent) {
636638 job .setAgentId (agent .getId ());
637639 job .setAgentSnapshot (agent );
638640
639- // set executed cmd step to running
640- Step nextStep = stepService .toStatus (job .getId (), nextPath , Step .Status .RUNNING , null );
641-
642641 // dispatch job to agent queue
642+ Step nextStep = stepService .get (job .getId (), nextPath );
643643 CmdIn cmd = cmdManager .createShellCmd (job , nextStep , tree );
644- setJobStatusAndSave (job , Job .Status .RUNNING , null );
644+ boolean canExecute = conditionManager .run (cmd );
645+
646+ if (!canExecute ) {
647+ nextStep .setStartAt (new Date ());
648+ nextStep .setFinishAt (new Date ());
649+ updateJobTime (job , nextStep , tree , next );
650+
651+ setJobStatusAndSave (job , Job .Status .RUNNING , null );
652+ stepService .toStatus (nextStep , Executed .Status .SKIPPED , null );
653+
654+ JobSmContext context = new JobSmContext ();
655+ context .job = job ;
656+ context .step = nextStep ;
657+ context .agentId = agent .getId ();
658+
659+ this .toNextStep (context );
660+ return ;
661+ }
645662
663+ setJobStatusAndSave (job , Job .Status .RUNNING , null );
664+ stepService .toStatus (nextStep , Executed .Status .RUNNING , null );
646665 agentService .dispatch (cmd , agent );
647666 logInfo (job , "send to agent: step={}, agent={}" , next .getName (), agent .getName ());
648667 }
@@ -652,9 +671,9 @@ private void dispatch(Job job, Agent agent) {
652671 *
653672 * @return true if next step dispatched, false if no more steps or failure
654673 */
655- private boolean toNextStep (JobSmContext context ) {
674+ private boolean toNextStep (JobSmContext context ) throws ScriptException {
656675 Job job = context .job ;
657- Step step = context .step ;
676+ Step step = context .step ; // current step
658677
659678 // save executed cmd
660679 stepService .resultUpdate (step );
@@ -677,15 +696,29 @@ private boolean toNextStep(JobSmContext context) {
677696 Optional <StepNode > next = findNext (tree , node , step .isSuccess ());
678697 if (next .isPresent ()) {
679698 String nextPath = next .get ().getPathAsString ();
680- job .setCurrentPath (nextPath );
681-
682- Step nextStep = stepService .toStatus (job .getId (), nextPath , Step .Status .RUNNING , null );
699+ Step nextStep = stepService .get (job .getId (), nextPath );
683700 context .setStep (nextStep );
684701
702+ job .setCurrentPath (nextPath );
703+ setJobStatusAndSave (job , Job .Status .RUNNING , null );
704+
685705 Agent agent = agentService .get (job .getAgentId ());
686706 CmdIn cmd = cmdManager .createShellCmd (job , nextStep , tree );
687- setJobStatusAndSave (job , Job .Status .RUNNING , null );
707+ boolean canExecute = conditionManager .run (cmd );
708+
709+ if (!canExecute ) {
710+ nextStep .setStartAt (new Date ());
711+ nextStep .setFinishAt (new Date ());
712+ updateJobTime (job , nextStep , tree , next .get ());
688713
714+ setJobStatusAndSave (job , Job .Status .RUNNING , null );
715+ stepService .toStatus (nextStep , Step .Status .SKIPPED , null );
716+ return toNextStep (context );
717+ }
718+
719+ // TODO: run condition
720+ setJobStatusAndSave (job , Job .Status .RUNNING , null );
721+ stepService .toStatus (nextStep , Step .Status .RUNNING , null );
689722 agentService .dispatch (cmd , agent );
690723 logInfo (job , "send to agent: step={}, agent={}" , next .get ().getName (), agent .getName ());
691724 return true ;
@@ -722,11 +755,11 @@ private synchronized void setJobStatusAndSave(Job job, Job.Status newStatus, Str
722755 logInfo (job , "status = {}" , job .getStatus ());
723756 }
724757
725- private void updateJobTime (Job job , Step execCmd , NodeTree tree , StepNode node ) {
758+ private void updateJobTime (Job job , Step step , NodeTree tree , StepNode node ) {
726759 if (tree .isFirst (node .getPath ())) {
727- job .setStartAt (execCmd .getStartAt ());
760+ job .setStartAt (step .getStartAt ());
728761 }
729- job .setFinishAt (execCmd .getFinishAt ());
762+ job .setFinishAt (step .getFinishAt ());
730763 }
731764
732765 private void updateJobContextAndLatestStatus (Job job , StepNode node , Step cmd ) {
0 commit comments