ctkCmdLineModuleProcessTask.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. ctkCmdLineModuleProcessWatcher::ctkCmdLineModuleProcessWatcher(QProcess& process, const QString& location,
  27. ctkCmdLineModuleFutureInterface &futureInterface)
  28. : process(process), location(location), futureInterface(futureInterface), processXmlWatcher(&process),
  29. processPaused(false), progressValue(0)
  30. {
  31. futureInterface.setProgressRange(0, 1000);
  32. connect(&processXmlWatcher, SIGNAL(filterStarted(QString,QString)), SLOT(filterStarted(QString,QString)));
  33. connect(&processXmlWatcher, SIGNAL(filterProgress(float)), SLOT(filterProgress(float)));
  34. connect(&processXmlWatcher, SIGNAL(filterFinished(QString)), SLOT(filterFinished(QString)));
  35. connect(&processXmlWatcher, SIGNAL(filterXmlError(QString)), SLOT(filterXmlError(QString)));
  36. connect(&futureWatcher, SIGNAL(canceled()), SLOT(cancelProcess()));
  37. #ifdef Q_OS_UNIX
  38. connect(&futureWatcher, SIGNAL(resumed()), SLOT(resumeProcess()));
  39. // Due to Qt bug 12152, we cannot listen to the "paused" signal because it is
  40. // not emitted directly when the QFuture is paused. Instead, it is emitted after
  41. // resuming the future, after the "resume" signal has been emitted...
  42. //connect(&futureWatcher, SIGNAL(paused()), SLOT(pauseProcess()));
  43. connect(&pollPauseTimer, SIGNAL(timeout()), this, SLOT(pauseProcess()));
  44. pollPauseTimer.start(500);
  45. #endif
  46. futureWatcher.setFuture(futureInterface.future());
  47. }
  48. void ctkCmdLineModuleProcessWatcher::filterStarted(const QString& name, const QString& comment)
  49. {
  50. Q_UNUSED(comment)
  51. futureInterface.setProgressValueAndText(incrementProgress(), name);
  52. }
  53. void ctkCmdLineModuleProcessWatcher::filterProgress(float progress)
  54. {
  55. futureInterface.setProgressValue(updateProgress(progress));
  56. }
  57. void ctkCmdLineModuleProcessWatcher::filterFinished(const QString& name)
  58. {
  59. futureInterface.setProgressValueAndText(incrementProgress(), "Finished: " + name);
  60. }
  61. void ctkCmdLineModuleProcessWatcher::filterXmlError(const QString &error)
  62. {
  63. qDebug().nospace() << "[Module " << location << "]: " << error;
  64. }
  65. void ctkCmdLineModuleProcessWatcher::pauseProcess()
  66. {
  67. if (processPaused || !futureInterface.isPaused()) return;
  68. #ifdef Q_OS_UNIX
  69. if (::kill(process.pid(), SIGSTOP))
  70. {
  71. // error
  72. futureInterface.setPaused(false);
  73. }
  74. else
  75. {
  76. processPaused = true;
  77. }
  78. #endif
  79. }
  80. void ctkCmdLineModuleProcessWatcher::resumeProcess()
  81. {
  82. if (!processPaused) return;
  83. #ifdef Q_OS_UNIX
  84. if(::kill(process.pid(), SIGCONT))
  85. {
  86. // error
  87. futureInterface.setPaused(true);
  88. }
  89. else
  90. {
  91. processPaused = false;
  92. }
  93. #endif
  94. }
  95. void ctkCmdLineModuleProcessWatcher::cancelProcess()
  96. {
  97. process.terminate();
  98. }
  99. int ctkCmdLineModuleProcessWatcher::updateProgress(float progress)
  100. {
  101. progressValue = static_cast<int>(progress * 1000.0f);
  102. // normalize the value to lie between 0 and 1000.
  103. // 0 is reported when the process starts and 1000 is reserved for
  104. // reporting completion + standard output text
  105. if (progressValue < 1) progressValue = 1;
  106. if (progressValue > 999) progressValue = 999;
  107. return progressValue;
  108. }
  109. int ctkCmdLineModuleProcessWatcher::incrementProgress()
  110. {
  111. if (++progressValue > 999) progressValue = 999;
  112. return progressValue;
  113. }
  114. ctkCmdLineModuleProcessTask::ctkCmdLineModuleProcessTask(const QString& location, const QStringList& args)
  115. : location(location), args(args)
  116. {
  117. this->setCanCancel(true);
  118. #ifdef Q_OS_UNIX
  119. this->setCanPause(true);
  120. #endif
  121. }
  122. ctkCmdLineModuleProcessTask::~ctkCmdLineModuleProcessTask()
  123. {
  124. }
  125. ctkCmdLineModuleFuture ctkCmdLineModuleProcessTask::start()
  126. {
  127. this->setRunnable(this);
  128. this->reportStarted();
  129. ctkCmdLineModuleFuture future = this->future();
  130. QThreadPool::globalInstance()->start(this, /*m_priority*/ 0);
  131. return future;
  132. }
  133. void ctkCmdLineModuleProcessTask::run()
  134. {
  135. if (this->isCanceled())
  136. {
  137. this->reportFinished();
  138. return;
  139. }
  140. QProcess process;
  141. process.setReadChannel(QProcess::StandardOutput);
  142. QEventLoop localLoop;
  143. QObject::connect(&process, SIGNAL(finished(int)), &localLoop, SLOT(quit()));
  144. QObject::connect(&process, SIGNAL(error(QProcess::ProcessError)), &localLoop, SLOT(quit()));
  145. process.start(location, args);
  146. ctkCmdLineModuleProcessWatcher progressWatcher(process, location, *this);
  147. Q_UNUSED(progressWatcher)
  148. localLoop.exec();
  149. if (process.error() != QProcess::UnknownError)
  150. {
  151. this->reportException(ctkCmdLineModuleRunException(location, process.exitCode(), process.errorString()));
  152. }
  153. else if (process.exitCode() != 0)
  154. {
  155. this->reportException(ctkCmdLineModuleRunException(location, process.exitCode(), process.readAllStandardError()));
  156. }
  157. this->setProgressValueAndText(1000, process.readAllStandardError());
  158. //this->reportResult(result);
  159. this->reportFinished();
  160. }