Scheduler error

406 views
Skip to first unread message

Prasad Muley

unread,
Aug 26, 2014, 2:58:06 AM8/26/14
to web...@googlegroups.com
Hi,
     I've web2py 2.3 version. I tried to use scheduler for mail sending.
I got this error while creating a worker for scheduler

$ python web2py.py -K send_mail_app                                                                                                          [12:07:02]
web2py Web Framework
Created by Massimo Di Pierro, Copyright 2007-2014
Version 2.3.2 (2012-12-17 15:03:30) stable
Database drivers available: SQLite(sqlite2), SQLite(sqlite3), MySQL(pymysql), MySQL(MySQLdb), PostgreSQL(psycopg2), PostgreSQL(pg8000), MSSQL(pyodbc), DB2(pyodbc), Teradata(pyodbc), IMAP(imaplib)
starting single-scheduler for "send_mail_app"...
Traceback (most recent call last):
  File "/home/prasad/Rootpy/web2py 2.3/gluon/restricted.py", line 212, in restricted
    exec ccode in environment
  File "applications/send_mail_app/models/scheduler.py", line 2, in <module>
    mail_scheduler = Scheduler(db)
  File "/home/prasad/Rootpy/web2py 2.3/gluon/scheduler.py", line 449, in __init__
    self.define_tables(db, migrate=migrate)
  File "/home/prasad/Rootpy/web2py 2.3/gluon/scheduler.py", line 501, in define_tables
    migrate=migrate, format='%(task_name)s')
  File "/home/prasad/Rootpy/web2py 2.3/gluon/dal.py", line 7186, in define_table
    table = self.lazy_define_table(tablename,*fields,**args)
  File "/home/prasad/Rootpy/web2py 2.3/gluon/dal.py", line 7222, in lazy_define_table
    polymodel=polymodel)
  File "/home/prasad/Rootpy/web2py 2.3/gluon/dal.py", line 935, in create_table
    self.create_sequence_and_triggers(query,table)
  File "/home/prasad/Rootpy/web2py 2.3/gluon/dal.py", line 1694, in create_sequence_and_triggers
    self.execute(query)
  File "/home/prasad/Rootpy/web2py 2.3/gluon/dal.py", line 1709, in execute
    return self.log_execute(*a, **b)
  File "/home/prasad/Rootpy/web2py 2.3/gluon/dal.py", line 1703, in log_execute
    ret = self.cursor.execute(*a, **b)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 201, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1050, "Table 'scheduler_task' already exists")

I've followed following instructions.

1) Created a file models/scheduler.py

#code from scheduler.py file
from gluon.scheduler import Scheduler
mail_scheduler = Scheduler(db)


def send_mail(to, subject, message, cc=None, bcc=None,
              attachments=None):
    """
    This module is called by scheduler.
    You can check scheduler_task table and scheduler.task_status table
    """
    if cc is None:
        cc = []
    if bcc is None:
        bcc = []
    if attachments is None:
        attachments = []
    #sending mail
    mail.send(to=to, cc=cc, bcc=bcc,
              subject=subject, message=message,
              attachments=attachments)

2) Called send_mail module in an external script.
(scripts/send_mails.py)

#extra code here
task = mail_scheduler.queue_task('send_mail',
                                             pvars={'to': email,
                                                    'subject': subject,
                                                    'message': html_email},
                                             start_time=datetime.datetime.now())
print task


3) Created a worker and got above error


Am I missing some steps?

Niphlod

unread,
Aug 26, 2014, 3:34:06 AM8/26/14
to web...@googlegroups.com
there's yet a scheduler_task table in the database. Fix migrations in the usual way, or set Scheduler(db,....,migrate=False) to avoid the error.

Prasad Muley

unread,
Aug 26, 2014, 3:59:11 AM8/26/14
to web...@googlegroups.com
Thanks.
It works.


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/gx5o0vAGXQQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks and Regards
Prasad M. Muley
Programmer at One Delta Synergies Pvt Ltd. | PICT 2013

“Pretty much everything on the web uses those two things: C and UNIX,” - Dennis Ritchie
                               http://www.cs.bell-labs.com/who/dmr/        

Prasad Muley

unread,
Aug 26, 2014, 5:18:25 AM8/26/14
to web...@googlegroups.com
Hi,
   I tried to access db.scheduler_run table but it is giving me following error in web2py shell and app admin UI.

OperationalError: (1054, "Unknown column 'scheduler_run.scheduler_task' in 'field list'")


