archive.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2020 HPCC Systems®.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ##############################################################################
  16. */
  17. #pragma warning(disable:4786)
  18. #include "archive.hpp"
  19. CArchiveTankFileHelper::CArchiveTankFileHelper(IProperties* input)
  20. {
  21. logServer.set(input->queryProp("server"));
  22. if (logServer.isEmpty())
  23. throw makeStringException(-1, "Please specify url.");
  24. groupName.set(input->queryProp("group"));
  25. archiveToDir.set(input->queryProp("dir"));
  26. if (archiveToDir.isEmpty())
  27. throw makeStringException(-1, "Please specify archive dir.");
  28. client.setown(createLogServerClient(input));
  29. }
  30. void CArchiveTankFileHelper::archive()
  31. {
  32. printf("Starting archive() ...\n");
  33. Owned<IClientGetAckedLogFilesResponse> resp = getAckedLogFiles();
  34. IArrayOf<IConstLogAgentGroupTankFiles>& ackedLogFilesInGroups = resp->getAckedLogFilesInGroups();
  35. //Check any invalid response and print out the files to be archived just in case
  36. if (!validateAckedLogFiles(ackedLogFilesInGroups))
  37. {
  38. printf("Found invalid response. No file will be archived.\n");
  39. return;
  40. }
  41. ForEachItemIn(i, ackedLogFilesInGroups)
  42. {
  43. IConstLogAgentGroupTankFiles& ackedLogFilesInGroup = ackedLogFilesInGroups.item(i);
  44. StringArray& ackedLogFiles = ackedLogFilesInGroup.getTankFileNames();
  45. if (!ackedLogFiles.length())
  46. continue;
  47. const char* groupName = ackedLogFilesInGroup.getGroupName();
  48. const char* tankFileDir = ackedLogFilesInGroup.getTankFileDir();
  49. printf("%s: archiving files from %s to %s.\n", groupName, tankFileDir, archiveToDir.str());
  50. archiveAckedLogFilesForLogAgentGroup(groupName, tankFileDir, ackedLogFiles);
  51. printf("%s: files archived.\n", groupName);
  52. cleanAckedLogFilesForLogAgentGroup(groupName, ackedLogFiles);
  53. printf("%s: files cleaned.\n", groupName);
  54. }
  55. printf("The archive() done\n");
  56. }
  57. IClientGetAckedLogFilesResponse* CArchiveTankFileHelper::getAckedLogFiles()
  58. {
  59. Owned<IClientGetAckedLogFilesRequest> req = client->createGetAckedLogFilesRequest();
  60. if (!groupName.isEmpty())
  61. {
  62. IArrayOf<IEspLogAgentGroup> groups;
  63. Owned<IEspLogAgentGroup> group = createLogAgentGroup();
  64. group->setGroupName(groupName.get());
  65. groups.append(*group.getClear());
  66. req->setGroups(groups);
  67. }
  68. Owned<IClientGetAckedLogFilesResponse> resp = client->GetAckedLogFiles(req);
  69. if (!resp)
  70. throw makeStringException(-1, "Failed in GetAckedLogFiles.");
  71. const IMultiException* excep = &resp->getExceptions();
  72. if (excep != nullptr && excep->ordinality() > 0)
  73. {
  74. StringBuffer msg;
  75. printf("%s\n", excep->errorMessage(msg).str());
  76. throw makeStringException(-1, "Cannot archiveTankFiles.");
  77. }
  78. return resp.getClear();
  79. }
  80. bool CArchiveTankFileHelper::validateAckedLogFiles(IArrayOf<IConstLogAgentGroupTankFiles>& ackedLogFilesInGroups)
  81. {
  82. bool invalidResponse = false;
  83. ForEachItemIn(i, ackedLogFilesInGroups)
  84. {
  85. IConstLogAgentGroupTankFiles& ackedLogFilesInGroup = ackedLogFilesInGroups.item(i);
  86. const char* groupName = ackedLogFilesInGroup.getGroupName();
  87. if (isEmptyString(groupName))
  88. {
  89. printf("Empty group name in GetAckedLogFilesResponse.\n");
  90. invalidResponse = true;
  91. continue;
  92. }
  93. const char* tankFileDir = ackedLogFilesInGroup.getTankFileDir();
  94. if (isEmptyString(tankFileDir))
  95. {
  96. printf("Empty TankFile directory for group %s.\n", groupName);
  97. invalidResponse = true;
  98. continue;
  99. }
  100. printf("Group %s, TankFile directory %s:\n", groupName, tankFileDir);
  101. StringArray& ackedLogFiles = ackedLogFilesInGroup.getTankFileNames();
  102. if (!ackedLogFiles.length())
  103. {
  104. printf("No TankFile to be archived for %s.\n", groupName);
  105. }
  106. else
  107. {
  108. ForEachItemIn(i, ackedLogFiles)
  109. printf("TankFile to be archived: %s.\n", ackedLogFiles.item(i));
  110. }
  111. }
  112. return !invalidResponse;
  113. }
  114. void CArchiveTankFileHelper::archiveAckedLogFilesForLogAgentGroup(const char* groupName, const char* archiveFromDir, StringArray& ackedLogFiles)
  115. {
  116. Owned<IFile> archiveToFolder = createIFile(archiveToDir);
  117. if (!archiveToFolder->exists())
  118. archiveToFolder->createDirectory();
  119. StringBuffer timeStr;
  120. ForEachItemIn(i, ackedLogFiles)
  121. {
  122. const char* ackedFileName = ackedLogFiles.item(i);
  123. printf("Archiving %s.\n", ackedFileName);
  124. StringBuffer ackedFile(archiveFromDir), archivedFile(archiveToDir);
  125. addPathSepChar(ackedFile);
  126. addPathSepChar(archivedFile);
  127. ackedFile.append(ackedFileName);
  128. archivedFile.append(ackedFileName);
  129. Owned<IFile> srcfile = createIFile(ackedFile);
  130. Owned<IFile> dstFile = createIFile(archivedFile);
  131. if (dstFile->exists())
  132. {
  133. StringBuffer newName;
  134. if (timeStr.isEmpty())
  135. {
  136. CDateTime now;
  137. now.setNow();
  138. now.getString(timeStr);
  139. }
  140. newName.append(archivedFile).append(".").append(timeStr);
  141. dstFile->move(newName);
  142. printf("Old %s is moved to %s.\n", archivedFile.str(), newName.str());
  143. }
  144. srcfile->move(archivedFile);
  145. printf("%s is archived to %s.\n", ackedFile.str(), archivedFile.str());
  146. }
  147. }
  148. void CArchiveTankFileHelper::cleanAckedLogFilesForLogAgentGroup(const char* groupName, StringArray& ackedLogFiles)
  149. {
  150. Owned<IClientCleanAckedFilesRequest> req = client->createCleanAckedFilesRequest();
  151. req->setGroupName(groupName);
  152. req->setFileNames(ackedLogFiles);
  153. Owned<IClientCleanAckedFilesResponse> resp = client->CleanAckedFiles(req);
  154. if (!resp)
  155. throw makeStringException(-1, "Failed in CleanAckedFiles.");
  156. const IMultiException* excep = &resp->getExceptions();
  157. if (excep != nullptr && excep->ordinality() > 0)
  158. {
  159. StringBuffer msg;
  160. printf("%s\n", excep->errorMessage(msg).str());
  161. }
  162. }
  163. IClientWSDecoupledLog* createLogServerClient(IProperties* input)
  164. {
  165. Owned<IClientWSDecoupledLog> client = createWSDecoupledLogClient();
  166. const char* server = input->queryProp("server");
  167. if (isEmptyString(server))
  168. throw MakeStringException(0, "Server url not defined");
  169. StringBuffer url(server);
  170. addPathSepChar(url);
  171. url.append("WSDecoupledLog");
  172. client->addServiceUrl(url.str());
  173. const char* user = input->queryProp("user");
  174. const char* password = input->queryProp("password");
  175. if (!isEmptyString(user))
  176. client->setUsernameToken(user, password, nullptr);
  177. return client.getClear();
  178. }
  179. void archiveTankFiles(IProperties* input)
  180. {
  181. try
  182. {
  183. printf("Starting archiveTankFiles\n");
  184. Owned<CArchiveTankFileHelper> helper = new CArchiveTankFileHelper(input);
  185. helper->archive();
  186. printf("Finished archiveTankFiles\n");
  187. }
  188. catch (IException *e)
  189. {
  190. EXCLOG(e);
  191. e->Release();
  192. }
  193. }