restoreQueue method
Restore the queue from a file. Returns true
if successful.
Implementation
Future<bool> restoreQueue() async {
bool success = true;
UserTaskSnapshotList? queue;
try {
String name = (await filename)!;
info("$runtimeType - Restoring task queue from file '$name'.");
String jsonString = File(name).readAsStringSync();
queue = UserTaskSnapshotList.fromJson(
json.decode(jsonString) as Map<String, dynamic>);
// now create new AppTaskExecutors, initialize them, and add them to the queue
for (var snapshot in queue.snapshots) {
AppTaskExecutor executor = AppTaskExecutor();
// find the deployment
SmartphoneDeployment? deployment;
if (snapshot.studyDeploymentId != null &&
snapshot.deviceRoleName != null) {
deployment = SmartPhoneClientManager()
.lookupStudyRuntime(
snapshot.studyDeploymentId!,
snapshot.deviceRoleName!,
)
?.deployment;
}
if (deployment == null) {
warning(
'$runtimeType - Could not find deployment information based on snapshot: $snapshot\nAppTask is not restored.');
} else {
executor.initialize(snapshot.task, deployment);
// now put the task on the queue
if (_userTaskFactories[executor.task.type] == null) {
warning(
'Could not enqueue AppTask. Could not find a factory for creating '
"a UserTask for type '${executor.task.type}'");
} else {
UserTask userTask =
_userTaskFactories[executor.task.type]!.create(executor);
userTask.id = snapshot.id;
userTask.state = snapshot.state;
userTask.enqueued = snapshot.enqueued;
userTask.triggerTime = snapshot.triggerTime;
_userTaskMap[userTask.id] = userTask;
debug(
'$runtimeType - Enqueued UserTask from loaded task queue: $userTask');
}
}
}
} catch (exception) {
success = false;
warning('$runtimeType - Failed to load task queue - $exception');
}
return success;
}