Move log file deletion into separate task

This commit is contained in:
Peter Conrad 2019-09-15 14:09:44 +02:00
parent 2e50624b28
commit 5cd9f7d631

View file

@ -21,6 +21,7 @@ namespace fc {
boost::mutex slock; boost::mutex slock;
private: private:
future<void> _deletion_task;
boost::atomic<int64_t> _current_file_number; boost::atomic<int64_t> _current_file_number;
public: public:
@ -36,6 +37,7 @@ namespace fc {
FC_ASSERT( cfg.rotation_limit >= cfg.rotation_interval ); FC_ASSERT( cfg.rotation_limit >= cfg.rotation_interval );
rotate_files( true ); rotate_files( true );
delete_files();
} else { } else {
out.open( cfg.filename, std::ios_base::out | std::ios_base::app); out.open( cfg.filename, std::ios_base::out | std::ios_base::app);
} }
@ -46,6 +48,17 @@ namespace fc {
} }
} }
~impl()
{
try
{
_deletion_task.cancel_and_wait("file_appender is destructing");
}
catch( ... )
{
}
}
void rotate_files( bool initializing = false ) void rotate_files( bool initializing = false )
{ {
if( !cfg.rotate ) return; if( !cfg.rotate ) return;
@ -78,31 +91,37 @@ namespace fc {
out.open( log_filename, std::ios_base::out | std::ios_base::app ); out.open( log_filename, std::ios_base::out | std::ios_base::app );
create_hard_link(log_filename, link_filename); create_hard_link(log_filename, link_filename);
} }
}
void delete_files()
{
/* Delete old log files */ /* Delete old log files */
fc::time_point limit_time = now - cfg.rotation_limit; auto current_file = _current_file_number.load();
int64_t interval_seconds = cfg.rotation_interval.to_seconds();
fc::time_point_sec start_time = time_point_sec( (uint32_t)(current_file * interval_seconds) );
fc::time_point limit_time = time_point::now() - cfg.rotation_limit;
fc::path link_filename = cfg.filename;
string link_filename_string = link_filename.filename().string(); string link_filename_string = link_filename.filename().string();
directory_iterator itr(link_filename.parent_path()); directory_iterator itr(link_filename.parent_path());
string timestamp_string = start_time.to_non_delimited_iso_string();
for( ; itr != directory_iterator(); itr++ ) for( ; itr != directory_iterator(); itr++ )
{ {
try try
{ {
string current_filename = itr->filename().string(); string current_filename = itr->filename().string();
if (current_filename.compare(0, link_filename_string.size(), link_filename_string) != 0 || if( current_filename.compare(0, link_filename_string.size(), link_filename_string) != 0
current_filename.size() <= link_filename_string.size() + 1) || current_filename.size() <= link_filename_string.size() + 1 )
continue; continue;
string current_timestamp_str = current_filename.substr(link_filename_string.size() + 1, string current_timestamp_str = current_filename.substr(link_filename_string.size() + 1,
timestamp_string.size()); timestamp_string.size());
fc::time_point_sec current_timestamp = fc::time_point_sec::from_iso_string( current_timestamp_str ); fc::time_point_sec current_timestamp = fc::time_point_sec::from_iso_string( current_timestamp_str );
if( current_timestamp < start_time ) if( current_timestamp < start_time
{ && ( current_timestamp < limit_time || file_size( current_filename ) <= 0 ) )
if( current_timestamp < limit_time || file_size( current_filename ) <= 0 )
{ {
remove_all( *itr ); remove_all( *itr );
continue; continue;
} }
} }
}
catch (const fc::canceled_exception&) catch (const fc::canceled_exception&)
{ {
throw; throw;
@ -111,6 +130,9 @@ namespace fc {
{ {
} }
} }
_deletion_task = schedule( [this]() { delete_files(); },
start_time + cfg.rotation_interval.to_seconds(),
"delete_files(3)" );
} }
}; };