Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 2x 2x 32x 32x 32x 32x 23x 35x 35x 33x 1x 32x 32x 32x 32x 32x 32x 1x 32x 1x 1x 14x 3x 1x 1x 1x 3x 1x 1x 2x 1x 1x 1x 16x 15x 17x 17x 17x 17x 17x 10x 10x 4x 4x 3x 3x 2x 2x 2x 2x 1x 1x 13x 2x | /**
* State Machine - 状态机实现
*
* 设计模式:状态机模式
* 来源:DeerFlow 架构学习
*/
/**
* 编排任务状态枚举
*/
const OrchestrationState = {
PENDING: 'pending',
RUNNING: 'running',
PAUSED: 'paused',
COMPLETED: 'completed',
FAILED: 'failed',
CANCELLED: 'cancelled'
};
/**
* 状态转换规则
*/
const STATE_TRANSITIONS = {
[OrchestrationState.PENDING]: [OrchestrationState.RUNNING, OrchestrationState.CANCELLED],
[OrchestrationState.RUNNING]: [OrchestrationState.COMPLETED, OrchestrationState.FAILED, OrchestrationState.PAUSED],
[OrchestrationState.PAUSED]: [OrchestrationState.RUNNING, OrchestrationState.CANCELLED],
[OrchestrationState.COMPLETED]: [], // 终态
[OrchestrationState.FAILED]: [OrchestrationState.RUNNING], // 允许重试
[OrchestrationState.CANCELLED]: [] // 终态
};
/**
* 状态机类
*/
class StateMachine {
constructor(initialState = OrchestrationState.PENDING, options = {}) {
this.state = initialState;
this.history = [{ state: initialState, timestamp: Date.now() }];
this.metadata = options.metadata || {};
this.listeners = [];
}
/**
* 获取当前状态
*/
getState() {
return this.state;
}
/**
* 检查是否可以转换到目标状态
*/
canTransitionTo(targetState) {
const allowedTransitions = STATE_TRANSITIONS[this.state] || [];
return allowedTransitions.includes(targetState);
}
/**
* 状态转换
*/
async transition(targetState, context = {}) {
if (!this.canTransitionTo(targetState)) {
throw new Error(
`Invalid state transition: ${this.state} → ${targetState}. ` +
`Allowed transitions: ${STATE_TRANSITIONS[this.state]?.join(', ') || 'none'}`
);
}
const previousState = this.state;
this.state = targetState;
const historyEntry = {
state: targetState,
previousState,
timestamp: Date.now(),
context
};
this.history.push(historyEntry);
// 通知监听器
await this._notifyListeners(targetState, previousState, context);
return {
previousState,
newState: targetState,
timestamp: historyEntry.timestamp
};
}
/**
* 注册状态变化监听器
*/
onStateChange(listener) {
this.listeners.push(listener);
}
/**
* 通知监听器
*/
async _notifyListeners(newState, previousState, context) {
for (const listener of this.listeners) {
try {
await listener(newState, previousState, context);
} catch (error) {
console.error('State change listener error:', error.message);
}
}
}
/**
* 获取状态历史
*/
getHistory() {
return this.history;
}
/**
* 获取状态持续时间
*/
getStateDuration(state) {
const entries = this.history.filter(h => h.state === state);
Iif (entries.length === 0) return 0;
const firstEntry = entries[0];
const nextEntry = this.history.find(h =>
h.timestamp > firstEntry.timestamp && h.state !== state
);
Eif (nextEntry) {
return nextEntry.timestamp - firstEntry.timestamp;
}
// 如果是当前状态,计算到现在的时间
if (this.state === state) {
return Date.now() - firstEntry.timestamp;
}
return 0;
}
/**
* 序列化状态机
*/
serialize() {
return {
state: this.state,
history: this.history,
metadata: this.metadata,
serializedAt: Date.now()
};
}
/**
* 从序列化数据恢复
*/
static deserialize(data) {
const machine = new StateMachine(data.state, { metadata: data.metadata });
machine.history = data.history || [];
return machine;
}
/**
* 判断是否为终态
*/
isFinalState() {
return [OrchestrationState.COMPLETED, OrchestrationState.FAILED, OrchestrationState.CANCELLED].includes(this.state);
}
/**
* 判断是否可以重试
*/
canRetry() {
return this.state === OrchestrationState.FAILED;
}
}
/**
* 编排任务类
*/
class OrchestrationTask {
constructor(taskId, definition) {
this.taskId = taskId;
this.definition = definition;
this.stateMachine = new StateMachine(OrchestrationState.PENDING, {
metadata: { taskId, definition }
});
this.createdAt = Date.now();
this.updatedAt = Date.now();
}
async start() {
await this.stateMachine.transition(OrchestrationState.RUNNING, { action: 'start' });
this.updatedAt = Date.now();
}
async complete(result = {}) {
await this.stateMachine.transition(OrchestrationState.COMPLETED, { action: 'complete', result });
this.updatedAt = Date.now();
}
async fail(error) {
await this.stateMachine.transition(OrchestrationState.FAILED, { action: 'fail', error: error.message });
this.updatedAt = Date.now();
}
async pause() {
await this.stateMachine.transition(OrchestrationState.PAUSED, { action: 'pause' });
this.updatedAt = Date.now();
}
async resume() {
await this.stateMachine.transition(OrchestrationState.RUNNING, { action: 'resume' });
this.updatedAt = Date.now();
}
async cancel() {
await this.stateMachine.transition(OrchestrationState.CANCELLED, { action: 'cancel' });
this.updatedAt = Date.now();
}
getStatus() {
return {
taskId: this.taskId,
state: this.stateMachine.getState(),
createdAt: this.createdAt,
updatedAt: this.updatedAt,
history: this.stateMachine.getHistory(),
isFinal: this.stateMachine.isFinalState(),
canRetry: this.stateMachine.canRetry()
};
}
}
module.exports = {
OrchestrationState,
STATE_TRANSITIONS,
StateMachine,
OrchestrationTask
};
|