首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用cronjob (node-cron)修复mysql错误

如何使用cronjob (node-cron)修复mysql错误
EN

Stack Overflow用户
提问于 2021-06-17 18:26:21
回答 1查看 157关注 0票数 0

我使用node-cron每12点午夜运行一次cron作业,运行的作业是增加用户表上的一些数据,现在的问题是该作业将运行两次。

例如,我想增加15,在12点钟之后,它将变成17而不是16。

我已经尽了最大的努力来检测这个问题,我不能。

当我停止Nodejs应用程序时,MySQL数据库一直在更新,而这里已经关闭了nodejs应用程序,作业正在运行

代码语言:javascript
复制
const CronJob = require('cron').CronJob;
const logger = require("../helpers/logger");
const { updateUser } = require("../helpers/user");
const db = require("../models/db");



const job = new CronJob('* * * * *', async function() {
    
    db.query("UPDATE sponsored_posts SET post_status = 0", (err, data) => {
        if (err) logger.debug(err)
    });

    db.query("DELETE FROM sponsored_posts", (err, data) => {
        if (err) logger.debug(err)
    });


    //EXPIRING USER
    db.query("SELECT * FROM all_users WHERE membership_expired > 0", async (err, data) => {
        if (data.length > 0) {
            //FIlter Through All User
            //Deduct While They Havent Expired
            await data.map(async user => {
                await updateUser({
                        uid: parseInt(user.uid),
                        membership_expired: user.membership_expired - 1
                })
                
            })
        }
    })
    
    db.query("SELECT * FROM all_users WHERE membership_expired = 0", async (err, data) => {
        if (data.length > 0) {
            //FIlter Through All User
            //Set Can Purchase To 1
            await data.map(async user => {
                await updateUser({
                    uid: parseInt(user.uid),
                    can_purchase: 1
                })
                
            })
        }
    });

    
    
    
}, null, false, "Africa/Lagos");

job.start();

EN

回答 1

Stack Overflow用户

发布于 2021-06-17 18:55:11

如果您使用的是Linux系统,则可以使用crontab命令来设置cronJob

  1. 创建sh文件

例如,

-cron.sh

//在这里写下你的cron脚本执行建议

节点youscript.js

//或者

npm运行你的脚本

确保授予了execute权限

chmod +x your-cron.sh

  1. 在crontab

中设置您的cron

使用以下命令在crontab中设置cron

crontab -e

0 0*** /your/absoulute/path/your-cron.sh /your/absoulute/path/logs/your-cron-output.log 2>&1

写入命令并保存使用crontab -l确保它被正确保存

https://crontab.guru/是一个帮助您调度cron的站点。

0 0***平均每天00:00运行Cron。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68017496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档