-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7153 from arafubeatbox/feat/110268-111686-get-que…
…stionnaire-order-periodically feat: 110268 111686 get questionnaire order periodically
- Loading branch information
Showing
8 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import axiosRetry from 'axios-retry'; | ||
|
||
import { getRandomIntInRange } from '~/utils/rand'; | ||
import { sleep } from '~/utils/sleep'; | ||
|
||
const axios = require('axios').default; | ||
const nodeCron = require('node-cron'); | ||
|
||
axiosRetry(axios, { retries: 3 }); | ||
|
||
class QuestionnaireCronService { | ||
|
||
crowi: any; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
constructor(crowi) { | ||
this.crowi = crowi; | ||
} | ||
|
||
setUpCron(): void { | ||
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 = getRandomIntInRange(0, maxSecondsUntilRequest); | ||
|
||
await sleep(secToSleep * 1000); | ||
|
||
try { | ||
const response = await axios.get(`${growiQuestionnaireServerOrigin}/questionnaire-order/index`); | ||
console.log(response.data); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
|
||
}).start(); | ||
} | ||
|
||
} | ||
|
||
export default QuestionnaireCronService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const sleep = (msec: number): Promise<void> => new Promise(resolve => setTimeout(resolve, msec)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters