Hacker News
More dynamic cronjobs
hiAndrewQuinn
|next
[-]
The next sleep interval would be calculated probably as as t = -\lambda \ln(U) (where U is a uniform random variable). This way you ensure that the probability of the job firing in the next 10 seconds is the same whether the last job finished an hour ago or just five seconds ago. But \lambda remains the average amount of time between jobs.
It’s compelling to me because it solves thundering herd problems at the architectural level, and also because it simply seems like a lot of fun to have to code very defensively against such chaos. Switching back to a deterministic schedule after surviving such chaos probably leads to a much more robust system overall.
bayesnet
|root
|parent
|next
[-]
Nit: you’re not relying on the memoryless property here but just plain old independent sampling. You’re right that memorylessness means that the elapsed time since the last job provides no information on when the job fires next, but this is orthogonal to the independence of the sleep intervals.
jpalomaki
|next
|previous
[-]
*/5 * * * * flock -n /var/lock/myjob.lock /usr/local/bin/myjob.sh
chillaranand
|root
|parent
|next
[-]
https://avilpage.com/2024/08/guide-systemd-timer-cronjob.htm...
cluckindan
|root
|parent
|previous
[-]
atherton94027
|root
|parent
|next
[-]
If you're thinking about using NFS, why would you want to distribute your locks across other machines?
cluckindan
|root
|parent
[-]
Sometimes certain containerized processes need to run according to a schedule, but maintainers also need a way to run them manually without the scheduled processing running or starting concurrently. A shared FS seems like the ”simplest thing that could possibly work” distribution method for locks intended for that purpose, but unfortunately not all cloud storage volumes are strongly consistent, even to the same user, and may take several ms for the lock to take hold.
atherton94027
|root
|parent
[-]
cluckindan
|root
|parent
[-]
Seems easier to have a managed lockfile for each process, diligently checking that the lock has actually been acquired. Performance is not a concern anyway, as long as acquire takes just a few ms we’re golden.
FWIW, it’s not NFS.
garganzol
|root
|parent
|previous
[-]
In other words, it's not the author's problem. It's the problem of a particular storage that may decide to throw the spec out of the window. But even in an eventually consistent file system, the manufacturer is better off ensuring that the locking semantics is fully consistent as per the spec.
AndrewDavis
|next
|previous
[-]
[1]https://metacpan.org/pod/App::Cronjob https://metacpan.org/dist/App-Cronjob/view/bin/cronjob
WolfCop
|next
|previous
[-]
I work at a company with different holidays in certain countries, which would complicate things, and require something more structured than a list of dates. But having that accessible could be useful.
Has anyone tackled that, or come across a solution?
glawre
|root
|parent
|next
[-]
This happens surprisingly often, given that religious dates change and there are holidays/closures for storms in some regions.
krick
|root
|parent
|next
|previous
[-]
What you do instead, is you schedule the cronjob for the most generic case, e.g. each day. And if it does not need to run 3 days before holidays with crescent moon when wind is blowing from the south, it is just the part of business logic of the process, which you write in the any proper programming language that you prefer (or that the system is written in anyway).
Now, how do you manage the list itself depends on the details and I've done all sorts dirty things that one probably shouldn't do (cutting corners), but in the most flexible case it is just some CRUD-type page in your back-office system, with a real UI, and there is a person (usually in the bookkeeping department of the company) who has it among his responsibilities to maintain the schedule. You store it in some proper SQL database and cache it aggressively, so the the myriads of cronjobs don't bother it more than necessary.
godelski
|root
|parent
|next
|previous
[-]
I think the systemd timer would give you the benefit here as you can write the time in varying formats. Timezones, UTC, local, or whatever. That should give you the structure you need, if I'm understanding your problem correctly.
While systemd has more boilerplate than cron I think it has a lot of advantages that make it worth it. Best to just have a skeleton of these jobs (I keep some in my dotfiles) and then you have it. Or have the LLM write it (ironically one of the few instances I'll advocate for letting the AI write the code). You can do everything in the article and so much more.
stackskipton
|root
|parent
|next
|previous
[-]
One just did it with code where all the processes had holiday.json which would be checked at each launch, if it was holiday, it would do no work and exit.
Other one is operator that would monitor if it was supposed to be a holiday and either change systemd or Kubernetes to suspend the jobs.
I'd recommend code over messing with the system, much more flexible.
petepete
|root
|parent
[-]
stirfish
|root
|parent
[-]
recursivecaveat
|root
|parent
[-]
jaredsohn
|root
|parent
|next
|previous
[-]
sublinear
|root
|parent
|previous
[-]
It parses the file using jq and compares its entries with the current time according to GNU date. At the root is the names of the jobs. Each job has its own list of holidays. Each of these holiday items in the job's respective list has keys for the display name of the holiday, the formatted date to compare to, and in a few cases the ISO day-of-week and a string containing a modulo arithmetic function (e.g. don't run the friday before Christmas, etc.).
Sorry, yes that means I call eval on that string and yes that means some of these are repeated in the same file under the arrays for the other jobs. Also, such lists will have to be maintained and the exact observed dates cannot always be known ahead of time beyond about a year since people can change their minds for various reasons (think bank holidays). Depending on your use case you may also want to define a start time and end time for a window of when this should or shouldn't run (i.e. business hours).
I don't know if that helps. I know it's hacky, but I don't think there's a nice way to handle things like "second monday after 4th of july, but if the 4th also happens to be monday then it should instead be the second tuesday". God help you if you also need to handle each holiday being observed in different timezones. At least at the end of the day none of this would be much code, just very terse code dense with meaning.
dherls
|next
|previous
[-]
threemux
|next
|previous
[-]
These days I tend to use systemd timers on Linux though. Despite my love/hate relationship with systemd, timers and service files are really nice.
stevenjgarner
|next
|previous
[-]
rendaw
|next
|previous
[-]
victorbjorklund
|next
|previous
[-]
garganzol
|root
|parent
[-]
test && action
Where 'test' is another shell command that returns 0 or 1. This is not a special cron syntax, it's just the inherent capability of the Unix shell.In any case, this whole approach is very clever and shows the beauty of The Unix Way.