PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sked/src/task.cpp

https://bitbucket.org/sourcey/libsourcey
C++ | 313 lines | 98 code | 65 blank | 150 comment | 7 complexity | b4ea722c6eff661f7f76eb6d8138af25 MD5 | raw file
Possible License(s): MIT, GPL-3.0, ISC
  1. //
  2. // LibSourcey
  3. // Copyright (C) 2005, Sourcey <http://sourcey.com>
  4. //
  5. // LibSourcey is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // LibSourcey is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. #include "scy/sked/task.h"
  19. #include "scy/sked/scheduler.h"
  20. #include "scy/datetime.h"
  21. /*
  22. #include "Poco/DateTimeParser.h"
  23. #include "Poco/DateTime.h"
  24. #include "Poco/Timespan.h"
  25. #include "Poco/Format.h"
  26. using namespace Poco;
  27. */
  28. using namespace std;
  29. namespace scy {
  30. namespace sked {
  31. Task::Task(const std::string& type, const std::string& name) :
  32. //scy::Task(true),
  33. _type(type),
  34. _name(name),
  35. _scheduler(nullptr),
  36. _trigger(nullptr)
  37. {
  38. TraceL << "Create" << endl;
  39. }
  40. Task::Task(sked::Scheduler& scheduler, const std::string& type, const std::string& name) :
  41. //scy::Task(true),
  42. //scy::Task(reinterpret_cast<Scheduler&>(scheduler), true, false),
  43. _type(type),
  44. _name(name),
  45. _scheduler(&scheduler),
  46. _trigger(nullptr)
  47. {
  48. TraceL << "Create" << endl;
  49. }
  50. Task::~Task()
  51. {
  52. TraceL << "Destroy" << endl;
  53. }
  54. /*
  55. void Task::start()
  56. {
  57. trigger(); // throw if trigger is nullptr
  58. scy::Task::start();
  59. }
  60. */
  61. void Task::serialize(json::Value& root)
  62. {
  63. TraceL << "Serializing" << endl;
  64. Mutex::ScopedLock lock(_mutex);
  65. root["id"] = _id;
  66. root["type"] = _type;
  67. root["name"] = _name;
  68. }
  69. void Task::deserialize(json::Value& root)
  70. {
  71. TraceL << "Deserializing" << endl;
  72. Mutex::ScopedLock lock(_mutex);
  73. json::assertMember(root, "id");
  74. json::assertMember(root, "type");
  75. json::assertMember(root, "name");
  76. _id = root["id"].asUInt();
  77. _type = root["type"].asString();
  78. _name = root["name"].asString();
  79. }
  80. bool Task::beforeRun()
  81. {
  82. Mutex::ScopedLock lock(_mutex);
  83. return _trigger && _trigger->timeout() && !_destroyed && !cancelled();
  84. }
  85. bool Task::afterRun()
  86. {
  87. Mutex::ScopedLock lock(_mutex);
  88. DateTime now;
  89. _trigger->update();
  90. _trigger->timesRun++;
  91. _trigger->lastRunAt = now;
  92. return !_trigger->expired();
  93. }
  94. void Task::setTrigger(sked::Trigger* trigger)
  95. {
  96. Mutex::ScopedLock lock(_mutex);
  97. if (_trigger)
  98. delete _trigger;
  99. _trigger = trigger;
  100. }
  101. string Task::name() const
  102. {
  103. Mutex::ScopedLock lock(_mutex);
  104. return _name;
  105. }
  106. string Task::type() const
  107. {
  108. Mutex::ScopedLock lock(_mutex);
  109. return _type;
  110. }
  111. Int64 Task::remaining() const
  112. {
  113. Mutex::ScopedLock lock(_mutex);
  114. if (!_trigger)
  115. throw std::runtime_error("Tasks must be have a Trigger instance.");
  116. return _trigger->remaining();
  117. }
  118. sked::Trigger& Task::trigger()
  119. {
  120. Mutex::ScopedLock lock(_mutex);
  121. if (!_trigger)
  122. throw std::runtime_error("Tasks must have a Trigger instance.");
  123. return *_trigger;
  124. }
  125. sked::Scheduler& Task::scheduler()
  126. {
  127. Mutex::ScopedLock lock(_mutex);
  128. if (!_scheduler)
  129. throw std::runtime_error("Tasks must be started with a sked::Scheduler instance.");
  130. return *_scheduler;
  131. }
  132. } } // namespace scy::sked
  133. /*
  134. void Task::setName(const std::string& name)
  135. {
  136. Mutex::ScopedLock lock(_mutex);
  137. _name = name;
  138. }
  139. */
  140. //schedule(time);
  141. /*
  142. if (!root.isMember("time"))
  143. throw std::runtime_error("A time member is required.");
  144. int tzd;
  145. DateTime time(DateTimeParser::parse(DateTimeFormat::ISO8601_FORMAT, root["time"].asString(), tzd));
  146. schedule(time);
  147. */
  148. //root["time"] = DateTimeFormatter::format(time(),
  149. // DateTimeFormat::ISO8601_FORMAT);
  150. //if (!root.isMember("time"))
  151. // throw std::runtime_error("A time member is required.");
  152. //int tzd;
  153. //DateTime time(DateTimeParser::parse(DateTimeFormat::ISO8601_FORMAT, root["time"].asString(), tzd));
  154. //schedule(time);
  155. //if (!&trigger())
  156. // throw std::runtime_error("A sked::Trigger is required to start the task.");
  157. /*
  158. void Task::schedule(const DateTime& time)
  159. {
  160. Timestamp now;
  161. Timestamp::TimeDiff diff = time.timestamp() - now;
  162. setTimeout(diff / 1000);
  163. }
  164. void Task::scheduleRepeated(const DateTime& time, const Timespan& interval)
  165. {
  166. schedule(time);
  167. setInterval(interval.totalMilliseconds());
  168. }
  169. DateTime Task::time() const
  170. {
  171. Mutex::ScopedLock lock(_mutex);
  172. return _time;
  173. }
  174. */
  175. //setTimeout(interval.totalMilliseconds());
  176. //DateTime now;
  177. //Timespan s = now += time;
  178. //Timespan s = time - now;
  179. //TraceL << "[Task: " << this << "] Time Now: " << DateTimeFormatter::format(now, DateTimeFormat::ISO8601_FORMAT) << endl;
  180. //TraceL << "[Task: " << this << "] Time Trigger: " << DateTimeFormatter::format(time, DateTimeFormat::ISO8601_FORMAT) << endl;
  181. //TraceL << "[Task: " << this << "] Timeout in " << (diff / 1000) << endl;
  182. //start();
  183. //DateTime now;
  184. //Timespan s = time - now;
  185. //start();
  186. /*
  187. bool Task::run()
  188. {
  189. bool doTimeout = false;
  190. bool doDestroy = false;
  191. {
  192. Mutex::ScopedLock lock(_mutex);
  193. if (_scheduleAt.timeout()) {
  194. doTimeout = true;
  195. if (_interval > 0) {
  196. _scheduleAt.setDelay(_interval);
  197. _scheduleAt.reset();
  198. }
  199. else doDestroy = true;
  200. }
  201. }
  202. if (doTimeout)
  203. onTimeout();
  204. if (doDestroy)
  205. destroy();
  206. }
  207. */
  208. /*
  209. void Task::schedule(time_t time)
  210. {
  211. DateTime runAt(Timestamp::fromEpochTime(time));
  212. schedule(runAt);
  213. }
  214. void Task::schedule(const std::string& time, const std::string& fmt)
  215. {
  216. int tzd;
  217. DateTime runAt(DateTimeParser::parse(fmt, time, tzd));
  218. schedule(runAt);
  219. }
  220. */
  221. /*
  222. void Task::serialize(json::Value& root)
  223. {
  224. TraceL << "Serializing" << endl;
  225. root["time"] = DateTimeFormatter::format(time(),
  226. DateTimeFormat::ISO8601_FORMAT);
  227. }
  228. void Task::deserialize(json::Value& root)
  229. {
  230. TraceL << "Deserializing" << endl;
  231. if (!root.isMember("time"))
  232. throw std::runtime_error("A time member is required.");
  233. schedule(root["time"].asString());
  234. }
  235. */