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.

пре 11 година
пре 13 година
пре 13 година
пре 12 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 12 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  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. import webnotes.utils
  16. def execute(site=None):
  17. """
  18. execute jobs
  19. this method triggers the other scheduler events
  20. Database connection: Ideally it should be connected from outside, if there is
  21. no connection, it will connect from defs.py
  22. """
  23. from datetime import datetime
  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. getattr(startup.schedule_handlers, method)()
  54. except Exception:
  55. traceback += log(method)
  56. else:
  57. webnotes.conn.commit()
  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. else:
  64. webnotes.conn.commit()
  65. return traceback or 'ok'
  66. def log(method, message=None):
  67. """log error in patch_log"""
  68. message = webnotes.utils.cstr(message) + "\n" if message else ""
  69. message += webnotes.getTraceback()
  70. if not (webnotes.conn and webnotes.conn._conn):
  71. webnotes.connect()
  72. webnotes.conn.rollback()
  73. webnotes.conn.begin()
  74. d = webnotes.doc("Scheduler Log")
  75. d.method = method
  76. d.error = message
  77. d.save()
  78. webnotes.conn.commit()
  79. return message
  80. def get_errors(from_date, to_date, limit):
  81. errors = webnotes.conn.sql("""select modified, method, error from `tabScheduler Log`
  82. where date(modified) between %s and %s
  83. and error not like '%%[Errno 110] Connection timed out%%'
  84. order by modified limit %s""", (from_date, to_date, limit), as_dict=True)
  85. return ["""<p>Time: {modified}</p><pre><code>Method: {method}\n{error}</code></pre>""".format(**e)
  86. for e in errors]
  87. def get_error_report(from_date=None, to_date=None, limit=10):
  88. from webnotes.utils import get_url, now_datetime, add_days
  89. if not from_date:
  90. from_date = add_days(now_datetime().date(), -1)
  91. if not to_date:
  92. to_date = add_days(now_datetime().date(), -1)
  93. errors = get_errors(from_date, to_date, limit)
  94. if errors:
  95. return 1, """<h4>Scheduler Failed Events (max {limit}):</h4>
  96. <p>URL: <a href="{url}" target="_blank">{url}</a></p><hr>{errors}""".format(
  97. limit=limit, url=get_url(), errors="<hr>".join(errors))
  98. else:
  99. return 0, "<p>Scheduler didn't encounter any problems.</p>"
  100. if __name__=='__main__':
  101. execute()