Sunday 8 October 2017

Handling email scheduling by day, time and timezone

I’m going to share my experience of scheduling task using


a. cron job
b.sidekiq
c.sidekiq-scheduler
d.redis
Triggers every hour and look for the day, time and start schedule by timezone.
In detail i’m sharing my problem statement to understand better.
There is a TV channel and its subscribers. The subscriber will be receiving email at starting of the program and ending of the program. My Job is to trigger two emails which are at start time and end time of a particular day, time and location by considering the timezone.
Example
User : John,
Timezone : Asia/Singapore,
start time : 21:02:55 SGT +08:00
end time : 22:02:55 SGT +08:00
days : Monday, TuesDay, Friday

User : Alice
Timezone : Asia/Calcutta
start time : 18:31:13 IST +05:30
end time : 19:31:13 IST +05:30
days : Friday, Sunday



John is at Singapore timezone and he should be receiving the start time email at 21:02 and end time email at 22:02 on Monday, Tuesday, Friday. Similar for Alice.
a. cron to schedule jobs that run every hour
b. Group the list of data by timezone. Result looks like key and values. Key is timezone and values are subscriber list.
c. Loop through the list(timezone), again loop the values and get current dayat time zone, check it for days include current day.
For example days to send email for John is [Monday, Tuesday, Friday]. So current day is Friday look for Friday inside John’s day list.
d. If it does n’t include the day, skip it.
e. If it includes then look for (current_time start_time) leave it if false. If true then look for the status. Here we have status to tract the job has scheduled or started or completed.
f. If status is scheduled or started then leave it. Meaning is that job has already scheduled. Remember cron runs for every hour.
g. If status is completed which means it’s ready for scheduling.
h. Now we are ready to schedule a new job and executing at background using gem ‘sidekiq and gem ‘sidekiq-scheduler’.
Worker.perform_in(start_time, 'send_start_email')
Scheduling the stop email at same time.
Worker.perform_in(end_time, 'send_stop_email')
Updating the status and job id for future reference. It’ll be helpfull if scheduling is changed once Job is scheduled.

Handling the job once job is Scheduled.



Scenario one : Job is scheduled and didn’t start
I mentioned that each job id is stored for future reference.
By using the job ids (start and end job ids) we ‘ll able to find the scheduled job and delete it. Again look for updated days, time and same process mentioned above.
Scenario two : Job is scheduled and started (start email has been sent)
By using the job id (end job id) we’ll able to find the end scheduled job,delete it and send the email at same time saying ended now. Again look for updated days, time and same process mentioned above.
Scenario three : Job is scheduled, started and ended.
No need to look for job ids. It’ll be updated and scheduled.
Scenario four : Job is not scheduled.
No need to look for job ids. It’ll be updated and scheduled.

No comments:

Post a Comment