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.
 
 
 
 
 
 

114 lines
3.2 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. # if first task of the day execute daily tasks
  48. nowtime = webnotes.utils.now_datetime()
  49. last = webnotes.conn.get_global('scheduler_last_event')
  50. if last:
  51. last = datetime.strptime(last, format)
  52. if nowtime.day != last.day:
  53. out.append('daily:' + trigger('execute_daily'))
  54. if nowtime.month != last.month:
  55. out.append('monthly:' + trigger('execute_monthly'))
  56. if nowtime.weekday()==0:
  57. out.append('weekly:' + trigger('execute_weekly'))
  58. if nowtime.hour != last.hour:
  59. out.append('hourly:' + trigger('execute_hourly'))
  60. out.append('all:' + trigger('execute_all'))
  61. webnotes.conn.begin()
  62. webnotes.conn.set_global('scheduler_last_event', nowtime.strftime(format))
  63. webnotes.conn.commit()
  64. return '\n'.join(out)
  65. def trigger(method):
  66. """trigger method in startup.schedule_handler"""
  67. try:
  68. import startup.schedule_handlers
  69. if hasattr(startup.schedule_handlers, method):
  70. webnotes.conn.begin()
  71. getattr(startup.schedule_handlers, method)()
  72. webnotes.conn.commit()
  73. return 'ok'
  74. except Exception, e:
  75. return log(method)
  76. def log(method):
  77. """log error in patch_log"""
  78. import webnotes
  79. webnotes.conn.rollback()
  80. traceback = webnotes.getTraceback()
  81. import webnotes.utils
  82. webnotes.conn.begin()
  83. d = webnotes.doc("Scheduler Log")
  84. d.method = method
  85. d.error = traceback
  86. d.save()
  87. webnotes.conn.commit()
  88. return traceback
  89. if __name__=='__main__':
  90. execute()