ctkCmdLineModuleProcessTask.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleProcessTask.h"
  16. #include "ctkCmdLineModuleRunException.h"
  17. #include "ctkCmdLineModuleXmlProgressWatcher.h"
  18. #include "ctkCmdLineModuleFuture.h"
  19. #include <QDebug>
  20. #include <QEventLoop>
  21. #include <QThreadPool>
  22. #include <QProcess>
  23. #ifdef Q_OS_UNIX
  24. #include <signal.h>
  25. #endif
  26. //----------------------------------------------------------------------------
  27. ctkCmdLineModuleProcessWatcher::ctkCmdLineModuleProcessWatcher(QProcess& process, const QString& location,
  28. ctkCmdLineModuleFutureInterface &futureInterface)
  29. : process(process), location(location), futureInterface(futureInterface), processXmlWatcher(&process),
  30. processPaused(false), progressValue(0)
  31. {
  32. futureInterface.setProgressRange(0, 1000);
  33. connect(&processXmlWatcher, SIGNAL(filterStarted(QString,QString)), SLOT(filterStarted(QString,QString)));
  34. connect(&processXmlWatcher, SIGNAL(filterProgress(float)), SLOT(filterProgress(float)));
  35. connect(&processXmlWatcher, SIGNAL(filterFinished(QString)), SLOT(filterFinished(QString)));
  36. connect(&processXmlWatcher, SIGNAL(filterXmlError(QString)), SLOT(filterXmlError(QString)));
  37. connect(&futureWatcher, SIGNAL(canceled()), SLOT(cancelProcess()));
  38. #ifdef Q_OS_UNIX
  39. connect(&futureWatcher, SIGNAL(resumed()), SLOT(resumeProcess()));
  40. // Due to Qt bug 12152, we cannot listen to the "paused" signal because it is
  41. // not emitted directly when the QFuture is paused. Instead, it is emitted after
  42. // resuming the future, after the "resume" signal has been emitted...
  43. //connect(&futureWatcher, SIGNAL(paused()), SLOT(pauseProcess()));
  44. connect(&pollPauseTimer, SIGNAL(timeout()), this, SLOT(pauseProcess()));
  45. pollPauseTimer.start(500);
  46. #endif
  47. futureWatcher.setFuture(futureInterface.future());
  48. }
  49. //----------------------------------------------------------------------------
  50. void ctkCmdLineModuleProcessWatcher::filterStarted(const QString& name, const QString& comment)
  51. {
  52. Q_UNUSED(comment)
  53. futureInterface.setProgressValueAndText(incrementProgress(), name);
  54. }
  55. //----------------------------------------------------------------------------
  56. void ctkCmdLineModuleProcessWatcher::filterProgress(float progress)
  57. {
  58. futureInterface.setProgressValue(updateProgress(progress));
  59. }
  60. //----------------------------------------------------------------------------
  61. void ctkCmdLineModuleProcessWatcher::filterFinished(const QString& name)
  62. {
  63. futureInterface.setProgressValueAndText(incrementProgress(), "Finished: " + name);
  64. }
  65. //----------------------------------------------------------------------------
  66. void ctkCmdLineModuleProcessWatcher::filterXmlError(const QString &error)
  67. {
  68. qDebug().nospace() << "[Module " << location << "]: " << error;
  69. }
  70. //----------------------------------------------------------------------------
  71. void ctkCmdLineModuleProcessWatcher::pauseProcess()
  72. {
  73. if (processPaused || !futureInterface.isPaused()) return;
  74. #ifdef Q_OS_UNIX
  75. if (::kill(process.pid(), SIGSTOP))
  76. {
  77. // error
  78. futureInterface.setPaused(false);
  79. }
  80. else
  81. {
  82. processPaused = true;
  83. }
  84. #endif
  85. }
  86. //----------------------------------------------------------------------------
  87. void ctkCmdLineModuleProcessWatcher::resumeProcess()
  88. {
  89. if (!processPaused) return;
  90. #ifdef Q_OS_UNIX
  91. if(::kill(process.pid(), SIGCONT))
  92. {
  93. // error
  94. futureInterface.setPaused(true);
  95. }
  96. else
  97. {
  98. processPaused = false;
  99. }
  100. #endif
  101. }
  102. //----------------------------------------------------------------------------
  103. void ctkCmdLineModuleProcessWatcher::cancelProcess()
  104. {
  105. process.terminate();
  106. }
  107. int ctkCmdLineModuleProcessWatcher::updateProgress(float progress)
  108. {
  109. progressValue = static_cast<int>(progress * 1000.0f);
  110. // normalize the value to lie between 0 and 1000.
  111. // 0 is reported when the process starts and 1000 is reserved for
  112. // reporting completion + standard output text
  113. if (progressValue < 1) progressValue = 1;
  114. if (progressValue > 999) progressValue = 999;
  115. return progressValue;
  116. }
  117. //----------------------------------------------------------------------------
  118. int ctkCmdLineModuleProcessWatcher::incrementProgress()
  119. {
  120. if (++progressValue > 999) progressValue = 999;
  121. return progressValue;
  122. }
  123. //----------------------------------------------------------------------------
  124. ctkCmdLineModuleProcessTask::ctkCmdLineModuleProcessTask(const QString& location, const QStringList& args)
  125. : location(location), args(args)
  126. {
  127. this->setCanCancel(true);
  128. #ifdef Q_OS_UNIX
  129. this->setCanPause(true);
  130. #endif
  131. }
  132. //----------------------------------------------------------------------------
  133. ctkCmdLineModuleProcessTask::~ctkCmdLineModuleProcessTask()
  134. {
  135. }
  136. //----------------------------------------------------------------------------
  137. ctkCmdLineModuleFuture ctkCmdLineModuleProcessTask::start()
  138. {
  139. this->setRunnable(this);
  140. this->reportStarted();
  141. ctkCmdLineModuleFuture future = this->future();
  142. QThreadPool::globalInstance()->start(this, /*m_priority*/ 0);
  143. return future;
  144. }
  145. //----------------------------------------------------------------------------
  146. void ctkCmdLineModuleProcessTask::run()
  147. {
  148. if (this->isCanceled())
  149. {
  150. this->reportFinished();
  151. return;
  152. }
  153. QProcess process;
  154. process.setReadChannel(QProcess::StandardOutput);
  155. QEventLoop localLoop;
  156. QObject::connect(&process, SIGNAL(finished(int)), &localLoop, SLOT(quit()));
  157. QObject::connect(&process, SIGNAL(error(QProcess::ProcessError)), &localLoop, SLOT(quit()));
  158. process.start(location, args);
  159. ctkCmdLineModuleProcessWatcher progressWatcher(process, location, *this);
  160. Q_UNUSED(progressWatcher)
  161. localLoop.exec();
  162. if (process.error() != QProcess::UnknownError)
  163. {
  164. this->reportException(ctkCmdLineModuleRunException(location, process.exitCode(), process.errorString()));
  165. }
  166. else if (process.exitCode() != 0)
  167. {
  168. this->reportException(ctkCmdLineModuleRunException(location, process.exitCode(), process.readAllStandardError()));
  169. }
  170. this->setProgressValueAndText(1000, process.readAllStandardError());
  171. //this->reportResult(result);
  172. this->reportFinished();
  173. }