From 78a3ec0d6ee2beb694c4169e673ef565aaa2fe5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Wed, 15 Apr 2020 21:08:05 +0200 Subject: Cleanup cache --- bin/feed2imap-cleaner | 55 +++++++++++++++++++++++++++++++++++++++++++++----- lib/feed2imap/cache.rb | 11 ++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/bin/feed2imap-cleaner b/bin/feed2imap-cleaner index dc119f6..b616585 100755 --- a/bin/feed2imap-cleaner +++ b/bin/feed2imap-cleaner @@ -7,6 +7,7 @@ require 'optparse' configf = ENV['HOME'] + '/.feed2imaprc' dryrun = false +docache = false opts = OptionParser::new do |opts| opts.banner = "Usage: feed2imap-cleaner [options]" @@ -15,6 +16,9 @@ opts = OptionParser::new do |opts| opts.on("-d", "--dry-run", "Dont really remove messages") do |v| dryrun = true end + opts.on("-c", "--cache", "Clean cache instead of messages") do |v| + docache = true + end opts.on("-f", "--config ", "Select alternate config file") do |f| configf = f end @@ -23,10 +27,51 @@ opts.parse!(ARGV) config = nil File::open(configf) { |f| config = F2IConfig::new(f) } -config.imap_accounts.each_value do |ac| - ac.connect -end -config.feeds.each do |f| - f.imapaccount.cleanup(f.folder, dryrun) +if docache + puts 'Initializing cache ...' + cache = ItemCache::new(true) + if not File::exist?(config.cache + '.lock') + f = File::new(config.cache + '.lock', 'w') + f.close + end + if File::new(config.cache + '.lock', 'w').flock(File::LOCK_EX | File::LOCK_NB) == false + puts "Another instance of feed2imap is already locking the cache file" + exit(1) + end + if File::exist?(config.cache) + File::open(config.cache) do |f| + cache.load(f) + end + end + + before = cache.nbchannels + + puts "Cleaning up" + cache.cleanup(config.feeds) + + after = cache.nbchannels + + if not dryrun + puts "Saving cache ..." + begin + File::open("#{config.cache}.new", 'w') { |f| cache.save(f) } + rescue + puts "Exception caught while writing new cache to #{config.cache}.new: #{$!}" + end + begin + File::rename("#{config.cache}.new", config.cache) + rescue + puts"Exception caught while renaming #{@config.cache}.new to #{@config.cache}: #{$!}" + end + end + + puts "#Channels before: #{before}, after: #{after}" +else + config.imap_accounts.each_value do |ac| + ac.connect + end + config.feeds.each do |f| + f.imapaccount.cleanup(f.folder, dryrun) + end end diff --git a/lib/feed2imap/cache.rb b/lib/feed2imap/cache.rb index 9fb9930..9c5977c 100644 --- a/lib/feed2imap/cache.rb +++ b/lib/feed2imap/cache.rb @@ -110,6 +110,17 @@ class ItemCache @@cacheidx += 1 i end + + def cleanup(feeds) + remaining = {} + feeds.each do |f| + id = f.name + if @channels.key?(id) + remaining[id] = @channels[id] + end + end + @channels = remaining + end end class CachedChannel -- cgit v1.2.3