-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtask_retry_handler.cpp
More file actions
234 lines (204 loc) · 7.35 KB
/
task_retry_handler.cpp
File metadata and controls
234 lines (204 loc) · 7.35 KB
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
#include "task_retry_handler.h"
#include "system_monitor.h"
#include "display_manager.h"
#include "logging.h"
// Global instance
TaskRetryHandler taskRetryHandler;
TaskRetryHandler::TaskRetryHandler() : lastProcessTime(0) {
}
unsigned long TaskRetryHandler::calculateNextRetryInterval(const RetryTask& task) {
if (!task.exponentialBackoff) {
return task.baseRetryInterval;
}
// Exponential backoff: baseInterval * 2^(attemptCount-1), capped at 60 seconds
unsigned long interval = task.baseRetryInterval;
for (int i = 1; i < task.attemptCount; i++) {
interval *= 2;
if (interval > 60000) { // Cap at 60 seconds
return 60000;
}
}
return interval;
}
void TaskRetryHandler::addTask(TaskType type, TaskCallback callback, const char* taskName,
int maxAttempts, unsigned long baseInterval,
const char* errorMessage) {
LOG_DEBUG_F("[TaskRetry] Adding task: %s (max attempts: %d, interval: %lu ms)\n", taskName, maxAttempts, baseInterval);
// Check if task already exists and remove it
removeTask(type);
RetryTask task;
task.type = type;
task.callback = callback;
task.taskName = taskName;
task.maxAttempts = maxAttempts;
task.baseRetryInterval = baseInterval;
task.errorMessage = errorMessage;
task.status = TASK_PENDING;
task.attemptCount = 0;
task.nextRetryTime = millis(); // Start immediately
taskQueue.push_back(task);
}
void TaskRetryHandler::process() {
unsigned long now = millis();
// Only process at regular intervals to avoid excessive overhead
if (now - lastProcessTime < PROCESS_INTERVAL) {
return;
}
lastProcessTime = now;
for (size_t i = 0; i < taskQueue.size(); i++) {
RetryTask& task = taskQueue[i];
// Skip if already completed or cancelled
if (task.status == TASK_SUCCESS || task.status == TASK_CANCELLED) {
continue;
}
// Skip if not yet time to retry
if (now < task.nextRetryTime) {
continue;
}
// Check if max attempts exceeded
if (task.attemptCount >= task.maxAttempts) {
task.status = TASK_FAILED;
LOG_ERROR_F("[TaskRetry] Task FAILED after %d attempts: %s\n", task.maxAttempts, task.taskName);
if (task.errorMessage && strlen(task.errorMessage) > 0) {
LOG_ERROR_F("[TaskRetry] Error: %s\n", task.errorMessage);
}
continue;
}
// Execute task
task.attemptCount++;
task.status = TASK_RUNNING;
task.lastAttemptTime = now;
LOG_DEBUG_F("[TaskRetry] Executing task (attempt %d/%d): %s\n", task.attemptCount, task.maxAttempts, task.taskName);
// Reset watchdog before task execution
systemMonitor.forceResetWatchdog();
// Call the task callback
bool success = false;
if (task.callback) {
success = task.callback();
}
// Reset watchdog after task execution
systemMonitor.forceResetWatchdog();
if (success) {
task.status = TASK_SUCCESS;
LOG_INFO_F("[TaskRetry] Task completed successfully: %s\n", task.taskName);
} else {
task.status = TASK_RETRYING;
unsigned long retryInterval = calculateNextRetryInterval(task);
task.nextRetryTime = now + retryInterval;
LOG_WARNING_F("[TaskRetry] Task failed, will retry in %lu ms: %s (attempt %d/%d)\n",
retryInterval, task.taskName, task.attemptCount, task.maxAttempts);
}
}
}
TaskStatus TaskRetryHandler::getTaskStatus(TaskType type) {
for (const auto& task : taskQueue) {
if (task.type == type) {
return task.status;
}
}
return TASK_FAILED; // Not found = failed
}
void TaskRetryHandler::cancelTask(TaskType type) {
for (auto& task : taskQueue) {
if (task.type == type) {
LOG_INFO_F("[TaskRetry] Task cancelled: %s\n", task.taskName);
task.status = TASK_CANCELLED;
break;
}
}
}
void TaskRetryHandler::clearCompletedTasks() {
taskQueue.erase(
std::remove_if(taskQueue.begin(), taskQueue.end(),
[](const RetryTask& task) {
return task.status == TASK_SUCCESS ||
task.status == TASK_FAILED ||
task.status == TASK_CANCELLED;
}),
taskQueue.end()
);
}
int TaskRetryHandler::getActiveTasks() {
int count = 0;
for (const auto& task : taskQueue) {
if (task.status != TASK_SUCCESS && task.status != TASK_CANCELLED) {
count++;
}
}
return count;
}
String TaskRetryHandler::getTaskStatusString(TaskType type) {
for (const auto& task : taskQueue) {
if (task.type == type) {
String status = "";
switch (task.status) {
case TASK_PENDING:
status = "PENDING";
break;
case TASK_RUNNING:
status = "RUNNING";
break;
case TASK_SUCCESS:
status = "SUCCESS";
break;
case TASK_FAILED:
status = "FAILED";
break;
case TASK_RETRYING:
status = String("RETRYING (") + task.attemptCount + "/" + task.maxAttempts + ")";
break;
case TASK_CANCELLED:
status = "CANCELLED";
break;
}
return status + " - " + task.taskName;
}
}
return "UNKNOWN";
}
String TaskRetryHandler::getAllTasksStatus() {
String result = "Active Tasks: " + String(getActiveTasks()) + "\n";
for (const auto& task : taskQueue) {
if (task.status != TASK_SUCCESS && task.status != TASK_CANCELLED) {
result += " [" + String(task.attemptCount) + "/" + String(task.maxAttempts) + "] ";
result += task.taskName;
result += " - ";
switch (task.status) {
case TASK_PENDING:
result += "Pending";
break;
case TASK_RUNNING:
result += "Running";
break;
case TASK_RETRYING:
result += "Retrying";
break;
case TASK_FAILED:
result += "Failed";
break;
default:
result += "Unknown";
}
result += "\n";
}
}
return result;
}
void TaskRetryHandler::removeTask(TaskType type) {
taskQueue.erase(
std::remove_if(taskQueue.begin(), taskQueue.end(),
[type](const RetryTask& task) {
return task.type == type;
}),
taskQueue.end()
);
}
bool TaskRetryHandler::hasCriticalFailures() {
for (const auto& task : taskQueue) {
if (task.status == TASK_FAILED &&
(task.type == TASK_NETWORK_CONNECT || task.type == TASK_SYSTEM_INIT)) {
return true;
}
}
return false;
}