Database table design for scheduling tasks

后端 未结 5 1317
悲哀的现实
悲哀的现实 2020-12-08 05:09

I want to be able to create schedules that can be executed based on a fixed date, repeated daily, repeated on a particular day of the week, repeated on a particular month of

5条回答
  •  离开以前
    2020-12-08 05:45

    I have read through the answers above and I think a lot of things are unnecessary, correct me if I'm wrong.

    Here is what I think should be done:

    Schedule


    • Id

    • type (Daily, monthly, weekly, fixed, yearly) - Enum

    • frequency (Can be 1-7[days of week], 1-30(or 28)[days of month], 1-365[days of year] or null(for daily, fixed) - ArrayField(of ints) - [1, 7] OR [23] OR [235]OR null

    • time (time of day in UTC) - ArrayField(of Char strings - ['9:00', '13:30']

    • date (for fixed type) - datetime - 2009-03-21

    • is_active (boolean) - for enabling, disabling the schedule

    • name (CharField) - If you want to name the schedule

    Rest of the fields would require context to what you are building.

    Now, for this I'm thinking of running a cronjob every 30mins(I'm taking time input separated by 30mins) which runs a script(django management command in my case) which filters schedules from this table that need to be run:

    Query would be something like this:

    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)
    

提交回复
热议问题