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.
 
 
 
 
 
 

119 lines
3.3 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. """
  24. Scheduler will call the following events from the module
  25. `startup.schedule_handler`
  26. execute_always
  27. execute_daily
  28. execute_monthly
  29. execute_weekly
  30. The scheduler should be called from a cron job every x minutes (5?) depending
  31. on the need.
  32. """
  33. import webnotes
  34. def execute():
  35. """
  36. execute jobs
  37. this method triggers the other scheduler events
  38. Database connection: Ideally it should be connected from outside, if there is
  39. no connection, it will connect from defs.py
  40. """
  41. from datetime import datetime
  42. import webnotes.utils
  43. format = '%Y-%m-%d %H:%M:%S'
  44. if not webnotes.conn:
  45. webnotes.connect()
  46. out = []
  47. nowtime = webnotes.utils.now_datetime()
  48. last = webnotes.conn.get_global('scheduler_last_event')
  49. # set scheduler last event
  50. webnotes.conn.begin()
  51. webnotes.conn.set_global('scheduler_last_event', nowtime.strftime(format))
  52. webnotes.conn.commit()
  53. if last:
  54. last = datetime.strptime(last, format)
  55. if nowtime.day != last.day:
  56. # if first task of the day execute daily tasks
  57. out.append('daily:' + trigger('execute_daily'))
  58. if nowtime.month != last.month:
  59. out.append('monthly:' + trigger('execute_monthly'))
  60. if nowtime.weekday()==0:
  61. out.append('weekly:' + trigger('execute_weekly'))
  62. if nowtime.hour != last.hour:
  63. out.append('hourly:' + trigger('execute_hourly'))
  64. out.append('all:' + trigger('execute_all'))
  65. return '\n'.join(out)
  66. def trigger(method):
  67. """trigger method in startup.schedule_handler"""
  68. try:
  69. import startup.schedule_handlers
  70. if hasattr(startup.schedule_handlers, method):
  71. webnotes.conn.begin()
  72. getattr(startup.schedule_handlers, method)()
  73. webnotes.conn.commit()
  74. return 'ok'
  75. except Exception:
  76. return log(method)
  77. def log(method):
  78. """log error in patch_log"""
  79. import webnotes
  80. if not (webnotes.conn and webnotes.conn._conn):
  81. webnotes.connect()
  82. webnotes.conn.rollback()
  83. traceback = webnotes.getTraceback()
  84. import webnotes.utils
  85. webnotes.conn.begin()
  86. d = webnotes.doc("Scheduler Log")
  87. d.method = method
  88. d.error = traceback
  89. d.save()
  90. webnotes.conn.commit()
  91. return traceback
  92. if __name__=='__main__':
  93. execute()