run method
Executes the task
Implementation
Future<T> run() async {
if (!canExecute) {
throw ScrapingException.validation(
'Task cannot be executed (status: $status, retries: $retryCount/$maxRetries)',
isRetryable: false,
);
}
// Check dependencies
if (!areDependenciesComplete) {
throw ScrapingException.validation(
'Task dependencies are not complete',
isRetryable: false,
);
}
// Update status and start time
status = TaskStatus.executing;
startedAt = DateTime.now();
logger?.info('Starting task $id for $url (priority: $priority)');
try {
// Execute the task
final result = await execute();
// Update status and completion time
status = TaskStatus.completed;
completedAt = DateTime.now();
this.result = result;
// Calculate execution time
final executionTime = completedAt!.difference(startedAt!).inMilliseconds;
logger?.info('Completed task $id in ${executionTime}ms');
// Complete the future
if (!_completer.isCompleted) {
_completer.complete(result);
}
return result;
} catch (e, st) {
// Update status and error information
status = TaskStatus.failed;
error = e;
stackTrace = st;
completedAt = DateTime.now();
// Log the error
logger?.error('Task $id failed: $e');
// Check if we can retry
if (retryCount < maxRetries) {
retryCount++;
logger?.info('Retrying task $id (attempt $retryCount/$maxRetries)');
return run();
}
// Complete the future with an error
if (!_completer.isCompleted) {
_completer.completeError(e, st);
}
rethrow;
}
}