diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-25 17:21:22 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-07-25 17:21:22 +0200 |
commit | 898f35f2982e86f95c7fe061aa5e697c771a0d47 (patch) | |
tree | 95425299b4c4668258707de9cc149ba6f43add10 /common/resourcecontrol.cpp | |
parent | a9cd61f1baafe09fd20ffb6ba1c6728a8792b344 (diff) | |
download | sink-898f35f2982e86f95c7fe061aa5e697c771a0d47.tar.gz sink-898f35f2982e86f95c7fe061aa5e697c771a0d47.zip |
Avoid the socket probing and move the shutdown logic into
resourceaccess.
The problem was (as excercised by the last test in resourcecontroltest),
that in this scenario we would:
* trigger a synchronization that starts the resource, and then goes into
a loop trying to connecting (KAsync::wait -> singleshot timer)
* trigger a shutdown that would probe for the socket, not find it, and
thus do nothing.
* exit the testfunction, which somehow stops qtimer processing, meaning
we are stuck in KAsync::wait.
For now this is fixed by simply not probing for the socket.
Diffstat (limited to 'common/resourcecontrol.cpp')
-rw-r--r-- | common/resourcecontrol.cpp | 49 |
1 files changed, 19 insertions, 30 deletions
diff --git a/common/resourcecontrol.cpp b/common/resourcecontrol.cpp index 5e8e09a..ea660c5 100644 --- a/common/resourcecontrol.cpp +++ b/common/resourcecontrol.cpp | |||
@@ -37,38 +37,27 @@ KAsync::Job<void> ResourceControl::shutdown(const QByteArray &identifier) | |||
37 | SinkTrace() << "shutdown " << identifier; | 37 | SinkTrace() << "shutdown " << identifier; |
38 | auto time = QSharedPointer<QTime>::create(); | 38 | auto time = QSharedPointer<QTime>::create(); |
39 | time->start(); | 39 | time->start(); |
40 | return ResourceAccess::connectToServer(identifier) | 40 | |
41 | .then<void, QSharedPointer<QLocalSocket>>( | 41 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(identifier, ResourceConfig::getResourceType(identifier)); |
42 | [identifier, time](const KAsync::Error &error, QSharedPointer<QLocalSocket> socket) { | 42 | return resourceAccess->shutdown() |
43 | if (error) { | 43 | .addToContext(resourceAccess) |
44 | SinkTrace() << "Resource is already closed."; | 44 | .then<void>([resourceAccess, time](KAsync::Future<void> &future) { |
45 | // Resource isn't started, nothing to shutdown | 45 | SinkTrace() << "Shutdown command complete, waiting for shutdown." << Log::TraceTime(time->elapsed()); |
46 | return KAsync::null(); | 46 | if (!resourceAccess->isReady()) { |
47 | future.setFinished(); | ||
48 | return; | ||
49 | } | ||
50 | auto guard = new QObject; | ||
51 | QObject::connect(resourceAccess.data(), &ResourceAccess::ready, guard, [&future, guard](bool ready) { | ||
52 | if (!ready) { | ||
53 | //Protect against callback getting called twice. | ||
54 | delete guard; | ||
55 | future.setFinished(); | ||
47 | } | 56 | } |
48 | // We can't currently reuse the socket | ||
49 | socket->close(); | ||
50 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(identifier, ResourceConfig::getResourceType(identifier)); | ||
51 | resourceAccess->open(); | ||
52 | return resourceAccess->sendCommand(Sink::Commands::ShutdownCommand) | ||
53 | .addToContext(resourceAccess) | ||
54 | .then<void>([resourceAccess, time](KAsync::Future<void> &future) { | ||
55 | SinkTrace() << "Shutdown command complete, waiting for shutdown." << Log::TraceTime(time->elapsed()); | ||
56 | if (!resourceAccess->isReady()) { | ||
57 | future.setFinished(); | ||
58 | return; | ||
59 | } | ||
60 | auto guard = new QObject; | ||
61 | QObject::connect(resourceAccess.data(), &ResourceAccess::ready, guard, [&future, guard](bool ready) { | ||
62 | if (!ready) { | ||
63 | //Protect against callback getting called twice. | ||
64 | delete guard; | ||
65 | future.setFinished(); | ||
66 | } | ||
67 | }); | ||
68 | }).then([time] { | ||
69 | SinkTrace() << "Shutdown complete." << Log::TraceTime(time->elapsed()); | ||
70 | }); | ||
71 | }); | 57 | }); |
58 | }).then([time] { | ||
59 | SinkTrace() << "Shutdown complete." << Log::TraceTime(time->elapsed()); | ||
60 | }); | ||
72 | } | 61 | } |
73 | 62 | ||
74 | KAsync::Job<void> ResourceControl::start(const QByteArray &identifier) | 63 | KAsync::Job<void> ResourceControl::start(const QByteArray &identifier) |