2

I have a low traffic internal webpage for which we use for the management of remote computers.

This webpage attempts to register a CRON job, so that once a week at a specified time, an e-mail will be sent to a list, with the current status of the data from the database.

However, in testing, everything appeared fine. When pushing to production, I would test the cron job, it would work. But when leaving the system running over the weekend ( when possibly no one is on the system), the job does not get executed.

I have tried initializing in a few different ways, so that it wouldn't (shouldn't) be tied to any requests on the system, and should register at initialization, regardless of if anyone has hit the system.

mmsver.wsgi

from flask_apscheduler.scheduler import APScheduler
from nydps.web.mmserver import app as application

scheduler = APScheduler()
scheduler.init_app(application)
scheduler.start()

mmserver.py (excerpt)

def weekly_report():
    # doing things with a session
    # send_smtp is a custom smtp function
    send_smtp(smtp_content, destination, smtpSender, 'TMM Directory: Weekly Utilization Report')

app.config['JOBS'] = [{
    'id': 'weekly_report',
    'func': weekly_report,
    'trigger': {
        'type': 'cron',
        'day_of_week': weekly_report_day_of_week,
        'hour': weekly_report_hour,
        'minute': weekly_report_minute
    }
}]

If I go change the report day, hour, minute. Restart the server, it will execute. Only when I set the day hour minute to a time in the slightly further future (where I am not actively on the site), it doesnt appear to be executed.

Any thoughts?

Edit: Production is deployed behind Apache using WAMP on Windows.

Busturdust
  • 2,447
  • 24
  • 41
  • 1
    I had a similar problem a while back with django on Linux. The issue was that Apache log rotation restarted the web server, but didn't restart the wsgi application until it got a request for it. Could you be hitting the same? – Peter Brittain Jul 11 '16 at 22:15
  • Thanks for the input, that's very interesting. I do have log rotation enabled with apache, but I'm not sure how that interacts with restarting the web server? I guess the logical conclusion would be to test it with log rotation off, I will have to try it later this week. – Busturdust Jul 11 '16 at 22:20
  • I was on vacation this past week but I certainly have not forgotton about this thread. Unfortunately, it seems to have missed its scheduled appointment (saturday off time) even with log rotation disabled. I was really hoping to limit any added complexity with things like task scheduler since I have to sell it by a support staff first, some more time with the problem at hand before I go that route I think. – Busturdust Jul 17 '16 at 15:54
  • 1
    At this point, all I can think of is to try [catching the Python interpreter shutdown](https://code.google.com/archive/p/modwsgi/wikis/RegisteringCleanupCode.wiki#Cleanup_On_Process_Shutdown) and seeing when it gets terminated, then following it back from there... – Peter Brittain Jul 17 '16 at 16:48

2 Answers2

1

IMHO, it is not a good idea to used Apache + Webapp to run scheduled tasks.

If your are under Windows, why don't you add a task on the Task Scheduler? You can run Python scripts (using a BAT script).

  1. Create a standalone Python application,
  2. Deploy the application on Windows,
  3. Create a new task in the Task Scheduler.

Create a standalone Python application

You can develop on Linux or Windows, but you need to prepare your release on Windows:

First, install Python on Windows, with pip, setuptools and wheel. Add C:\{PYTHON_INSTALL_DIR}\Scripts to your PATH for simplicity.

As administrator, create your target directory structure in C:\Program Files\{YOUR_COMPANY}.

With a MS-DOS console, create a virtualenv and activate it:

C:
CD C:\Program Files\{YOUR_COMPANY}
virtualenv {YOUR_APP_NAME}
{YOUR_APP_NAME}\Scripts\activate

Install your application:

pip install {YOUR_APP_NAME}   # if on PyPi
pip install {YOUR_WHEEL}      # if on another source (ex.: a file)

Now, you can compress the C:\Program Files\{YOUR_COMPANY} folder in a ZIP file and publish it: ex.: "{YOUR_APP_NAME}-py2-win64.zip".

Deploy the application on Windows

On windows, download your ZIP file "{YOUR_APP_NAME}-py2-win64.zip" and uncompress it.

As administrator, copy the uncompressed folder {YOUR_COMPANY}\{YOUR_APP_NAME} to C:\Program Files\{YOUR_COMPANY}.

Run your installation script if you have one, or update configuration files.

Create a new task in the Task Scheduler

The Python executable path will be:

"C:\Program Files\{YOUR_COMPANY}\{YOUR_APP_NAME}\Scripts\python.exe"

You can also consider your entry point if you declare one in setup.py.

Community
  • 1
  • 1
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
0

Why are you complicating the issue by trying to launch a cron job like this? Just save the hassle and write directly to the crontab with python-crontab:

from crontab import CronTab 
cron = CronTab(user = True) #user='username' to specify a different account

job = cron.new(command='python /home/usr/script/script.py')
job.week.every(1)
job.enable()
if job.is_enabled():
    print 'success!'
Callam Delaney
  • 641
  • 3
  • 15