MYSQL changing value of column when certain time is reached - python

I currently have a job scheduler made in python that talks to a mysql database. The scheduler finds any job with the status 0 and run it.
I want add a new column which will be "start_time". So if i want to run a job at 10 o'clock mysql database will pick up on the start_time and when it reaches 10 o'clock it will change the status from 1 to 0.
Does anyone know if this is possible? and if it is how i go about setting this up?

Related

Checking if a flask server is busy or idle

I want my flask server to do a certain task when its idle for some time lets say 5 mins, i.e after 5 mins it will do another job. For this i need something to check if a server is busy or idle. How can I achieve this?
You can save the current datetime in a variable everytime a request is made via the #app.after_request annotation.
Then you need a routine to periodically check if the time difference between now and the saved timestamp is greater than 5 miutes. If so then run your task.
For executing your check every 5 minutes you may need some kind of scheduler which will be started with your application.

Python scheduled task to run every 14 days

I am currently working on a program that needs to run every 14 days. I have looked into Schedule which works fine, but I have a few doubts about how to go about this.
I will create a service which will handle the execution of the python program itself on a CentOS 7 system.
The issue here is that every 14 days I will run a function that generates a lot of email addresses and send them to a support entity. I am afraid that if something unintended happens, and the program restart - the support entity will get spammed with emails outside the time frame in which they should receive emails.
As far as I can tell, Schedule does not have any way of determining if the program has restarted, and therefore a reboot of either the system or the service will cause this behaviour.
Would it be a correct solution to write a date to a text file after each completed function run, and then check that text file once a day to determine whether the function should run or not? This method would survive a service and/or system reboot, but is it a "correct" way of doing it?
****UPDATE**** Having the cronjob run on specific days of the month (for example 1st and 15th.) is not sufficient. This could cause gaps in the data which the program processes. The script makes a call which pulls data from 14 days back, and this is the maximum number of days supported by the script (licensing and stuff, can't be changed so not that important except that it is a limitation). So it need to run on lets say odd or even week numbers (to get 14 days).
Any ideas on how to accomplish this given this new information?.
You should look into the use of cron (or google it yourself if you dont like the link).
I suggest creating a simple Python script that is called by cron every 14 days. The crontab entry could look like the following:
# this will run at 00:01 on the 15th and 30th of every month
1 0 */15 * * /path/to/python/script.py
# this will run at 00:01 on the 1st and 15th of every month
1 0 1,15 * * /path/to/python/script.py
You still could make your script write some sort of result (with maybe a timestamp) to a file, so that you could easily check that file to see if it ran correctly (or log some error info).
# this will run at 00:01 on the 1st and 15th of every month
1 0 1,15 * * /path/to/python/script.py >> /path/to/logfile.log 2>&1
EDIT
You can also configure cron to run every Monday (or another day) if the 1st and 15th of every month are not sufficient. And the script could check a log file to see if it was run the previous Monday to assure it only executes your business logic every 2 weeks.
# this will run at 00:01 once a week on Mondays
1 0 * * 1 /path/to/python/script.py >> /path/to/logfile.log 2>&1

Time condition in python

I have a python script monitor.py that analyzes text log. The log get generated by 'logging' module inside each python script from Scheduled Job and all results get logged to e.g. C:\log.txt file. I scheduled to run monitor.py script every hour. I don't want this c:\log.txt get growing and accumulate hence I think it would be a good idea to delete it sometime after midnight. Note: I don't have other scheduled jobs at night hence it will not have impact.
I want to check the current time, if the time is between 12:00 AM and 1:00 AM i.e. between midnight and 1 AM I will delete C:\log.txt and immidiately generate a new c:\log.txt file. I noticed that Scheduled Job on windows starts not exactly time it was scheduled but a few seconds before hence my prototype would be:
1. check if current time is between 23:59 PM and 1:00 AM
2. in case 1. is 'true' -> delete c:\log.txt and create a new c:\log.txt
My only problem is that I don't know how could create condition like:
1:00 AM < current time > 23:59 PM
Could someone help me on it?
Thanks
I don't want this c:\log.txt get growing
You can use https://docs.python.org/2.7/library/logging.handlers.html#rotatingfilehandler or https://docs.python.org/2.7/library/logging.handlers.html#timedrotatingfilehandler to limit a logfile size.

How could I do this kind of scheduled task in AppEngine Python?

I have a form with a text field for entering a number without decimal
places representing an amount of minutes that will have to be added to the current time
and will be inserted into a table named Alarm.
When the resulting time comes, my web app must make an insert operation over another table.
For example, if the user enters 20 minutes, and the current time is 22:10, the result time
will have to be 22:30 and will be inserted into Alarm table. So, when the 22:30 arrives, a new insert will have to be made over the another table.
How can I do this on AppEngine using Python?
Depending on your requirements, you may also want to consider using Tasks with an eta or countdown.
If you plan to allow users to cancel the action, you'd need to use some type of no-op marker the task checks for before adding to the "other" table. Or, make the task check the Alarm table before performing the add.
Also, note that the countdown / eta are not precise, they are more like polite requests. So if your queues are backing up with tasks, your adds will happen after they are supposed to. (though cron, particularly 1 minute jobs, also periodically suffer timing issues).
The advantage of this method is that you don't have to figure out how to avoid missing work. Each task represents one add (or a related set of adds). Also, if a write fails the task will retry, which is nice.
Cron may be a better solution for your particular problem though.
You've said that you're storing the target time in the Alarm table. So, your cron just has to run every minute (or every 5 or 10, depending on the resolution of your alarms) and check if there's an alarm matching the current time, and if so then do the action.

How to trigger datastore update if condition is met? (Google App Engine - Python)

In my application I have a column display with values y or n. Default value is n and the item is not displayed. When payment is received the value is changed to y and the item is displayed. If the item is not renewed in a month I would like to change it to n. How do I do this?
Schedule a cron job to periodically check and update your data. See http://code.google.com/appengine/docs/python/config/cron.html
The tricky part if calculating the dates; I assume that you know how to change the value itself.
You'll need to store the date of renewal, and check them periodically using Scheduled Tasks With Cron for Python or Java.
Just create a job that changes all expired accounts to n after a certain time period (here, 1 month), and have it run every day, or every hour.

Categories

Resources