How to get the previous run time in APScheduler? - cron

I know there's job.next_run_time to get the next run, but anyway to calculate what would have been the previous run time?

The triggers don't work backwards, so you would have to add that functionality to them yourself.

Related

I am running python code and the class takes several minutes to run. Is there a way to pause the code mid-run and edit the code?

I am running a class that takes several minutes. There are many times that it is about halfway done when I realize that I would like to print something. Is there a way to add in a print statement without having to stop the run and add it in if the specific function has not run yet? All I have really been able to find is sleep statements, but those would need to have been added before the run and I can't edit the code using a sleep timer.
You cannot pause the execution of the code and overwrite the code to reflect the changes in the forthcoming execution steps. The python file is compiled into a .pyc file which is currently getting executed. Hence even if you manage to pause it somehow, the current execution will resume the execution of the previous executable itself. also the use of sleep function is not actually stopping the execution. The code is still executing.
A good way to go about doing this is to setup logical breakpoints in your code rather than using multiple print statements throughout. You may have a look at this or this to know how to set break points. Although I would recommend using the VsCode debugger for the task.
Thus in conclusion, you cannot pause the script and make edits such that they reflect upon resuming the script's execution. Technically you can stall the execution of the script but that would never allow you to achieve what you intend to do.
The print statement is not your logger; use logging instead
Here are the docs: https://docs.python.org/3/library/logging.html
Here's a good example on how to use Logging: https://realpython.com/python-logging/
Start putting logger statements throughout your code.
a way to pause the code mid-run
Yes, it's called a breakpoint. You'll need to set a breakpoint somewhere in your code. It can even be a conditional breakpoint in that the breakpoint only triggers when a certain condition is met.
You can then look at the variable values in your program's stack.
PyCharm is very good at all of this. I highly recommend it.

Run job repeatedly, but with no overlap and not at precise scheduled times

I have a background task that needs to be run repeatedly, every hour or so, sending me an email whenever the task emitted non-trivial output.
I'm currently using cron for that, but it's somewhat ill-suited: it forces me to choose exact times at which the command is run, and it doesn't prevent overlap.
An alternative would be to run the script in a loop with sleep 3600 at the end of each iteration but this then needs extra work to make sure the script is always restarted after boot and such.
Ideally, I'd like a cron-like tool where I can give a set of commands to run repeatedly with approximate execution rates and the tool will run them "when convenient" and without overlapping execution of different iterations of a command (or even without overlapping execution of any command).
Short of writing such a tool myself, what would be the recommended approach?

Immediate window stops updating on continuous running

I am writing a script with the For loop like this one:
Sub test()
For i = 1 to 200000
'Some code
Debug.print i
Next i
End Sub
When i goes up to to some point the updating of the immediate window stops, whole application looks freezed without response until the 200000 loops are all done.
I have minimized my spreadsheets and disabled ScreenUpdating already.
In terms of successfully finishing a run there is no big deal but I think I should be able follow the progress, like giving me an idea when the run is going to finish. Current I use the Wait function between sections to allow the windows to update to get around the problem but I feel like this is a stupid way to do so... I think the system should be clever enough to allocate resources to update the windows...
Any better idea?
Rather than using Wait, how about using DoEvents?

How to find the time when a Puppet manifest is executed

I'm wondering if anyone knows a good way to get the date and time when a portion of code in a Puppet manifest is actually executed. Sometimes my manifests take a long time to run, and I need to schedule a task to occur soon after the end of the run, no matter when that occurs.
I have tried the time() function, setting a variable using generate() (using the date function on the Puppet master), and even creating a custom fact, but everything I've tried gets evaluated when the manifests are parsed on the server, rather than when they actually execute on the client.
Any ideas? The clients are all Windows, FWIW.
Thanks in advance!
I am not sure I understand what you mean, but you can't get this information during catalog compilation (obviously), so you can't use it to change the way the catalog will be applied.
If you need to trigger another process on the same host, then you should use any IPC mechanism you have available. You can exec anything, and have it happen just after any other resources is applied, so it is just a matter of finding the proper command.

Cron expression for every other day?

I'm setting up a 2 builds in Teamcity, with scheduled triggering using cron expressions.
I want the builds to alternate every other day. I.e., one gets build on one day, then the other one gets built the next day.
Under no circumstance do I want the same build to run 2 days back to back.
Is this sort of scheduling even possible using cron expressions?
This is impossible to do using only cron, but you can still get this behavior with a bit of a workaround. Create a simple script or program in whatever language you prefer that keeps track of the last build program to run. Any time it is run have it run the build that was not run last, then save that one as the new 'last build'. Then, run this program using cron every day.
You'll need to figure out what works for saving the last build in a persistent way, one the simpler approaches would be to use a file.

Resources