periodic_file_deleter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <chrono>
17 #include <cstdio>
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <thread>
22 
23 #include <aws/core/utils/logging/LogMacros.h>
24 #include <boost/function.hpp>
25 #include <ros/ros.h>
26 
30 
31 namespace Aws
32 {
33 namespace Rosbag
34 {
35 namespace Utils
36 {
37 
39  boost::function<std::vector<std::string> ()> deletion_list_callback,
40  const int interval_period_s)
41 : is_active_(false),
42 deletion_list_callback_(std::move(deletion_list_callback)),
43 interval_period_s_(interval_period_s)
44 {}
45 
47 {
48  Stop();
49 }
50 
52 {
53  {
54  std::lock_guard<std::mutex> lock(mutex_);
55  if (is_active_) {
56  AWS_LOG_INFO(__func__, "Failed to start PeriodicFileDeleter, deleter already active");
57  return;
58  }
59  is_active_ = true;
60  }
61  thread_ = std::thread{&PeriodicFileDeleter::DeleteFiles, this};
62  AWS_LOG_INFO(__func__, "PeriodicFileDeleter started");
63 }
64 
66 {
67  AWS_LOG_INFO(__func__, "Stopping PeriodicFileDeleter");
68  std::lock_guard<std::mutex> lock(mutex_);
69  is_active_ = false;
70  if (thread_.joinable()) {
71  thread_.join();
72  }
73 }
74 
76 {
77  return is_active_;
78 }
79 
81 {
82  while (is_active_) {
83  auto files_to_delete = deletion_list_callback_();
84  for (const auto& file: files_to_delete) {
85  AWS_LOGSTREAM_INFO(__func__, "Deleting file " << file);
86  auto result = DeleteFile(file);
87  if (result != RecorderErrorCode::SUCCESS) {
88  AWS_LOGSTREAM_ERROR(__func__, "Failed to delete file " << file << ", skipping.");
89  }
90  }
91  std::this_thread::sleep_for(std::chrono::seconds(interval_period_s_));
92  }
93 }
94 
95 } // namespace Utils
96 } // namespace Rosbag
97 } // namespace Aws
boost::function< std::vector< std::string >)> deletion_list_callback_
Aws::Rosbag::RecorderErrorCode DeleteFile(const std::string &file_path)
delete a file
Definition: file_utils.cpp:93
PeriodicFileDeleter(boost::function< std::vector< std::string >()> deletion_list_callback, const int interval_period_s=10)


rosbag_cloud_recorders
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:27