summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-15 21:08:05 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-15 21:13:06 +0200
commit78a3ec0d6ee2beb694c4169e673ef565aaa2fe5b (patch)
tree8e37854abfc60c7a8b8d7e7b2ea17124aa4b52f1
parentacb2a03c983157693beaac092dd9932ab75fcd83 (diff)
downloadfeed2imap-78a3ec0d6ee2beb694c4169e673ef565aaa2fe5b.tar.gz
feed2imap-78a3ec0d6ee2beb694c4169e673ef565aaa2fe5b.tar.bz2
feed2imap-78a3ec0d6ee2beb694c4169e673ef565aaa2fe5b.zip
Cleanup cache
-rwxr-xr-xbin/feed2imap-cleaner55
-rw-r--r--lib/feed2imap/cache.rb11
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 <file>", "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