"重复任务"的设计选项

Cra*_*aig 5 sql sql-server

我正在编写一个处理人员任务的小应用程序.非常简单,但就桌面设计而言,我所坚持的区域是一个重复性任务的情况,可以是一次性,每天,每周或每月.如果每周一次,那就是特定的一天,每周一次.每月是特定的一天.

我有一个任务表和一个recurring_type_id,并且将在代码中处理重复的任务,但这是理想的方法吗?另一种方法是在创建任务时插入所有任务 - 每个事件时间.但这似乎也不正确.

任何人都可以就设计提出建议,以及如何以可维护和有效的方式处理这个问题?

我正在使用SQL Server 2008 R2

Lan*_*nce 13

我会创建一个任务表来插入我的任务.

taskTable
|taskID  |Freq   |runAt       |
-------------------------------
|1       |daily  |1           |
|2       |daily  |0           |
|3       |weekly |5           |
|4       |weekly |1           |
|5       |monthly|15          |
|6       |monthly|1           |
|7       |once   |2013-7-4    |
Run Code Online (Sandbox Code Playgroud)

RUNAT的样片没有考虑过这样没关系进入什么样的价值.

每周项目的runAt 是任务运行的星期几.

runAt for mothly是任务运行的月份的那一天(月末任务我通常在第一天运行,因为这样可以节省处理月份结束的那天的麻烦,尽管你可以用它来解决这个问题

lastDayOfMonth = datePart(d,dateadd(s,-1,dateadd(mm, datediff(m,0,getdate())+1,0)))
Run Code Online (Sandbox Code Playgroud)

runAt for once是任务运行的实际日期.

然后我创建一个每天运行的任务,看看需要运行什么.

select  taskID
from    taskTable
where   (freq = 'once' and runAt = convert(varchar(10),getDate(),21))
  or    freq = 'daily'
  or    (freq = 'weekly' and runAt = datePart(dw,getDate()))
  or    (freq = 'monthly' and runAt = datePart(d,getDate())
Run Code Online (Sandbox Code Playgroud)

此查询为我提供了我需要运行的任何任务的所有taskID.

不确定这是否是你想要的,但希望你能找到有用的东西.


Ati*_*gur 6

我会尝试使用经过试验和测试的解决方案.Unix Cron Table解决方案.许多其他工具(如Hudson/Jenkins)也使用此解决方案.

来自维基百科.

*    *    *    *    *  command to be executed
?    ?    ?    ?    ?
?    ?    ?    ?    ?
?    ?    ?    ?    ?
?    ?    ?    ?    ?????? day of week (0 - 7) (0 or 7 are Sunday, or use names)
?    ?    ?    ??????????? month (1 - 12)
?    ?    ???????????????? day of month (1 - 31)
?    ????????????????????? hour (0 - 23)
?????????????????????????? min (0 - 59)

它还允许条目的快捷方式名称.

Entry      Description                                                             Equivalent To
@yearly    Run once a year at midnight in the morning of January 1              0 0 1 1 *
@annually   
@monthly   Run once a month at midnight in the morning of the first of the month    0 0 1 * *
@weekly    Run once a week at midnight in the morning of Sunday                     0 0 * * 0
@daily     Run once a day at midnight                                               0 0 * * *
@hourly    Run once an hour at the beginning of the hour                        0 * * * *

从这里下表设计.

taskID  cronExpression preDefinedDefinition
 1       30 * * * *      null
 2       0 * * * *      @hourly
 3        ...            ....

@Timothy Britt提到这个解决方案并不能解释一次性问题.那是真实的.Linux和/或Unix也有一个叫命令.似乎在商店中它的条目在一个单独的文件和单独的deamon监视此文件.如果我们想在此表中包含一次性作业.我们可以添加额外的布尔列OneTimeOnly.或另一个仅包含一次性作业的表.