Spring scheduled / how zone attribute works in case of daylight saving? - spring

Could someone can explain how zone attribute in spring #Scheduled annotation works in case of daylight saving changes ?
For example: I have task that is running every 4PM Europe/Warsaw zone, application is long running(no restarts).
Does it mean that scheduled task will be automatically updated on 30.10(DST end) ?

Related

String #Scheduled trigged before the set time with long time pass

I made a schedule by #Scheduled
#Scheduled(cron = "0 0 23 * * ?")
but the method trigged 9 PM or 10 PM everyday, it not only 1 second different, it for a long time
log:
2023-01-14 21:04:12,582 INFO c.e.t.b.d.c.DAGController [scheduling-1]
and different trigged time every day
the server runs on k8s
the pod date is correct, the server date is correct
Spring version : 5.1.6.RELEASE
add some infomation
1, #Scheduled used with #Async. There are three schedule task on 8AM, 10PM and 11PM, each one is finished quickly started early from logs. The first task after server starting started on wrong time
2, server and k8s pod use CST time, and time is correct. There is no rule for running time , the running time is different every day, and it is not a full point
3,restart pod ,restart server , repull image from harbor is useless
need help, thanks

DRF datetime field serializer 'Invalid datetime for the timezone' twice a year

I'm importing data from csv to InfluxDB through a Django Rest Framework API endpoint.
The relevant part of the viewset:
if request.method == "PUT":
measurements = InputMeasurementSerializer(data=request.data, many=True)
measurements.is_valid(raise_exception=True)
The serializer:
class InputMeasurementSerializer(serializers.Serializer):
value = serializers.FloatField()
time = serializers.DateTimeField(input_formats=[
"%Y-%m-%d_%H:%M:%S", ...])
The input data is in the form of time value paires for every 15 minutes:
time,value
2021.04.11 00:00:00,0.172
2021.04.11 00:15:00,0.76
2021.04.11 00:30:00,0.678
2021.04.11 00:45:00,1.211
It works fine for all dates, except for the time values on the 28.03.2021 between 02:00-03:00 and on the 25.10.2020 between 02:00-03:00 it throws the exception: Invalid datetime for the timezone "Europe/Budapest".
Could be related to the time setting because of the daylight saving - but I don't see how exactly. Has anyone any clue what could be the problem here?
In my settings.py:
TIME_ZONE = 'Europe/Budapest'
USE_TZ = True
It indeed is because of the daylight savings, explained in the documentation
Even if your website is available in only one time zone, it’s still good practice to store data in UTC in your database. The main reason is Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions happen. (The pytz documentation discusses these issues in greater detail.) This probably doesn’t matter for your blog, but it’s a problem if you over-bill or under-bill your customers by one hour, twice a year, every year. The solution to this problem is to use UTC in the code and use local time only when interacting with end users.

Spring Scheduler didnot run during DST

In our Java application we have Spring scheduler and the configuration is as given below :
<task:scheduled-tasks>
<task:scheduled ref="myHealthCheck" method="getStatus" cron="0 0/5 * * * *"/>
</task:scheduled-tasks>
During DST end on November 3rd, the scheduler (which runs every 5 minutes) didnt run for one hour. Can some one help in handling this during DST start and end.
Thanks
Shaan
This may be related to the time zone configuration.
What is the default time zone of your host? Try running System.out.println(TimeZone.getDefault()) to check.
If the default time zone doesn't support DST (e.g. UTC), you can set the right time zone for the whole JVM by passing the system property:
-Duser.timezone=Europe/Kiev
Alternatively, you can specify time zone for the scheduled task directly.
Unfortunately, Spring Task XSD doesn't have a property to define a time zone (http://www.springframework.org/schema/task/spring-task-4.3.xsd).
But you can use annotations. As of Spring 4, the #Scheduled annotation has a new zone attribute (https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-annotation-support-scheduled):
You can also use the zone attribute to specify the time zone in which the cron expression is resolved.
#Scheduled(cron="*/5 * * * * MON-FRI", zone="Europe/Kiev")
public void doSomething() {
// something that should execute on weekdays only
}

Schedule a task with 'AND' and 'WAIT' conditions with Windows Tasks scheduler

I want a task to start when both of these conditions are met:
Once a week, every week. like every friday at 3PM
When a session is started and not locked
I know how to do 1 OR 2, but I'm not able to schedule the task only when the 2 conditions WILL meet.
In other words, IF the session is locked at 3PM, I don't want the task to start now, but I don't want it to be skipped! I want the task to wait for the session to be opened (or unlocked) and then to start immediately.
So if a session is unlocked only Saturday morning and not Friday, then the task shall execute on Saturday; and then it will repeat the same process next Friday.
I am not sure how to correct specify the conditions such that:
If its Friday at 3 PM and the session is unlocked; the task should run.
If the interval (Friday at 3 PM) has passed, but the task has not run (because the session was not available); then it should run at the very first instance the session is available, irrespective of the date and time.

Heroku Scheduler timezone?

What's the Heroku timezone for Scheduler? It says "UTC" but I'm not sure if that's correct. My tasks are starting at the wrong time despite converting the timezone correctly.
Thoughts?
This question's a bit old, but yes, Heroku Scheduler definitely uses UTC even if you specify a timezone in your app. I tested it with this code:
task :send_test_email => :environment do
if Date.today.strftime("%A").downcase == 'monday'
UserMailer.send_test_email(Time.now.strftime("%Z")).deliver
end
end
I'm in Pacific time zone. I set Heroku Scheduler to run this every 10 minutes, and this email was able to send on Sunday night PST, which is Monday in UTC time. Also, the string I passed to the mailer,Time.now.strftime("%Z"), is set as the subject of the email which indeed read, "UTC"
One potential source of confusion is Daylight Saving Time. For example, UTC is always 8 hours ahead of Pacific Standard Time (PST - used for winter months), and 7 hours ahead of Pacific Daylight Time (PDT - used for summer months). So if your tasks are off by an hour, it could be you measured from the wrong UTC zone.

Resources