From 169ded69825ab7bde01a5afe9dc842aebeb1c42e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 28 Nov 2016 23:44:55 +0100 Subject: Avoid race conditions if the notification comes back too early --- common/resourcecontrol.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'common') diff --git a/common/resourcecontrol.cpp b/common/resourcecontrol.cpp index af98b8b..f885deb 100644 --- a/common/resourcecontrol.cpp +++ b/common/resourcecontrol.cpp @@ -133,27 +133,26 @@ KAsync::Job ResourceControl::flushReplayQueue(const QByteArray &resourceId template KAsync::Job ResourceControl::inspect(const Inspection &inspectionCommand) { - auto resource = inspectionCommand.resourceIdentifier; - - auto time = QSharedPointer::create(); - time->start(); - SinkTrace() << "Sending inspection " << resource; - auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource, ResourceConfig::getResourceType(resource)); - resourceAccess->open(); + auto resourceIdentifier = inspectionCommand.resourceIdentifier; + auto resourceAccess = ResourceAccessFactory::instance().getAccess(resourceIdentifier, ResourceConfig::getResourceType(resourceIdentifier)); auto notifier = QSharedPointer::create(resourceAccess); auto id = QUuid::createUuid().toByteArray(); - return resourceAccess->sendInspectionCommand(inspectionCommand.type, id, ApplicationDomain::getTypeName(), inspectionCommand.entityIdentifier, inspectionCommand.property, inspectionCommand.expectedValue) - .template then([resourceAccess, notifier, id, time](KAsync::Future &future) { - notifier->registerHandler([&future, id, time](const Notification ¬ification) { + return KAsync::start([=](KAsync::Future &future) { + notifier->registerHandler([&future, id](const Notification ¬ification) { if (notification.id == id) { - SinkTrace() << "Inspection complete." << Log::TraceTime(time->elapsed()); + SinkTrace() << "Inspection complete"; if (notification.code) { + SinkWarning() << "Inspection returned an error"; future.setError(-1, "Inspection returned an error: " + notification.message); } else { future.setFinished(); } } }); + resourceAccess->sendInspectionCommand(inspectionCommand.type, id, ApplicationDomain::getTypeName(), inspectionCommand.entityIdentifier, inspectionCommand.property, inspectionCommand.expectedValue).onError([&future] (const KAsync::Error &error) { + SinkWarning() << "Failed to send command"; + future.setError(1, "Failed to send command: " + error.errorMessage); + }).exec(); }); } -- cgit v1.2.3