【问题标题】:Cron Logic (Lua)Cron 逻辑 (Lua)
【发布时间】:2015-03-15 22:36:17
【问题描述】:

我正在 Lua Jit 中开发像 Cron 这样的 Unix,并且我在算法上遇到了一些问题,也许我认为执行的逻辑顺序或类似的东西不太好,我试图获得一些伪代码来寻求帮助点代码。

如果我收到一个得到一个

`'* * * * *'`

如果我得到一个,我会在每个小时的每一分钟都做这项工作

'20 2 2 1 5'

我需要处理它,并以日期格式或类似的方式做出响应,这不是重要的事情

我的问题是采取所有可能的情况,以正确的方式完成所有过程的逻辑步骤。我不是在寻找特定的代码,只是为了打开我的思想并了解它的实际翻译方式。

【问题讨论】:

    标签: algorithm lua cron crontab cron-task


    【解决方案1】:

    首先,阅读https://en.wikipedia.org/wiki/Cron

    你正在寻找这样的东西:

    找出当前日期时间

    current_datetime = get_datetime()
    -> {year=.., month=.., day=.., weekday=.., hour=.., min=.., sec=..}
    

    了解设置

    given_string = '20 2 2 1 5 user, command'
    cron_config, user, command = parse_cron_config(given_string) -> some object, and cmd
    

    然后检查:

    -- skip for wrong day
    
    if cron_config:weekday_specified() or cron_config:monthday_specified() then
        if cron_config:weekday_specified() and current_datetime.weekday ~= cron_config.weekday then 
            return  -- skip!
        elseif ...
        end
    end
    
    if cron_config:hour_specified() and current_datetime.hour ~= cron_config.hour then
        return
    end
    
    ...
    
    -- finally found that all checks are done and operation should be performed
    
    perform_job(command)
    

    【讨论】:

    • .. 但我认为,从 lua 执行 cronconfigs 不是一个好主意:(
    • 是的,这就是检查我是否在 cron 时间或 cron 时间是否过去的算法,顺便说一句,我使用 time.lua 脚本,可以执行 + - 之类的操作并返回一个新日期,它是从 C 库中解析的。我的问题和我想要的,但我认为它更复杂,它在获得 cron 字符串后返回下一个计划日期。但最后我认为我只会检查当前时间并与我的 cron 时间进行比较。
    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 2020-06-29
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2015-05-01
    相关资源
    最近更新 更多