On Tue, Aug 26, 2014 at 1:04 PM, Niphlod <nip...@gmail.com> wrote:

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/gx5o0vAGXQQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niphlod

unread,
Aug 26, 2014, 6:16:41 AM8/26/14
to web...@googlegroups.com
I'd strongly advise you to read the section on the book about migrations and fixing broke migrations.
If you're starting now with the scheduler, please do the following:
- delete all databases/*_scheduler_*.table files
- delete scheduler_worker, scheduler_tasks, scheduler_run table from your backend MANUALLY
- use Scheduler(db, ...., migrate=True)
- open appadmin
- reset Scheduler(db, ...., migrate=False)

Prasad Muley

unread,
Aug 28, 2014, 3:30:28 AM8/28/14
to web...@googlegroups.com
Hi Niphlod,
      I need to get current task's id. I saw your sample scheduler example on niphlod_w2p_scheduler_tests
I did it in same way but got error.

#models/scheduler.py

def send_mail(to, subject, message, cc=None, bcc=None,
              attachments=None):
    """
    This module is called by scheduler.
    mail is queued by scheduler.
    """
    if cc is None:
        cc = []
    if bcc is None:
        bcc = []
    if attachments is None:
        attachments = []
    #sending mail
    print W2P_TASK
    result= mail.send(to=to, cc=cc, bcc=bcc,
                      subject=subject, message=message,
                      attachments=attachments)

    #if not result:
    #   print "FAILED: Task [%d]" % W2P_TASK.id
    ### update tasks' status


from gluon.scheduler import Scheduler
mail_scheduler = Scheduler(db, migrate=False)


I got following error

DEBUG:web2py.scheduler:    new task report: FAILED
DEBUG:web2py.scheduler:   traceback: Traceback (most recent call last):
  File "/home/prasad/Rootpy/web2py 2.3/gluon/scheduler.py", line 229, in executor
    result = dumps(_function(*args, **vars))
  File "applications/lcm/models/scheduler.py", line 16, in send_mail
    print W2P_TASK
NameError: global name 'W2P_TASK' is not defined



if mail.send() fails due to some issue [for eg wrong password, port etc]
Still web2py scheduler mark current task as COMPLETED.

That's why I want to check mail.send's result.
If result is False then I've to update current task's status as Failed.
How do I get id of current task?

Thanks in advance.

Niphlod

unread,
Aug 28, 2014, 4:17:28 AM8/28/14
to web...@googlegroups.com
what version are you using ?! W2P_TASK is available from 2.4.1.

Prasad Muley

unread,
Aug 28, 2014, 4:26:42 AM8/28/14
to web...@googlegroups.com
Okay.
I am using 2.3 . I can't upgrade it because Company decided to use 2.3 only.
Is there anyway to access current task id in 2.3?

Niphlod

unread,
Aug 28, 2014, 6:22:47 AM8/28/14
to web...@googlegroups.com
no.

Prasad Muley

unread,
Aug 28, 2014, 6:34:03 AM8/28/14
to web...@googlegroups.com
NP. I got another way to catch failed mail/task.


#models/scheduler.py

def send_mail(to, subject, message, cc=None, bcc=None,
              attachments=None):
    """
    This module is called by scheduler.
    mail is queued by scheduler.
    """
    if cc is None:
        cc = []
    if bcc is None:
        bcc = []
    if attachments is None:
        attachments = []
    #sending mail
    print W2P_TASK
    result= mail.send(to=to, cc=cc, bcc=bcc,
                      subject=subject, message=message,
                      attachments=attachments)

    if not result:
        raise Exception("Mail sent failed")


This is working for me. It will change current task status to "FAILED".
I can easily find which mails sent failed due to mail.send errors

Prasad Muley

unread,
Aug 28, 2014, 6:35:53 AM8/28/14
to web...@googlegroups.com
Pardon me earlier messaeg.
I copied wrong code.


#models/scheduler.py

def send_mail(to, subject, message, cc=None, bcc=None,
              attachments=None):
    """
    This module is called by scheduler.
    mail is queued by scheduler.
    """
    if cc is None:
        cc = []
    if bcc is None:
        bcc = []
    if attachments is None:
        attachments = []
    #sending mail
    result= mail.send(to=to, cc=cc, bcc=bcc,
                      subject=subject, message=message,
                      attachments=attachments)

    if not result:
        raise Exception("Mail sent failed")

This is working for me. It will change current task status to "FAILED".
I can easily find which mails sent failed due to mail.send errors
Reply all
Reply to author
Forward
0 new messages