You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

123 line
3.4 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. """
  5. Scheduler will call the following events from the module
  6. `startup.schedule_handler` and Control Panel (for server scripts)
  7. execute_always
  8. execute_daily
  9. execute_monthly
  10. execute_weekly
  11. The scheduler should be called from a cron job every x minutes (5?) depending
  12. on the need.
  13. """
  14. import webnotes
  15. def execute(site=None):
  16. """
  17. execute jobs
  18. this method triggers the other scheduler events
  19. Database connection: Ideally it should be connected from outside, if there is
  20. no connection, it will connect from defs.py
  21. """
  22. from datetime import datetime
  23. import webnotes.utils
  24. format = '%Y-%m-%d %H:%M:%S'
  25. if not webnotes.conn:
  26. webnotes.connect(site=site)
  27. out = []
  28. nowtime = webnotes.utils.now_datetime()
  29. last = webnotes.conn.get_global('scheduler_last_event')
  30. # set scheduler last event
  31. webnotes.conn.begin()
  32. webnotes.conn.set_global('scheduler_last_event', nowtime.strftime(format))
  33. webnotes.conn.commit()
  34. if last:
  35. last = datetime.strptime(last, format)
  36. if nowtime.day != last.day:
  37. # if first task of the day execute daily tasks
  38. out.append(nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - daily:' + trigger('execute_daily'))
  39. if nowtime.month != last.month:
  40. out.append(nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - monthly:' + trigger('execute_monthly'))
  41. if nowtime.weekday()==0:
  42. out.append(nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - weekly:' + trigger('execute_weekly'))
  43. if nowtime.hour != last.hour:
  44. out.append(nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - hourly:' + trigger('execute_hourly'))
  45. out.append(nowtime.strftime("%Y-%m-%d %H:%M:%S") + ' - all:' + trigger('execute_all'))
  46. return '\n'.join(out)
  47. def trigger(method):
  48. """trigger method in startup.schedule_handler"""
  49. traceback = ""
  50. try:
  51. import startup.schedule_handlers
  52. if hasattr(startup.schedule_handlers, method):
  53. webnotes.conn.begin()
  54. getattr(startup.schedule_handlers, method)()
  55. webnotes.conn.commit()
  56. except Exception:
  57. traceback += log(method)
  58. try:
  59. cp = webnotes.bean("Control Panel", "Control Panel")
  60. cp.run_method(method)
  61. except Exception:
  62. traceback += log("Control Panel: "+method)
  63. return traceback or 'ok'
  64. def log(method):
  65. """log error in patch_log"""
  66. import webnotes
  67. if not (webnotes.conn and webnotes.conn._conn):
  68. webnotes.connect()
  69. webnotes.conn.rollback()
  70. traceback = webnotes.getTraceback()
  71. import webnotes.utils
  72. webnotes.conn.begin()
  73. d = webnotes.doc("Scheduler Log")
  74. d.method = method
  75. d.error = traceback
  76. d.save()
  77. webnotes.conn.commit()
  78. return traceback
  79. def report_errors():
  80. from webnotes.utils.email_lib import sendmail_to_system_managers
  81. from webnotes.utils import get_url
  82. errors = [("""<p>Time: %(modified)s</p>
  83. <pre><code>%(error)s</code></pre>""" % d) for d in webnotes.conn.sql("""select modified, error
  84. from `tabScheduler Log` where DATEDIFF(NOW(), modified) < 1
  85. and error not like '%%[Errno 110] Connection timed out%%'
  86. limit 10""", as_dict=True)]
  87. if errors:
  88. sendmail_to_system_managers("ERPNext Scheduler Failure Report", ("""
  89. <p>Dear System Managers,</p>
  90. <p>Reporting ERPNext failed scheduler events for the day (max 10):</p>
  91. <p>URL: <a href="%(url)s" target="_blank">%(url)s</a></p><hr>""" % {"url":get_url()}) + "<hr>".join(errors))
  92. if __name__=='__main__':
  93. execute()