0

So, I have built this nice application(using camelot) and do not know how to implement a scheduled job on it, that can send emails based on some conditions at regular intervals. I am trying to implement it using schedule but don't know how to make my app call it automatically. here is my code:

import time, schedule


def job():
    print "I'm working..."

schedule.every(10).seconds.do(job)


while True:
    schedule.run_pending()
    time.sleep(1)

When I start my application, nothing works. How do I make my application be aware of this. If possible, I would like this schedule job to execute without even starting my application.

ephraimbuddy
  • 331
  • 2
  • 10
  • Do you need to access your model data (your database) from those jobs? Does your app have a GUI interface? Camelot uses two threads, one for the GUI and one for data access. Your `while` is problably preventing one of those threads to start or continue excecution. – Smasho Feb 04 '15 at 22:14
  • Yes. I want to access my model data and send mail based on the status of an data in the database, And yes, It has GUI interface. Also, if possible, I would like to run this job with camelot batchjob to monitor the status of run jobs – ephraimbuddy Feb 04 '15 at 22:40

1 Answers1

0

If you want to schedule jobs that will interact with the user, you have to use Camelot Actions, which have their own method for executing functions in the model thread, posting the result to the GUI thread, and back...

But you don't need that, you just need to run jobs (functions) that will access the database, create emails and send it, without interacting with the user. This can be done with a completly independent application, without GUI. In order to avoid creating two applications, you can change the behavior using a command line parameter. If you start the application without any params, it will open the GUI as usual, but if you run it with -background, it will only start the schedule loop. Then you can hook your application on system start, executing it with "-background", and you will have the schedule running without requiring your users to start the application.

If later a user starts the application, then you'll have two instances running, the first one running the schedule loop, and the second one with the GUI.

main.py:

if __name__ == '__main__':
    import sys
    if "-background" in sys.argv:

        import background
        background.main()

    else:
        main() #camelot's main

background.py:

from camelot.core.orm import Session
import time, schedule


def job():
    session = Session()
    #Use session to access and manipulate model
    print "I'm working..."

    session.flush() #flush session to DB


def main():
    from camelot.core.conf import settings
    settings.setup_model()

    schedule.every(10).seconds.do(job)

    while True:
        schedule.run_pending()
        time.sleep(1)    

You should run the app with python main.py -background.

Smasho
  • 1,170
  • 8
  • 19
  • This doesn't run as I want because it will be awkward for the app users to be issuing commands – ephraimbuddy Feb 06 '15 at 09:36
  • You can run the app in background, launching it with "-backgroud" when the operating system starts. Then users can open the normal app (with a GUI) just if they want. You said "I would like this schedule job to execute without even starting my application.". You can avoid the GUI to been shown, but if you don't run _something_, how do you plan to do that? – Smasho Feb 06 '15 at 15:22
  • Can you explain more on your first sentence above. I'm confused. – ephraimbuddy Feb 07 '15 at 10:16
  • I've edited the answer. Which OS does your app run on? – Smasho Feb 10 '15 at 05:09
  • On windows, whether you run from source or from a .exe, you can create a shortcut containing "-background", and put it into *startup folder*. There is also some other ways, check [this answer](http://stackoverflow.com/a/4439204/2797506) – Smasho Feb 10 '15 at 20:59