life00 / awecron.sh

small and simple cron that is the best option for desktop users
MIT License
3 stars 3 forks source link

Suggestion #8

Closed kurahaupo closed 1 year ago

kurahaupo commented 2 years ago

This isn't actually a code bug, more a documentation suggestion; note that this assumes you've applied the SECONDS patch I suggested in PR #7.

If you want a job to run close to the same time every day, its config file can contain something like this:

local_tz=-28800     # local time is UTC-8 (PST)
cycle=86400         # daily
time_of_day=7200   # run at 02:00 am every day

interval=$(( reftime = SECONDS + 1,
             utc_time_of_day = time_of_day - local_tz,
             seconds_since_last_run = ( reftime - utc_time_of_day ) % cycle,
             next_run = reftime + cycle - seconds_since_last_run,
             next_run - SECONDS ))

or with some algebraic simplification:

local_tz=3600       # local time is UTC+1 (CET)
cycle=86400         # daily
time_of_day=28800  # run at 08:00 am every day

(( interval = cycle + 1 - ( SECONDS + 1 + local_tz - time_of_day ) % cycle ))

or even:

local_tz=3600       # local time is UTC+1 (CET)
cycle=604800        # weekly
time_of_day=28800   # run at 08:00 am
day_of_week=6       #     on Saturdays

# +3 below is because 1 Jan 1970 was a Thursday
(( time_of_week = ( day_of_week + 3 ) % 7 * 86400 + time_of _day,
   interval = cycle + 1 - ( SECONDS + 1 + local_tz - time_of_week ) % cycle ))

suggestion

As a courtesy, you could provide these definitions in your script:

# get local_tz from your $TZ:

printf -v local_tz '%(%z)T'
local_tz=${local_tz/-0/-} local_tz=${local_tz#0}
(( local_tz -= local_tz / 100 * 40, local_tz *= 60 ))

# or just configure it manually:
local_tz=0   # adjust to suit your location

daily_cycle='86401 - ( SECONDS + 1 + local_tz - time_of_day ) % 86400'
weekly_cycle='604801 - ( SECONDS + 1 + local_tz - time_of_day - ( day_of_week + 3 ) % 7 * 86400 ) % 604800'

so that each config file can simply write:

time_of_day=28800  # 8am
(( interval = daily_cycle ))

or

time_of_day=0    # midnight
day_of_week=3  # Wednesday
(( interval = weekly_cycle ))

Accommodating DST is left as an exercise for the reader. :-)

life00 commented 2 years ago

so you suggest conversion of seconds to larger units? unfortunately i committed before seeing this but still i dont think its needed in minimal version of awecron. im personally fine with working with calculator a bit but its up to you and your fork as i mentioned already in the pr 7. i think i am done with code tweaking, i would like you to take a look at improvements.

kurahaupo commented 2 years ago

The reverse really; it allows you to specify daily or weekly cycles in your config files, and converts those to seconds.

kurahaupo commented 2 years ago

The current code with the parentheses is a reasonable way to encapsulate each job. It's a reasonable approach to being "minimal".

life00 commented 1 year ago

hi @kurahaupo! I finally came back to this project. I made a new branch called v1 and I am working on all of the improvements there. My current plan is to complete all the rest of to-do's there in the next year and release a full v1.0 awecron version. And actually I think I have addressed all of your concerns and suggestions. So it would be nice if you could take a look at my changes, comment on them, and suggest improvements (I recommend reading commit descriptions). Anyways its up to you. Also if you will make a decision to contribute then please make issues/pull requests feature specific (not general suggestions) so that it will be easier to manage everything. As I previously said I am planning to work on this release for the next year, so I will not be very active.

I am closing this issue as complete, please continue the discussing in new issues/pull requests.