archiver_1.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Use of this source code is governed by a BSD-style
  2. # license that can be found in the LICENSE file.
  3. # Copyright 2019 The OSArchiver Authors. All rights reserved.
  4. """
  5. The Archiver class file
  6. """
  7. import logging
  8. import traceback
  9. from osarchiver.errors import OSArchiverArchivingFailed
  10. class Archiver():
  11. """
  12. Archiver class
  13. """
  14. def __init__(self, name=None, src=None, dst=None, conf=None):
  15. """
  16. instantiator, take one source and a list of destinations
  17. """
  18. self.name = name
  19. # One source
  20. self.src = src
  21. # Pool of destinations
  22. self.dst = dst or []
  23. # Config parser instance
  24. self.conf = conf
  25. def __repr__(self):
  26. return "Archiver {name}: {src} -> {dst}".\
  27. format(name=self.name, src=self.src, dst=self.dst)
  28. def read(self):
  29. """
  30. read method which loop over each set of data from Source instance
  31. yield database, table, items
  32. """
  33. for data in self.src.read():
  34. for items in data['data']:
  35. yield (data['database'], data['table'], items)
  36. def write(self, database=None, table=None, data=None):
  37. """
  38. write method take a set of data as arguments, database and table and
  39. loop over each destination configured and call the write method of the
  40. destination object raise OSArchiverArchivingFailed in case of archiving
  41. error to prevent deletion
  42. """
  43. if not self.src.archive_data:
  44. logging.info("Ignoring data archiving because archive_data is "
  45. "set to %s", self.src.archive_data)
  46. else:
  47. for dst in self.dst:
  48. try:
  49. dst.write(database=database, table=table, data=data)
  50. except Exception as my_exception:
  51. logging.error(
  52. "An error occured while archiving data: %s",
  53. my_exception)
  54. logging.error("Full traceback is: %s",
  55. traceback.format_exc())
  56. raise OSArchiverArchivingFailed
  57. def delete(self, database=None, table=None, data=None):
  58. """
  59. delete method take a set of data, database, table as arguments and
  60. delete the data from source if the delete_data prameters is true
  61. """
  62. if not self.src.delete_data:
  63. logging.debug("Ignoring data deletion because delete_data is "
  64. "set to %s", self.src.delete_data)
  65. else:
  66. try:
  67. self.src.delete(database=database, table=table, data=data)
  68. except Exception as my_exception:
  69. logging.error("An error occured while deleting data: %s",
  70. my_exception)
  71. logging.error("Full traceback is: %s", traceback.format_exc())
  72. def run(self):
  73. """
  74. main method which basically read a set of data from the source then
  75. archive the data and delete them if no exception were caught
  76. """
  77. if not self.src.archive_data and not self.src.delete_data:
  78. logging.info("Nothing to do for archiver %s archive_data and "
  79. "delete_date are disabled", self.name)
  80. return 0
  81. if not self.src.delete_data:
  82. logging.info("Data won't be deleted because 'delete_data' set to"
  83. " %s", self.src.delete_data)
  84. for (database, table, items) in self.read():
  85. try:
  86. self.write(database=database, table=table, data=items)
  87. except OSArchiverArchivingFailed:
  88. logging.info("Ignoring deletion step because an error occured "
  89. "while archiving data")
  90. else:
  91. self.delete(database=database, table=table, data=items)
  92. self.clean_exit()
  93. return 0
  94. def clean_exit(self):
  95. """
  96. method called when archiving is finished. It calls clean_exit method of
  97. Source and Destination instances
  98. """
  99. logging.info("Please wait for clean exit...")
  100. self.src.clean_exit()
  101. for dst in self.dst:
  102. dst.clean_exit()