tasks.js 5.2 KB

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