javascript - 将带参数的函数传递给 cronjob
问题描述
我们在 typescript 项目中使用cron包。我看到的使用模式如下:
function jobUpdateVerStat() { // defined };
jobExecuteHourly = new CronJob('0 1 * * * *', jobUpdateVerStat);
我需要将参数传递sess
给jobUpdateVerStat
. 因为这不起作用
const sess = getSess();
jobExecuteHourly = new CronJob('0 1 * * * *', jobUpdateVerStat(sess));
我查看了Cronjob
看起来像这样的构造函数
constructor(cronTime: string | Date | Moment, onTick: CronCommand (..some more parameter..))
CronCommand
的声明类型是这样的
export declare type CronCommand = (() => void) | string | { command: string, args?: ReadonlyArray<string>, options?: SpawnOptions};
因此,我根据此信息尝试了以下操作,但似乎都不起作用...这是否仅意味着该程序包不支持带参数的函数的预定运行?通常不希望将参数传递给 cronjob 还是我错过了一个明显的提示来完成这项工作?
// function updateVersionStatus(sess) was properly defined
const sess = await createBotSess();
let job1: CronJob;
let job2: CronJob;
job1 = new CronJob('0 */1 * * * *', {
command: 'updateVersionStatus',
args: ['sess'],
});
job2 = new CronJob('0 */1 * * * *', 'updateVersionStatus(sess)');
解决方案
推荐阅读
- c# - 为什么双向流在 CompleteAsync 之前被阻塞
- reporting-services - SSRS IIF 和开关功能
- laravel - laravel 验证检查电子邮件或用户中是否存在输入值
- amazon-s3 - 为 Athena 分区重命名 S3 文件夹结构
- firebase - Firebase Functions CLI 中的 Firebase Admin SDK - 获取访问令牌时出错
- button - Netsuite:如何将“addMarkAllButtons()”缩小为仅包含一组按钮?
- vb.net - 调用存储在访问表中的 vb.net 函数
- vb.net - 重新打开应用程序时保留最新的组合框选择
- php - 从不同文件夹中的不同页面获取输入名称
- php - 在 Laravel Eloquent 中加入两个模型