summaryrefslogtreecommitdiffstats
path: root/common/resourcecontrol.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-11-28 23:44:55 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-11-28 23:44:55 +0100
commit169ded69825ab7bde01a5afe9dc842aebeb1c42e (patch)
treecafdfdcb24a3e38fd1973c687e304935eacb0922 /common/resourcecontrol.cpp
parent73ad9440244579ba625df970aa280e162f6f1c86 (diff)
downloadsink-169ded69825ab7bde01a5afe9dc842aebeb1c42e.tar.gz
sink-169ded69825ab7bde01a5afe9dc842aebeb1c42e.zip
Avoid race conditions if the notification comes back too early
Diffstat (limited to 'common/resourcecontrol.cpp')
-rw-r--r--common/resourcecontrol.cpp21
1 files changed, 10 insertions, 11 deletions
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<void> ResourceControl::flushReplayQueue(const QByteArray &resourceId
133template <class DomainType> 133template <class DomainType>
134KAsync::Job<void> ResourceControl::inspect(const Inspection &inspectionCommand) 134KAsync::Job<void> ResourceControl::inspect(const Inspection &inspectionCommand)
135{ 135{
136 auto resource = inspectionCommand.resourceIdentifier; 136 auto resourceIdentifier = inspectionCommand.resourceIdentifier;
137 137 auto resourceAccess = ResourceAccessFactory::instance().getAccess(resourceIdentifier, ResourceConfig::getResourceType(resourceIdentifier));
138 auto time = QSharedPointer<QTime>::create();
139 time->start();
140 SinkTrace() << "Sending inspection " << resource;
141 auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource, ResourceConfig::getResourceType(resource));
142 resourceAccess->open();
143 auto notifier = QSharedPointer<Sink::Notifier>::create(resourceAccess); 138 auto notifier = QSharedPointer<Sink::Notifier>::create(resourceAccess);
144 auto id = QUuid::createUuid().toByteArray(); 139 auto id = QUuid::createUuid().toByteArray();
145 return resourceAccess->sendInspectionCommand(inspectionCommand.type, id, ApplicationDomain::getTypeName<DomainType>(), inspectionCommand.entityIdentifier, inspectionCommand.property, inspectionCommand.expectedValue) 140 return KAsync::start<void>([=](KAsync::Future<void> &future) {
146 .template then<void>([resourceAccess, notifier, id, time](KAsync::Future<void> &future) { 141 notifier->registerHandler([&future, id](const Notification &notification) {
147 notifier->registerHandler([&future, id, time](const Notification &notification) {
148 if (notification.id == id) { 142 if (notification.id == id) {
149 SinkTrace() << "Inspection complete." << Log::TraceTime(time->elapsed()); 143 SinkTrace() << "Inspection complete";
150 if (notification.code) { 144 if (notification.code) {
145 SinkWarning() << "Inspection returned an error";
151 future.setError(-1, "Inspection returned an error: " + notification.message); 146 future.setError(-1, "Inspection returned an error: " + notification.message);
152 } else { 147 } else {
153 future.setFinished(); 148 future.setFinished();
154 } 149 }
155 } 150 }
156 }); 151 });
152 resourceAccess->sendInspectionCommand(inspectionCommand.type, id, ApplicationDomain::getTypeName<DomainType>(), inspectionCommand.entityIdentifier, inspectionCommand.property, inspectionCommand.expectedValue).onError([&future] (const KAsync::Error &error) {
153 SinkWarning() << "Failed to send command";
154 future.setError(1, "Failed to send command: " + error.errorMessage);
155 }).exec();
157 }); 156 });
158} 157}
159 158