我希望能够创建可以基于固定日期执行的日程表,每天重复,在一周中的特定日期重复,在一年中的特定月份重复,在每年的特定日期重复,并在一天的特定时间。
请问我该如何为这个问题构建数据库表?
编辑#1
基本上,我正在编写一个应用程序,允许用户安排在各种预先配置的时间发送预配置的问候语。我知道我需要一个存储有关时间表信息的表格(例如圣诞节,营销一,... |以及时间表应该运行)。然后是另一个表,用于记录已运行的计划,发送的问候语,人员和电子邮件;基本上是一个交易表。
我的问题是设计Schedule表,因为我希望允许用户在特定日期,一周的特定日期(经常性),每个月的特定日期,每天的特定时间以及每年特定的日/月(例如25/12)。
如何为计划创建一组表格,以灵活的方式处理这些输入?
Microsoft SQL Server具有高效灵活的设计:https://msdn.microsoft.com/en-us/library/ms178644.aspx?f=255&MSPPError=-2147217396
这是我提出的表格结构;
Schedule
- ScheduleName
- ScheduleTypeId (Daily, Weekly, Monthly, Yearly, Specific)
- StartDate
- IntervalInDays
- Frequency
- FrequencyCounter
ScheduleDaily
- ScheduleDailyId
- ScheduleId
- TimeOfDay
- StartDate
- EndDate
ScheduleMonthly
- ScheduleMonthlyId
- ScheduleId
- DayOfMonth
- StartDate
- EndDate
ScheduleSpecific
- ScheduleSpecificId
- ScheduleId
- SpecificDate
- StartDate
...
ScheduleJob
- ScheduleJobId
- ScheduleId
- ScheduleTypeId
- RunDate
- ScheduleStatusId
由于您在谈论计划,我假设您要构建一个批处理应用程序来管理和执行作业。
您可以查看spring batch meta data design以获取参考实现。但是,确切的设计取决于您的要求。这只是一个指针。
我已经阅读了上面的答案,我认为很多事情是不必要的,如果我错了,请纠正我。
以下是我认为应该做的事情:
时间表
其余的字段需要您正在构建的内容。
现在,为此我想每隔30分钟运行一个cronjob(我需要时间输入分开30分钟),它运行一个脚本(在我的情况下是django管理命令),它过滤需要运行的表中的计划:
查询将是这样的:
current_day_of_week = 3
current_day_of_month = 24
current_day_of_year = 114
current_time = 13:30
current_date = 2019-04-24
Filter records that match the below query(not even psuedo code)(I'm using Q objects(https://docs.djangoproject.com/en/2.2/topics/db/queries/#complex-lookups-with-q-objects)
Q(daily AND current_time) OR
Q(weekly AND current_day_of_week AND current_time) OR
Q(monthly AND current_day_of_month AND current_time) OR
Q(yearly AND current_day_of_year AND current_time) OR
Q(fixed AND current_date AND current_time)
为了使您的工作更轻松,您可以将现有的调度程序用于计划部分。在Java中有例如Quartz或我自己编写的库,db-scheduler。
db-scheduler
易于嵌入,并且可以按重复计划(固定持续时间,每天等)对任务进行开箱即用的支持。执行时间保存在单个数据库表中,因此它可以在重新启动后继续存在。