tasks.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. const coreClass = require("../core");
  3. const async = require("async");
  4. let tasks = {};
  5. module.exports = class extends coreClass {
  6. initialize() {
  7. return new Promise((resolve, reject) => {
  8. this.cache = this.moduleManager.modules["cache"];
  9. this.stations = this.moduleManager.modules["stations"];
  10. this.notifications = this.moduleManager.modules["notifications"];
  11. this.utils = this.moduleManager.modules["utils"];
  12. //this.createTask("testTask", testTask, 5000, true);
  13. this.createTask("stationSkipTask", this.checkStationSkipTask, 1000 * 60 * 30);
  14. this.createTask("sessionClearTask", this.sessionClearingTask, 1000 * 60 * 60 * 6);
  15. resolve();
  16. });
  17. }
  18. async createTask(name, fn, timeout, paused = false) {
  19. try { await this._validateHook(); } catch { return; }
  20. tasks[name] = {
  21. name,
  22. fn,
  23. timeout,
  24. lastRan: 0,
  25. timer: null
  26. };
  27. if (!paused) this.handleTask(tasks[name]);
  28. }
  29. async pauseTask(name) {
  30. try { await this._validateHook(); } catch { return; }
  31. if (tasks[name].timer) tasks[name].timer.pause();
  32. }
  33. async resumeTask(name) {
  34. try { await this._validateHook(); } catch { return; }
  35. tasks[name].timer.resume();
  36. }
  37. async handleTask(task) {
  38. try { await this._validateHook(); } catch { return; }
  39. if (task.timer) task.timer.pause();
  40. task.fn(() => {
  41. task.lastRan = Date.now();
  42. task.timer = new utils.Timer(() => {
  43. this.handleTask(task);
  44. }, task.timeout, false);
  45. });
  46. }
  47. /*testTask(callback) {
  48. //Stuff
  49. console.log("Starting task");
  50. setTimeout(() => {
  51. console.log("Callback");
  52. callback();
  53. }, 10000);
  54. }*/
  55. async checkStationSkipTask(callback) {
  56. try { await this._validateHook(); } catch { return; }
  57. this.logger.info("TASK_STATIONS_SKIP_CHECK", `Checking for stations to be skipped.`, false);
  58. async.waterfall([
  59. (next) => {
  60. this.cache.hgetall('stations', next);
  61. },
  62. (stations, next) => {
  63. async.each(stations, (station, next2) => {
  64. if (station.paused || !station.currentSong || !station.currentSong.title) return next2();
  65. const timeElapsed = Date.now() - station.startedAt - station.timePaused;
  66. if (timeElapsed <= station.currentSong.duration) return next2();
  67. else {
  68. this.logger.error("TASK_STATIONS_SKIP_CHECK", `Skipping ${station._id} as it should have skipped already.`);
  69. this.stations.initializeStation(station._id);
  70. next2();
  71. }
  72. }, () => {
  73. next();
  74. });
  75. }
  76. ], () => {
  77. callback();
  78. });
  79. }
  80. async sessionClearingTask(callback) {
  81. try { await this._validateHook(); } catch { return; }
  82. this.logger.info("TASK_SESSION_CLEAR", `Checking for sessions to be cleared.`, false);
  83. async.waterfall([
  84. (next) => {
  85. this.cache.hgetall('sessions', next);
  86. },
  87. (sessions, next) => {
  88. if (!sessions) return next();
  89. let keys = Object.keys(sessions);
  90. async.each(keys, (sessionId, next2) => {
  91. let session = sessions[sessionId];
  92. if (session && session.refreshDate && (Date.now() - session.refreshDate) < (60 * 60 * 24 * 30 * 1000)) return next2();
  93. if (!session) {
  94. this.logger.info("TASK_SESSION_CLEAR", 'Removing an empty session.');
  95. this.cache.hdel('sessions', sessionId, () => {
  96. next2();
  97. });
  98. } else if (!session.refreshDate) {
  99. session.refreshDate = Date.now();
  100. this.cache.hset('sessions', sessionId, session, () => {
  101. next2();
  102. });
  103. } else if ((Date.now() - session.refreshDate) > (60 * 60 * 24 * 30 * 1000)) {
  104. this.utils.socketsFromSessionId(session.sessionId, (sockets) => {
  105. if (sockets.length > 0) {
  106. session.refreshDate = Date.now();
  107. this.cache.hset('sessions', sessionId, session, () => {
  108. next2()
  109. });
  110. } else {
  111. this.logger.info("TASK_SESSION_CLEAR", `Removing session ${sessionId} for user ${session.userId} since inactive for 30 days and not currently in use.`);
  112. this.cache.hdel('sessions', session.sessionId, () => {
  113. next2();
  114. });
  115. }
  116. });
  117. } else {
  118. this.logger.error("TASK_SESSION_CLEAR", "This should never log.");
  119. next2();
  120. }
  121. }, () => {
  122. next();
  123. });
  124. }
  125. ], () => {
  126. callback();
  127. });
  128. }
  129. }