您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

120 行
3.0 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():
  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()
  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('daily:' + trigger('execute_daily'))
  39. if nowtime.month != last.month:
  40. out.append('monthly:' + trigger('execute_monthly'))
  41. if nowtime.weekday()==0:
  42. out.append('weekly:' + trigger('execute_weekly'))
  43. if nowtime.hour != last.hour:
  44. out.append('hourly:' + trigger('execute_hourly'))
  45. out.append('all:' + trigger('execute_all'))
  46. return '\n'.join(out)
  47. def trigger(method):
  48. """trigger method in startup.schedule_handler"""
  49. try:
  50. import startup.schedule_handlers
  51. if hasattr(startup.schedule_handlers, method):
  52. webnotes.conn.begin()
  53. getattr(startup.schedule_handlers, method)()
  54. webnotes.conn.commit()
  55. return 'ok'
  56. except Exception:
  57. return log(method)
  58. try:
  59. cp = webnotes.bean("Control Panel", "Control Panel")
  60. cp.run_method(method)
  61. except Exception:
  62. return log(method)
  63. def log(method):
  64. """log error in patch_log"""
  65. import webnotes
  66. if not (webnotes.conn and webnotes.conn._conn):
  67. webnotes.connect()
  68. webnotes.conn.rollback()
  69. traceback = webnotes.getTraceback()
  70. import webnotes.utils
  71. webnotes.conn.begin()
  72. d = webnotes.doc("Scheduler Log")
  73. d.method = method
  74. d.error = traceback
  75. d.save()
  76. webnotes.conn.commit()
  77. return traceback
  78. def report_errors():
  79. from webnotes.utils.email_lib import sendmail_to_system_managers
  80. from webnotes.utils import get_url
  81. errors = [("""<p>Time: %(modified)s</p>
  82. <pre><code>%(error)s</code></pre>""" % d) for d in webnotes.conn.sql("""select modified, error
  83. from `tabScheduler Log` where DATEDIFF(NOW(), modified) < 1 limit 10""", as_dict=True)]
  84. if errors:
  85. sendmail_to_system_managers("ERPNext Scheduler Failure Report", ("""
  86. <p>Dear System Managers,</p>
  87. <p>Reporting ERPNext failed scheduler events for the day (max 10):</p>
  88. <p>URL: <a href="%(url)s" target="_blank">%(url)s</a></p><hr>""" % {"url":get_url()}) + "<hr>".join(errors))
  89. if __name__=='__main__':
  90. execute()