Skip to content

Commit 0cc996a

Browse files
committed
Fix a bug when saving/reading state
1 parent 278ed6d commit 0cc996a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Sources/libhostmgr/CommandPolicy.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public enum CommandPolicy: Equatable, Codable {
2727
}
2828
}
2929

30+
var label: String {
31+
switch self {
32+
case .serialExecution: return "serial-execution"
33+
case .scheduled: return "schedule"
34+
}
35+
}
36+
3037
private func evaluateSchedule(
3138
forKey key: String,
3239
timeInterval: TimeInterval,
@@ -87,15 +94,20 @@ public protocol FollowsCommandPolicies {
8794
}
8895

8996
public extension FollowsCommandPolicies {
97+
98+
func key(forPolicy policy: CommandPolicy) -> String {
99+
Self.commandIdentifier + "-" + policy.label
100+
}
101+
90102
/// Evaluate any command policies on this command
91103
///
92104
/// If any policies are violated (ie – it's not time to run yet), an error will be thrown
93105
/// that will cause the program to exit.
94106
func evaluateCommandPolicies(stateStorageManager: StateStorageManager? = nil) throws {
95107
let stateStorageManager = stateStorageManager ?? FileStateStorage()
96108

97-
try Self.commandPolicies.forEach { policy in
98-
try policy.evaluate(forKey: Self.commandIdentifier, stateStorageManager: stateStorageManager)
109+
for policy in Self.commandPolicies {
110+
try policy.evaluate(forKey: key(forPolicy: policy), stateStorageManager: stateStorageManager)
99111
}
100112
}
101113

@@ -108,7 +120,7 @@ public extension FollowsCommandPolicies {
108120
var state = CommandPolicy.SerialExecutionState()
109121
state.heartbeat = date
110122

111-
try stateStorageManager.write(state, toKey: Self.commandIdentifier)
123+
try stateStorageManager.write(state, toKey: key(forPolicy: .serialExecution))
112124
}
113125

114126
/// Store the last run date for this job
@@ -120,6 +132,6 @@ public extension FollowsCommandPolicies {
120132
var state = CommandPolicy.ScheduledCommandState()
121133
state.lastRunAt = date
122134

123-
try stateStorageManager.write(state, toKey: Self.commandIdentifier)
135+
try stateStorageManager.write(state, toKey: key(forPolicy: .scheduled(every: 0)))
124136
}
125137
}

0 commit comments

Comments
 (0)