-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 110268 111686 get questionnaire order periodically #7153
Changes from 5 commits
ea5c026
8fd914b
0646bf1
cf1b63e
d7ddea5
0bb44ac
8ad1092
d5795c1
97e235a
57e4325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -646,6 +646,24 @@ const ENV_VAR_NAME_TO_CONFIG_INFO = { | |
type: ValueType.STRING, | ||
default: null, | ||
}, | ||
GROWI_QUESTIONNAIRE_URI: { | ||
ns: 'crowi', | ||
key: 'app:growiQuestionnaireUri', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
type: ValueType.STRING, | ||
default: null, | ||
}, | ||
QUESTIONNAIRE_CRON_SCHEDULE: { | ||
ns: 'crowi', | ||
key: 'app:questionnaireCronSchedule', | ||
type: ValueType.STRING, | ||
default: '0 22 * * *', | ||
}, | ||
QUESTIONNAIRE_CRON_MAX_HOURS_UNTIL_REQUEST: { | ||
ns: 'crowi', | ||
key: 'app:questionnaireCronMaxHoursUntilRequest', | ||
type: ValueType.NUMBER, | ||
default: 4, | ||
}, | ||
}; | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||
import axios from '~/utils/axios'; | ||||
|
||||
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); | ||||
}; | ||||
|
||||
const sleep = msec => new Promise(resolve => setTimeout(resolve, msec)); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. どっかの /utils あたりにファイル作って移動したいです |
||||
|
||||
class QuestionnaireCronService { | ||||
|
||||
growiQuestionnaireUri: string; | ||||
|
||||
cronSchedule: string; | ||||
|
||||
maxHoursUntilRequest: number; | ||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||||
constructor(crowi) { | ||||
this.growiQuestionnaireUri = crowi.configManager?.getConfig('crowi', 'app:growiQuestionnaireUri'); | ||||
this.cronSchedule = crowi.configManager?.getConfig('crowi', 'app:questionnaireCronSchedule'); | ||||
this.maxHoursUntilRequest = crowi.configManager?.getConfig('crowi', 'app:questionnaireCronMaxHoursUntilRequest'); | ||||
} | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 今回の場合は環境変数でしか設定値を変更しないので大丈夫なのですが、コンストラクタで設定値を初期化してしまうと、設定値を UI 上で変更可能にした場合などに対応できなくなってしまうので、使う時に取得するのがいいかと思います |
||||
|
||||
setUpCron(): void { | ||||
const maxSecondsUntilRequest = this.maxHoursUntilRequest * 60 * 60; | ||||
this.questionnaireOrderGetCron(this.cronSchedule, maxSecondsUntilRequest); | ||||
} | ||||
|
||||
questionnaireOrderGetCron(cronSchedule: string, maxSecondsUntilRequest: number): void { | ||||
nodeCron.schedule(cronSchedule, async() => { | ||||
const secToSleep = getRandomInt(0, maxSecondsUntilRequest); | ||||
|
||||
await sleep(secToSleep * 1000); | ||||
|
||||
try { | ||||
const response = await axios.get(`${this.growiQuestionnaireUri}/questionnaire-order/index`); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. softonic/axios-retry#159
|
||||
console.log(response.data); | ||||
} | ||||
catch (e) { | ||||
console.log(e); | ||||
} | ||||
|
||||
}).start(); | ||||
} | ||||
|
||||
} | ||||
|
||||
export default QuestionnaireCronService; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node 内の cron job 実行のために追加しました。
LICENSE 的に使って大丈夫だと思っています。
THIRD-PARTY-NOTICES はほぼ武井さんしかいじっていなかったので一旦いじりませんでした。