hes*_*wky 3 javascript node.js cron-task typescript nestjs
我正在构建一个通知触发方法,并希望每天在特定时间运行 3 次。
我已经检查了文档,但不理解正则表达式代码以及如何根据需要自定义它!
现在我的方法如下所示:(现在它在第 45 秒每分钟运行一次以进行测试)我需要它,例如每天下午 6 点、晚上 8 点、晚上 11 点运行
@Cron('45 * * * * *')
async sendNotificaiotns() {
console.log('sending notification');
try {
const randomArticle = await this.articleRepository
.createQueryBuilder()
// .select("id")
// .getMany();
// .select("id")
.orderBy("RAND()")
.limit(1)
.getOne();
const input = new NotificationBySegmentBuilder()
.setIncludedSegments(['Active Users', 'Inactive Users'])
.notification() // .email()
.setAttachments({
data: {
...randomArticle,
},
big_picture: encodeURI(randomArticle.image)
})
.setHeadings({ 'en': randomArticle.title })
.setContents({'en': randomArticle.excerpt })
.build();
console.log(randomArticle);
await this.oneSignalService.createNotification(input);
} catch (e) {
console.log(e);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要在下午 6 点、晚上 8 点和 11 点运行它,请尝试这样的操作。(使用您的时区)
var cron = require('node-cron');
cron.schedule('0 18,20,23 * * *', () => {
console.log('Runing a job at 6pm,8pm and 11pm at America/Sao_Paulo timezone');
}, {
scheduled: true,
timezone: "America/Sao_Paulo"
});
Run Code Online (Sandbox Code Playgroud)
我在这里使用了node-cron。 https://www.npmjs.com/package/node-cron
希望这可以帮助。
请将您的 cron 更改为以下给出的内容:
@Cron('0 0 18,20,23 * * *')
Run Code Online (Sandbox Code Playgroud)
上面给出的 cron 将在每天下午 6 点、晚上 8 点和晚上 11 点运行您的函数。
| 归档时间: |
|
| 查看次数: |
10951 次 |
| 最近记录: |