From 07202f62da21ecf978496f4bec7f97522f2fbe75 Mon Sep 17 00:00:00 2001 From: lnu Date: Thu, 9 Feb 2006 17:22:31 +0000 Subject: cleaned up the threaded code. git-svn-id: svn+ssh://svn.gna.org/svn/feed2imap/trunk/feed2imap@80 f70e237a-67f3-0310-a06c-d2b8a7116972 --- lib/feed2imap/feed2imap.rb | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'lib/feed2imap/feed2imap.rb') diff --git a/lib/feed2imap/feed2imap.rb b/lib/feed2imap/feed2imap.rb index b0eff19..a8fd1fb 100644 --- a/lib/feed2imap/feed2imap.rb +++ b/lib/feed2imap/feed2imap.rb @@ -99,28 +99,37 @@ class Feed2Imap end # for each feed, fetch, upload to IMAP and cache @logger.info("Fetching feeds") - loggermon = Mutex::new ths = [] + mutex = Mutex::new @config.feeds.each do |f| ths << Thread::new do url = f.url begin + mutex.lock lastcheck = @cache.get_last_check(f.name) if f.needfetch(lastcheck) - f.body = HTTPFetcher::fetch(f.url, @cache.get_last_check(f.name)) + mutex.unlock + s = HTTPFetcher::fetch(f.url, @cache.get_last_check(f.name)) + mutex.lock + f.body = s @cache.set_last_check(f.name, Time::now) end + mutex.unlock # dump if requested - if @config.dumpdir and f.body - fname = @config.dumpdir + '/' + f.name + '-' + Time::now.xmlschema - File::open(fname, 'w') { |file| file.puts f.body } + if @config.dumpdir + mutex.synchronize do + if f.body + fname = @config.dumpdir + '/' + f.name + '-' + Time::now.xmlschema + File::open(fname, 'w') { |file| file.puts f.body } + end + end end rescue Timeout::Error - loggermon.synchronize do + mutex.synchronize do @logger.fatal("Timeout::Error while fetching #{url}: #{$!}") end rescue - loggermon.synchronize do + mutex.synchronize do @logger.fatal("Error while fetching #{url}: #{$!}") end end -- cgit v1.2.3-54-g00ecf