Преглед изворни кода

refs 111686: apply feedback

Futa Arai пре 3 година
родитељ
комит
d5795c1251
2 измењених фајлова са 16 додато и 18 уклоњено
  1. 11 18
      packages/app/src/server/service/questionnaire-cron.ts
  2. 5 0
      packages/app/src/utils/rand.ts

+ 11 - 18
packages/app/src/server/service/questionnaire-cron.ts

@@ -1,42 +1,35 @@
 import axios from '~/utils/axios';
+import { getRandomIntInRange } from '~/utils/rand';
 import { sleep } from '~/utils/sleep';
 
 const nodeCron = require('node-cron');
 
-const getRandomInt = (min: number, max: number): number => {
-  const minInt = Math.ceil(min);
-  const maxInt = Math.floor(max);
-  return Math.floor(Math.random() * (maxInt - minInt) + minInt);
-};
-
 class QuestionnaireCronService {
 
-  growiQuestionnaireServerOrigin: string;
-
-  cronSchedule: string;
-
-  maxHoursUntilRequest: number;
+  crowi: any;
 
   // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
   constructor(crowi) {
-    this.growiQuestionnaireServerOrigin = crowi.configManager?.getConfig('crowi', 'app:growiQuestionnaireServerOrigin');
-    this.cronSchedule = crowi.configManager?.getConfig('crowi', 'app:questionnaireCronSchedule');
-    this.maxHoursUntilRequest = crowi.configManager?.getConfig('crowi', 'app:questionnaireCronMaxHoursUntilRequest');
+    this.crowi = crowi;
   }
 
   setUpCron(): void {
-    const maxSecondsUntilRequest = this.maxHoursUntilRequest * 60 * 60;
-    this.questionnaireOrderGetCron(this.cronSchedule, maxSecondsUntilRequest);
+    const cronSchedule = this.crowi.configManager?.getConfig('crowi', 'app:questionnaireCronSchedule');
+    const maxHoursUntilRequest = this.crowi.configManager?.getConfig('crowi', 'app:questionnaireCronMaxHoursUntilRequest');
+
+    const maxSecondsUntilRequest = maxHoursUntilRequest * 60 * 60;
+    this.questionnaireOrderGetCron(cronSchedule, maxSecondsUntilRequest);
   }
 
   questionnaireOrderGetCron(cronSchedule: string, maxSecondsUntilRequest: number): void {
+    const growiQuestionnaireServerOrigin = this.crowi.configManager?.getConfig('crowi', 'app:growiQuestionnaireServerOrigin');
     nodeCron.schedule(cronSchedule, async() => {
-      const secToSleep = getRandomInt(0, maxSecondsUntilRequest);
+      const secToSleep = getRandomIntInRange(0, maxSecondsUntilRequest);
 
       await sleep(secToSleep * 1000);
 
       try {
-        const response = await axios.get(`${this.growiQuestionnaireServerOrigin}/questionnaire-order/index`);
+        const response = await axios.get(`${growiQuestionnaireServerOrigin}/questionnaire-order/index`);
         console.log(response.data);
       }
       catch (e) {

+ 5 - 0
packages/app/src/utils/rand.ts

@@ -0,0 +1,5 @@
+export const getRandomIntInRange = (min: number, max: number): number => {
+  const minInt = Math.ceil(min);
+  const maxInt = Math.floor(max);
+  return Math.floor(Math.random() * (maxInt - minInt) + minInt);
+};