recordpatchevent.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2013 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. from config.logging import queue_log_duration
  29. from model.patchlog import PatchLog
  30. from model.queuelog import QueueLog
  31. from model.warninglog import WarningLog
  32. class RecordPatchEvent(object):
  33. @classmethod
  34. def added(cls, attachment_id, queue_name):
  35. PatchLog.lookup(attachment_id, queue_name)
  36. queue_log = QueueLog.get_current(queue_name, queue_log_duration)
  37. if queue_log.update_max_patches_waiting():
  38. queue_log.put()
  39. @classmethod
  40. def retrying(cls, attachment_id, queue_name, bot_id=None):
  41. patch_log = PatchLog.lookup_if_exists(attachment_id, queue_name)
  42. if not patch_log:
  43. WarningLog.record("patchlog missing", "In retrying event.", attachment_id, queue_name, bot_id)
  44. return
  45. if bot_id:
  46. patch_log.bot_id = bot_id
  47. patch_log.retry_count += 1
  48. patch_log.put()
  49. queue_log = QueueLog.get_current(queue_name, queue_log_duration)
  50. queue_log.patch_retry_count += 1
  51. queue_log.put()
  52. @classmethod
  53. def started(cls, attachment_id, queue_name, bot_id=None):
  54. patch_log = PatchLog.lookup_if_exists(attachment_id, queue_name)
  55. if not patch_log:
  56. WarningLog.record("patchlog missing", "In started event.", attachment_id, queue_name, bot_id)
  57. return
  58. # An existing wait_duration implies the patch had been started previously and is being picked up again because it had expired.
  59. if not patch_log.wait_duration:
  60. if bot_id:
  61. patch_log.bot_id = bot_id
  62. patch_log.calculate_wait_duration()
  63. patch_log.put()
  64. queue_log = QueueLog.get_current(queue_name, queue_log_duration)
  65. queue_log.patch_wait_durations.append(patch_log.wait_duration)
  66. queue_log.put()
  67. @classmethod
  68. def stopped(cls, attachment_id, queue_name, bot_id=None):
  69. patch_log = PatchLog.lookup_if_exists(attachment_id, queue_name)
  70. if not patch_log:
  71. WarningLog.record("patchlog missing", "In stopped event.", attachment_id, queue_name, bot_id)
  72. return
  73. if not patch_log.wait_duration:
  74. WarningLog.record("patchlog wait duration missing", "In stopped event.", attachment_id, queue_name, bot_id)
  75. return
  76. if not patch_log.finished:
  77. if bot_id:
  78. patch_log.bot_id = bot_id
  79. patch_log.finished = True
  80. patch_log.calculate_process_duration()
  81. patch_log.put()
  82. queue_log = QueueLog.get_current(queue_name, queue_log_duration)
  83. queue_log.patch_process_durations.append(patch_log.process_duration)
  84. queue_log.put()
  85. @classmethod
  86. def updated(cls, attachment_id, queue_name, bot_id=None):
  87. patch_log = PatchLog.lookup_if_exists(attachment_id, queue_name)
  88. if not patch_log:
  89. WarningLog.record("patchlog missing", "In updated event.", attachment_id, queue_name, bot_id)
  90. return
  91. if bot_id:
  92. patch_log.bot_id = bot_id
  93. patch_log.status_update_count += 1
  94. patch_log.put()
  95. queue_log = QueueLog.get_current(queue_name, queue_log_duration)
  96. queue_log.status_update_count += 1
  97. queue_log.put()