Managing timezones for different users - timezone

I have a website where I have many users coming from different countries. Users can schedule a task based on their timezone. Now there is a cron running on the server after every min, the cron executes a script which checks if there are any scheduled task of any user and if so it does the needful.
Since my server is based in the US, the script executed by the cron considers the timezone of the US. What do I have to do in my script that will execute the user's task based on user's timezone instead of server's timezone?
Thanks in advance for any ideas

Lookup the user's timezone.
Compute the current time in the user's timezone.
For each job, look up the last time it was run and compute the next time it should run.
For any job whose next run time is now or in the past, run that job and update the record of the last time each job was run.

I did something similar on the iPhone a few months ago.
My solution was to capture the time as a string. So if the user selected 8am, I would just capture 08:00 and their time zone e.g. Europe/London.
Every 5 minutes or so on my server, I could then convert this 08:00 into the current UTC time based on the timezone. If this time was "present", I would carry about a check on the user's transport status and issue alerts.
To help me with the TimeZones, I used NodaTime. http://noda-time.blogspot.co.uk/

Related

How to prevent edge cases or delay for a deadline of a scheduled task?

Suppose I have an event scheduled in the future with start_date, end_date (inclusive), timezone, and status. The dates are wall-time since users are from all of the places with different timezones. There will be incoming transactions and I'd like to check the validity of these transactions against the status of the task.
If the status of the task is active then it's still valid. What I'm planning to do is to setup a cron job (or ActiveJob in Rails) to run every 15 minutes to check whether the task has already started or ended according to the user's timezone setting and update the status field.
The problem is that suppose the task's end_date is 15 December and the cron job begins at exactly 16 December 00:00 at which the task should be already expired and the cron job takes approximately 2 minutes to complete the whole database. Then the status will updated to inactive at 16 around December 00:02. If there is a transaction coming in at 16 December 00:01 and then the application checks the validity against its status which is still active but in fact it's already past the deadline.
Any ideas how to combat this problem?
By the way, although the application itself is not really serious and can afford some mismatches, I'm still worried about the data intregrity when querying since there will be some datapoints missing out of the valid date range.
Cron won't work since it creates the problem you describe, but with activejob you can tell it the exact date and time to run https://edgeguides.rubyonrails.org/active_job_basics.html#enqueue-the-job
By the way, if something depends on a date, why don't you actually check that date instead of the "status" column?

User faced scheduler for rails

I need to implement user faced scheduler, like users have reports and might choose schedule when they want those reports being sent to them.
Requirements are quote complex, like there should be schedules like each 12 hours, each 30 minutes, each second day, at Fridays at 1am, last Sunday of the months etc.
Is there Rails solution for that our should I create it from the ground?
Thanks!
Most schedulers for rails and ruby depend on a static file. You can use a queuing system like Delayed Job and make every job enqueue itself for next time after success. Or you can do a basic SheduledJob model which relates to the user, and stores the periodicity, next execution and last execution. And use a normal (frequent) scheduled task engine like clockwork to check for pending jobs.

Ruby on Rails - Automated Email Sending - Flight Reminder

I'm creating flight booking website in Rails. Booking information is stored in database in the following table:
USERNAME | FLIGHT FROM | FLIGHT TO | DATE OF FLIGHT | TIME OF FLIGHT | some additional information not relevant to this task ... |
I'm looking to send an email an hour (or some specific time) before the TIME OF FLIGHT on a DATE OF FLIGHT. What is the best approach to do it ? I was looking into Cron and delayed_job however both seem to be based more on intervals rather than executing a job at specific date and time.
Please help.
Thank you
The simplest approach is just to have a cron job set to run every 10 minutes and determine via a database query which flights now require a reminder e-mail. You can have an additional field in the database such as "REMINDER_SENT" so that you only send an e-mail once.
If you are already using delayed job then the cron job should just call a ruby script which adds a SendReminders job on to the queue. You can then manage all of the db querying, e-mail sending and db updating from a normal delayed job.
This approach saves you having to queue up a large number of future dated events and you don't need to worry about flight times changing or events getting lost. If you miss one event then the next run in 10 minutes will pick up all the flights anyway.
Are you required to send those notifications exactly one hour (or another time) in advance?
If not I would create a cron job that calls a rake task, say every 10 minutes. This task checks if there are notifications due and sends them. If you expect them to arrive 60 minutes before, with these settings you have a delivery timeframe between 60-70 minutes in advance, given the delays imposed by spam filters etc I think this is reasonable.
If you call the script more often (every minute), the precision is higher, but you might have trouble with concurrently running tasks.

Should Postgres timezone be changed from UTC?

I have a cron that runs a script in my rails app hourly looking for new transactions to run. Typically this only picks up new transactions set to start on the current day, until midnight UTC, when all the following day's transactions become due. My rails app is operating in Central Time, but Postgres is set to UTC. Since I'm using a scope with a where clause, and now() to compare the dates, transactions for the new day run in the early evening (in the US). Is there a downside to switching Postgres's timezone to Central Time? From what I can tell, it will record all timestamps in UTC regardless, but will run queries based on its timezone setting.
You should always use UTC inside the application, and only perform conversions to other timezones at the edge of the application where necessary - such as in controllers or views.
In your query, perform some date manipulation on now() to translate it to the timezone you need.

how to start/run a windows service at particular time (say: 12:00 PM) every day?

Background: I have developed a windows service which runs once every day and does the work (every thing is just right!)
Issue: the service runs exactly at the same time (last run time) the other day..
Ex: If I start the service at 4:00 PM today so it will run again at 4:00 PM every day.
My solution till yet (but not working fine) I want it to configure it self with the StartTime and IntervalTime mentioned in the app.config file and run at that time everyday (regardless of when I have started it)
Having problems with this silly thing .. Please help in this regards with sample code.
Your help is really appreciated, Thanks in advance.
For further clarification, if I have mentioned in app.config
key="StartTime" value="12:00"
key="Interval" value="86400000"
so it must run on 12:00 PM every day (as interval period is 86400000 milliseconds i.e. 1day) regardless of the time when I started it or make it live.
You could set up a scheduled task that starts the service at a specific time and stops it at another via a batch file. You would utilize the commands net stop myservice and net start myservice in the batch file.

Resources