How to run a function every month in Django? [duplicate] - python

This question already has answers here:
How to use python to schedule tasks in a Django application
(2 answers)
Closed 9 months ago.
I want to make a website where every month a fixed amount will increase in student's database. I want that i'll write a function and the function will run every first day of a month
I Want to Add a certain amount of fees to be added in every individual student account every month.
To do that, I need to run a function in the 1st day of month. How do I run a function continuously after every month?

Creating a function that's triggered on an hourly basis would be a good idea. Scheduled jobs/tasks, like adding monthly fees, are good candidates for this.
You could also look into cron jobs, which would be useful if you're on a Linux server, or task scheduler for Windows.
Here's a decent tutorial on how to make timer triggered cloud functions with AWS:
https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-tutorial.html

I Found a solution by searching here and there...
I need a package to be installed and then that'll do the job!!
Here's the link for that answer.

Related

How to schedule full refresh scripts to run during weekends? [duplicate]

This question already has answers here:
How to execute script on schedule?
(3 answers)
Closed 1 year ago.
I have my python scripts which execute every weekday and refreshes tableau datasets. This is all being done incremental refresh on a daily basis.
Now, during weekends instead of manual refresh, I want these scripts to run with a full refresh. Any ideas on how we can achieve this?
Example:
I have tasks in my script like
#Run Tasks
refresh_tableA()
refresh_tableB()
refresh_tableC()
refresh_tablemain(i)
refresh_tableD()
Where the refresh_maintable() is an incremental refresh. Want that to trigger to a full refresh on weekends.
You need to have two cron entries.
week days only - call the python code and order it to create partial refresh
wekends - call the python code and order it to create full refresh
I think what you are looking for is a scheduler.
You have 2 options here either use Task Scheduler/Cronjob (Based on your OS) or using python library for this.
I think there is library for cronjob in python pycron.
That way you can schedule you scripts easily. let me know if you still dont understand the flow or want code. Most probably you'll find something on github.

Django create a new record for each user every month

The title is self explanatory. I would like to create a new record for a user at the start of each month. What is the best way to achieve this in Django?
My initial thoughts were to run a function that will check the date time every x amount of intervals and then if year and month of last record != then create a new record. Is this even possible? It seems that this constant checking of time would be inefficient but I am struggling to think of another solution.
Am I headed in the right direction?
In web application, the best way to do this is to rely on system task scheduler, like cron in linux, to execute your code at a given time. You can schedule your task every day at midnight, and check for each user if this month's record is there, and create it if it is not there.
Here is an article on how to make cron jobs with django https://gutsytechster.wordpress.com/2019/06/24/how-to-setup-a-cron-job-in-django/

Python: Schedule a task between two times

I'm using schedule to automate a few things and would like one task to be executed between two certain hours of day, twice a day. Basically, what I'm looking for, would be a command like:
schedule.between("08:00", "18:00").do(job)
Is it possible to create a helper function or similar to make this valid?

Is there a way to execute code every hour while still having other code run? [duplicate]

This question already has answers here:
How to repeatedly execute a function every x seconds?
(22 answers)
Closed 3 years ago.
I am new to python. I am making a bot to play Realm Grinder. Right now I am using PyAutoGui to control the mouse to click on certain options. I want my looping code to stop every hour and execute a different line of code. Then I need it to return to the main code and branch off again in half an hour.
I know I can't use sleep to do it and I have considered using a variable, but I don't know how to set up numeric variables yet. The python documentation I have found is very confusing.
Right now I have:
def realmGrinder():\
(code here)
while True:
(code here)
realmGrinder
Use asyncio.sleep(3600) to handle your delay. You'll want multithreading and the asyncio modules.
Have your loop check the current time at convenient intervals, executing the other code when an hour has passed. You'll want datetime for this; if you don't mind slipping a little over an hour on each interval, then simply time.time() will do the job.

How do I do simple conditional calling in Twilio based on time of day?

How do I easily, without complex code, redirect a number in Twilio based on time of day?
At 8;30 am I want the number to redirect to me
at 6:30 pm I want the number to redirect to employee b
at 1:00am I want the number to redirect to the call center?
At weekends from 6:30pm onwards, i want to redirect to the call center
The numbers could be fixed in the code as variables. Just need something to take the hassle out of manually switching who takes calls several times a day.
How do I do this easily without too much coding?
I have basic Python skills, but would be great if I could do this as a Twiml.
Thanks in advance
As #Robert suggests, you can achieve this as implied in this question:
Twilio call forwarding for specific time of day
You can't manipulate the time directly in TwiML, but using your favorite Python framework and time module you could code your own solution where response = "<Dial><Number>A_PHONE_NUMBER_FOR_GIVEN_TIME</Number></Dial>" is set for the appropriate number at the appropriate time.
I also found this example on Github which is likely overkill given your scenario but interesting nonetheless.

Categories

Resources