/
StartNextGroupRolloutGroupSuccessAction.java
88 lines (78 loc) · 4.05 KB
/
StartNextGroupRolloutGroupSuccessAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.jpa.rollout.condition;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.jpa.repository.RolloutGroupRepository;
import org.eclipse.hawkbit.repository.jpa.model.JpaRolloutGroup;
import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.RolloutGroup;
import org.eclipse.hawkbit.repository.model.RolloutGroup.RolloutGroupStatus;
import org.eclipse.hawkbit.security.SystemSecurityContext;
/**
* Success action which starts the next following {@link RolloutGroup}.
*/
@Slf4j
public class StartNextGroupRolloutGroupSuccessAction implements RolloutGroupActionEvaluator<RolloutGroup.RolloutGroupSuccessAction> {
private final RolloutGroupRepository rolloutGroupRepository;
private final DeploymentManagement deploymentManagement;
private final SystemSecurityContext systemSecurityContext;
public StartNextGroupRolloutGroupSuccessAction(final RolloutGroupRepository rolloutGroupRepository,
final DeploymentManagement deploymentManagement, final SystemSecurityContext systemSecurityContext) {
this.rolloutGroupRepository = rolloutGroupRepository;
this.deploymentManagement = deploymentManagement;
this.systemSecurityContext = systemSecurityContext;
}
@Override
public RolloutGroup.RolloutGroupSuccessAction getAction() {
return RolloutGroup.RolloutGroupSuccessAction.NEXTGROUP;
}
@Override
public void exec(final Rollout rollout, final RolloutGroup rolloutGroup) {
systemSecurityContext.runAsSystem(() -> {
startNextGroup(rollout, rolloutGroup);
return null;
});
}
private void startNextGroup(final Rollout rollout, final RolloutGroup rolloutGroup) {
// retrieve all actions according to the parent group of the finished
// rolloutGroup, so retrieve all child-group actions which need to be
// started.
final long countOfStartedActions = deploymentManagement.startScheduledActionsByRolloutGroupParent(
rollout.getId(), rollout.getDistributionSet().getId(), rolloutGroup.getId());
log.debug("{} Next actions started for rollout {} and parent group {}", countOfStartedActions, rollout,
rolloutGroup);
if (countOfStartedActions > 0) {
// get all next scheduled groups and set them in state running
rolloutGroupRepository.setStatusForCildren(RolloutGroupStatus.RUNNING, rolloutGroup);
} else {
log.debug("No actions to start for next rolloutgroup of parent {} {}", rolloutGroup.getId(),
rolloutGroup.getName());
// nothing for next group, just finish the group, this can happen
// e.g. if targets has been deleted after the group has been
// scheduled. If the group is empty now, we just finish the group if
// there are not actions available for this group.
final List<JpaRolloutGroup> findByRolloutGroupParent = rolloutGroupRepository
.findByParentIdAndStatus(rolloutGroup.getId(), RolloutGroupStatus.SCHEDULED);
findByRolloutGroupParent.forEach(nextGroup -> {
if (nextGroup.isDynamic()) {
nextGroup.setStatus(RolloutGroupStatus.RUNNING);
} else {
log.debug("Rolloutgroup {} is finished, starting next group", nextGroup);
nextGroup.setStatus(RolloutGroupStatus.FINISHED);
rolloutGroupRepository.save(nextGroup);
// find the next group to set in running state
startNextGroup(rollout, nextGroup);
}
});
}
}
}