Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

107 linhas
3.1 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. """
  23. Scheduler will call the following events from the module
  24. `startup.schedule_handler`
  25. execute_always
  26. execute_daily
  27. execute_monthly
  28. execute_weekly
  29. The scheduler should be called from a cron job every x minutes (5?) depending
  30. on the need.
  31. """
  32. import webnotes
  33. def execute():
  34. """
  35. execute jobs
  36. this method triggers the other scheduler events
  37. Database connection: Ideally it should be connected from outside, if there is
  38. no connection, it will connect from defs.py
  39. """
  40. from datetime import datetime
  41. import webnotes.utils
  42. format = '%Y-%m-%d %H:%M:%S'
  43. if not webnotes.conn:
  44. webnotes.connect()
  45. out = []
  46. # if first task of the day execute daily tasks
  47. nowtime = webnotes.utils.now_datetime()
  48. last = webnotes.conn.get_global('scheduler_last_event')
  49. if last:
  50. last = datetime.strptime(last, format)
  51. if nowtime.day != last.day:
  52. out.append('daily:' + trigger('execute_daily'))
  53. if nowtime.month != last.month:
  54. out.append('monthly:' + trigger('execute_monthly'))
  55. if nowtime.weekday()==0:
  56. out.append('weekly:' + trigger('execute_weekly'))
  57. if nowtime.hour != last.hour:
  58. out.append('hourly:' + trigger('execute_hourly'))
  59. out.append('all:' + trigger('execute_all'))
  60. webnotes.conn.set_global('scheduler_last_event', nowtime.strftime(format))
  61. return '\n'.join(out)
  62. def trigger(method):
  63. """trigger method in startup.schedule_handler"""
  64. try:
  65. import startup.schedule_handlers
  66. if hasattr(startup.schedule_handlers, method):
  67. webnotes.conn.begin()
  68. getattr(startup.schedule_handlers, method)()
  69. webnotes.conn.commit()
  70. return 'ok'
  71. except Exception, e:
  72. return log(method)
  73. def log(method):
  74. """log error in patch_log"""
  75. import webnotes
  76. webnotes.conn.rollback()
  77. traceback = webnotes.getTraceback()
  78. import webnotes.utils
  79. webnotes.conn.sql("""insert into __SchedulerLog (`timestamp`, method, error)
  80. values (%s, %s, %s)""", (webnotes.utils.now_datetime(), method, traceback))
  81. return traceback
  82. if __name__=='__main__':
  83. execute()