Send request in certain date and time in django-cron - django

I want to send one POST request or GET request in a certain date and certain time say date is 1st feb and time is 2:00pm then request should automatically be sent at that date and time. Those date and time are stored in the database. What should be the approach for that?
I tried using django-cron but it runs only when i type python3 manage.py runcrons. i want it to run when server is running i.e after every 5 mins.

i followed this document. I previously didn't follow
> crontab -e
*/5 * * * * source /home/ubuntu/.bashrc && source /home/ubuntu/work/your-project/bin/activate && python /home/ubuntu/work/your-project/src/manage.py runcrons > /home/ubuntu/cronjob.log
now it is working properly.

Related

Superset chart email schedules and celery beat schedule. Why report is sent with celery beat schedule instead of chart email schedules?

My configured email report is named "Raw player games" with crontab */20 * * * * (At every 20th minute I will expect a report in my email box). Look in the screenshot raw player games.
Another crontab is configured in main superset config - superset_config.py
# superset_config.py
CELERYBEAT_SCHEDULE = {
'email_reports.schedule_hourly': {
'task': 'email_reports.schedule_hourly',
'schedule': crontab(minute=1, hour='*'), # At minute 1, every hour
},
}
I receive emails, but only one per hour. I don't see any errors in logs, all jobs in celery flower are in success state.
apache-superset==0.37.2
celery==4.4.7
Why?
Why superset send me reports only once in a hour? How to reconfigure superset to correctly handle my crontabs, what I missed?
Note that your beat schedule is configured to run hourly. So on every minute one of each hour, beat is going to enqueue a new job that will verify if it's time to send a new report. It will not matter to have a thinner resolution configured on superset itself.
Yet by default email reports functionality has an hourly resolution:
https://github.com/apache/incubator-superset/blob/master/superset/tasks/schedules.py#L823
This default can be changed by configuring:
EMAIL_REPORTS_CRON_RESOLUTION

Django post-office setup

Perhaps it is just because I've never set up an e-mail system on Django before, or maybe I'm missing it... but does anyone have any insight on how to properly configure django post-office for sending queued e-mails?
I've got a mailing list of 1500 + people, and am hosting my app on heroku - using the standard email system doesn't work because I need to send customized emails to each user, and to connect to the server one by one leads to a timeout.
I've installed django-post_office via pip install, installed the app in settings.py, I've even been able to get an email to send by going:
mail.send(['recipient'],'sender',subject='test',message='hi there',priority='now')
However, if I try to schedule for 30 seconds from now let's say:
nowtime = datetime.datetime.now()
sendtime = nowtime + datetime.timedelta(seconds=30)
and then
mail.send(['recipient'],'sender',subject='test',message='hi there',scheduled_time=sendtime)
Nothing happens... time passes, and the e-mail is still listed as queued, and I don't receive any emails.
I have a feeling it's because I need to ALSO have Celery / RQ / Cron set up??? But the documentation seems to suggest that it should work out of the box. What am I missing?
Thanks folks
Actually, you can find this in the documentation (at the time I'm writing this comment):
Usage
If you use post_office’s EmailBackend, it will automatically queue emails sent using django’s send_mail in the database.
To actually send them out, run python manage.py send_queued_mail. You can schedule this to run regularly via cron:
* * * * * (/usr/bin/python manage.py send_queued_mail >> send_mail.log 2>&1)

How to setup my crontab in order to execute process_tasks in Django?

I decided to use a library for my Django project called django-background-tasks (link to the documentation: https://django-background-tasks.readthedocs.io/en/latest/). I want to deploy my Django application to a Linux server (Ubuntu 19.0.4). How should I write the crontab in order to call the command "process_tasks" every five seconds?
Here Running a cron every 30 seconds is a workaround to achieve the seconds part, but since I am new to this part of the job (deploying and automation of process), how should I create my crontab file in order to achieve my desired purpose?
I'll be using process_tasks for a lot of different functionalities like: do some analysis at night and send results in the morning, expire some codes, etc. So basically I'll need to be running it almost constantly.
Thank you in advance for any suggestion, if you need something more I would be happy to provide it to you.
Since you know cron only allows for a minimum of one minute but you want to run it every 5 seconds.
how about writing a shell script (running as a service via supervisor) that runs your task in an infinite loop. This script will sleeps after every run for 5 seconds.
The only difference between this service and a cron is:
A cron will fork a process every time it runs, a process runs your job and periodically check if the job is finished in order to clean it up, and this script (as service) will not work like cron but it'll do the job I believe.
#!/bin/bash
while true; do
# your code here,
# call your python file which initializes
# django variables (or whatever you want) and do the needful.
sleep 5;
done
You can configure a file process_django_tasks.sh(this file contains above code) with supervisor as a service so it runs on boot time and you have a control to start and stop with quick commands.
To test quickly you can easily run sh process_django_tasks.sh
If you could tell us why exactly you want your script to run every 5 seconds. Maybe we all suggest a better way than running a script every 5 sec.
I'd recommend using Supervisor (http://supervisord.org) to run python manage.py process_tasks, which will then monitor for the tasks you scheduled in your code and run them based on their repeat syntax:
To run your task every 5 seconds:
function_to_call(var, repeat=5, repeat_until=None)
I'd also recommend Supervisor to run your entire Django project.
Looking at the documentation the process_tasks command by default runs every 5 seconds checking for new tasks to run. Not sure if you just wanted to check every 5 seconds or actually run a specific task every 5 seconds.
Not knowing any details, this may be a completely inapplicable alternative. However, if there is a guarantee that there will be nothing to be done until (an) instance(s) of (a) particular model(s) is saved, and provided that this save doesn't happen far too frequently, then you might look at Django post_save signals instead. Whenever that save happens, either execute the task, or execute the task if it wasn't run already in the last five seconds.

Sending data from cakephp automatically using date and time

I created a web services using cakephp that store on mysql some data including date and time provided by an android app.
now I want send data to another device on that precise date and time (kind of cron job ) but I didn't find how to do that !
any ideas how to solve that?!
Yes, that's possible using a cronjob per minute
Command for the cronjob
* * * * * wget -q --spider http://example.com/controller/function
In the controller function check that precise date and time against the current date time, and they match, perform your action
$current_time = date("Y-m-d H:i:s");
$precise_time = "2017-05-12 9:52:00"; // for example
if($current_time == $precise_time){
// Send data to another device
}

S3ResponseError: S3ResponseError: 403 Forbidden

<RequestTime>Mon, 14 Mar 2011 10:09:28 GMT</RequestTime>
<ServerTime>2011-03-14T09:09:29Z</ServerTime></Error>
reason: The reason of this problem is that Amazon S3 allows only a small time stamp variation up to 15 minutes between the server and its requesting client (user pc). As Amazon is a big backup server of large number of users, security does matter a lot.
solution: I installed ntp on my ubuntu machine and try to sync it with s3. But still throwing same error.
How can I solved it.
My project is in Django
Make sure you use UTC time for your requests. From the AWS docs:
Request Elements
Time stamp—Each request must contain the date and time the request
was created, represented as a string
in UTC.
I had the same problem: Update your date with the following:
rdate -s ntp.xs4all.nl
substitute with whatever ntp server you require.