feedupdater 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ == FeedUpdater 0.2.1
2
+ * no longer attempts to update feeds that have been updated very recently
3
+ * fixed logging levels
4
+ * added on_begin block
1
5
  == FeedUpdater 0.2.0
2
6
  * multithreaded
3
7
  * unchanged feeds will no longer call on_update
@@ -1,4 +1,4 @@
1
- #!/usr/local/bin/ruby
1
+ #!/usr/bin/env ruby
2
2
 
3
3
  #--
4
4
  # Copyright (c) 2005 Robert Aman
@@ -1,12 +1,22 @@
1
1
  class CustomUpdater < FeedTools::FeedUpdater
2
+ on_begin do
3
+ # self.feed_href_list = [
4
+ # "http://www.gigaom.com/feed/rss2/",
5
+ # "http://feeds.feedburner.com/ManeuverMarketingCommunique",
6
+ # "http://www.afp.com/english/rss/stories.xml",
7
+ # "http://www.railheaddesign.com/rss/railhead.xml",
8
+ # "http://www.nateanddi.com/rssfeed.xml"
9
+ # ]
10
+ end
11
+
2
12
  on_update do |feed, seconds|
3
- logger.info("Loaded '#{feed.href}'.")
4
- logger.info("=> Updated (#{feed.title}) in #{seconds} seconds.")
13
+ self.logger.info("Loaded '#{feed.href}'.")
14
+ self.logger.info("=> Updated (#{feed.title}) in #{seconds} seconds.")
5
15
  end
6
16
 
7
17
  on_error do |href, error|
8
- logger.info("Error updating '#{href}':")
9
- logger.info(error)
18
+ self.logger.info("Error updating '#{href}':")
19
+ self.logger.info(error)
10
20
  end
11
21
 
12
22
  on_complete do |updated_feed_hrefs|
@@ -64,7 +64,7 @@ class FeedUpdaterLogger < Logger
64
64
  alias_method :old_log, :log
65
65
  def log(level, message)
66
66
  if defined?(@prefix) && @prefix != nil
67
- self.old_log(level, self.prefix + message)
67
+ self.old_log(level, "#{self.prefix}#{message}")
68
68
  else
69
69
  self.old_log(level, message)
70
70
  end
@@ -94,6 +94,14 @@ end
94
94
  module FeedTools
95
95
  # A simple daemon for scheduled updating of feeds.
96
96
  class FeedUpdater
97
+ # Declares an on_begin event. The given block will be called before the
98
+ # update sequence runs to allow for any setup required. The block is
99
+ # not passed any arguments.
100
+ def self.on_begin(&block)
101
+ raise "No block supplied for on_begin." if block.nil?
102
+ @@on_begin = block
103
+ end
104
+
97
105
  # Declares an on_update event. The given block will be called after
98
106
  # every feed update. The block is passed the feed object that was loaded
99
107
  # and the time it took in seconds to successfully load it.
@@ -325,18 +333,25 @@ module FeedTools
325
333
 
326
334
  # The main feed update loop.
327
335
  loop do
328
- result = Benchmark.measure do
329
- self.update_feeds()
330
- end
331
- self.logger.info(
332
- "#{@feed_href_list.size} feed(s) updated " +
333
- "in #{result.real.round} seconds.")
334
- if !defined?(@feed_href_list_override) ||
335
- !@feed_href_list_override
336
- @feed_href_list = nil
336
+ result = nil
337
+ sleepy_time = 1.hour
338
+ begin
339
+ result = Benchmark.measure do
340
+ self.update_feeds()
341
+ end
342
+ self.logger.info(
343
+ "#{@feed_href_list.size} feed(s) updated " +
344
+ "in #{result.real.round} seconds.")
345
+ sleepy_time = 1.hour - result.real.round
346
+ rescue Exception => error
347
+ self.logger.error("Feed update sequence errored out.")
348
+ self.logger.error(error.class.name + ": " + error.message)
349
+ self.logger.error("\n" + error.backtrace.join("\n").to_s)
337
350
  end
351
+
352
+ @feed_href_list = nil
353
+ @feed_href_list_override = false
338
354
  ObjectSpace.garbage_collect()
339
- sleepy_time = 1.hour - result.real.round
340
355
  if sleepy_time > 0
341
356
  self.logger.info(
342
357
  "Sleeping for #{(sleepy_time / 60.0).round} minutes...")
@@ -419,18 +434,39 @@ module FeedTools
419
434
 
420
435
  # Updates all of the feeds.
421
436
  def update_feeds()
437
+ self.logger.level = 0
422
438
  self.logger.prefix = "FeedUpdater".ljust(20)
423
439
  ObjectSpace.garbage_collect()
440
+ if defined?(@@on_begin) && @@on_begin != nil
441
+ self.logger.info("Running custom startup event...")
442
+ self.cloaker(&(@@on_begin)).bind(self).call()
443
+ end
424
444
  if defined?(@feed_href_list_override) && @feed_href_list_override
445
+ self.logger.info("Using custom feed list...")
425
446
  self.feed_href_list()
426
447
  else
427
448
  self.logger.info("Loading default feed list...")
428
- @feed_href_list =
429
- (FeedTools.feed_cache.find(:all).collect do |cache_object|
430
- cache_object.href
431
- end)
449
+ begin
450
+ expire_time = (Time.now - 1.hour).utc
451
+ expire_time_string = sprintf('%04d-%02d-%02d %02d:%02d:%02d',
452
+ expire_time.year, expire_time.month, expire_time.day,
453
+ expire_time.hour, expire_time.min, expire_time.sec)
454
+ @feed_href_list =
455
+ FeedTools.feed_cache.connection.execute(
456
+ "SELECT href FROM cached_feeds WHERE " +
457
+ "last_retrieved < '#{expire_time_string}'").to_a.flatten
458
+ rescue Exception
459
+ self.logger.warn("Default feed list failed, using fallback.")
460
+ @feed_href_list =
461
+ FeedTools.feed_cache.find(:all).collect do |feed|
462
+ feed.href
463
+ end
464
+ self.logger.warn(
465
+ "Fallback succeeded. Custom feed list override recommended.")
466
+ end
432
467
  end
433
468
  self.logger.info("Updating #{@feed_href_list.size} feed(s)...")
469
+ self.logger.level = self.updater_options[:log_level]
434
470
  ObjectSpace.garbage_collect()
435
471
 
436
472
  threads = []
@@ -487,52 +523,56 @@ module FeedTools
487
523
  href_list = thread_slices[Thread.current.thread_id]
488
524
 
489
525
  for i in 0...href_list.size
490
- href = nil
491
526
  progress = nil
492
527
  mutex.synchronize do
493
- href = href_list[i]
528
+ Thread.current.href = href_list[i]
494
529
  Thread.current.progress =
495
- (href_list.index(href).to_f / href_list.size.to_f) * 100
530
+ (href_list.index(Thread.current.href).to_f /
531
+ href_list.size.to_f) * 100
496
532
  progress = sprintf("%.2f", Thread.current.progress)
497
533
  end
498
534
  begin
499
535
  begin
500
- feed = nil
536
+ Thread.current.feed = nil
501
537
  feed_load_benchmark = Benchmark.measure do
502
- feed = FeedTools::Feed.open(href)
538
+ Thread.current.feed =
539
+ FeedTools::Feed.open(Thread.current.href)
503
540
  end
504
541
  Thread.pass
505
- if feed.live?
542
+ if Thread.current.feed.live?
506
543
  unless @@on_update.nil?
507
544
  mutex.synchronize do
508
545
  progress = sprintf("%.2f", Thread.current.progress)
509
- self.logger.prefix = ("Thread #{Thread.current.thread_id} " +
510
- "(#{progress}%)"
511
- ).ljust(20)
546
+ self.logger.prefix =
547
+ ("Thread #{Thread.current.thread_id} (#{progress}%)"
548
+ ).ljust(20)
512
549
  self.cloaker(&(@@on_update)).bind(self).call(
513
- feed, feed_load_benchmark.real)
550
+ Thread.current.feed, feed_load_benchmark.real)
514
551
  end
515
552
  end
516
553
  else
517
554
  mutex.synchronize do
518
555
  progress = sprintf("%.2f", Thread.current.progress)
519
- self.logger.prefix = ("Thread #{Thread.current.thread_id} " +
520
- "(#{progress}%)"
521
- ).ljust(20)
556
+ self.logger.prefix =
557
+ ("Thread #{Thread.current.thread_id} (#{progress}%)"
558
+ ).ljust(20)
522
559
  self.logger.info(
523
- "'#{href}' unchanged, skipping.")
560
+ "'#{Thread.current.href}' unchanged " +
561
+ "or unavailable, skipping.")
524
562
  end
525
563
  end
526
564
  rescue Exception => error
527
565
  mutex.synchronize do
528
566
  progress = sprintf("%.2f", Thread.current.progress)
529
- self.logger.prefix = ("Thread #{Thread.current.thread_id} " +
530
- "(#{progress}%)"
531
- ).ljust(20)
567
+ self.logger.prefix =
568
+ ("Thread #{Thread.current.thread_id} (#{progress}%)"
569
+ ).ljust(20)
532
570
  if @@on_error != nil
533
- self.cloaker(&(@@on_error)).bind(self).call(href, error)
571
+ self.cloaker(&(@@on_error)).bind(self).call(
572
+ Thread.current.href, error)
534
573
  else
535
- self.logger.error("Error updating '#{href}':")
574
+ self.logger.error(
575
+ "Error updating '#{Thread.current.href}':")
536
576
  self.logger.error(error.class.name + ": " + error.message)
537
577
  self.logger.error(error.class.backtrace)
538
578
  end
@@ -545,7 +585,7 @@ module FeedTools
545
585
  "(#{progress}%)"
546
586
  ).ljust(20)
547
587
  self.logger.fatal("Critical unhandled error.")
548
- self.logger.fatal("Error updating '#{href}':")
588
+ self.logger.fatal("Error updating '#{Thread.current.href}':")
549
589
  self.logger.fatal(error.class.name + ": " + error.message)
550
590
  self.logger.fatal(error.class.backtrace)
551
591
  end
@@ -558,6 +598,8 @@ module FeedTools
558
598
  class <<updater_thread
559
599
  attr_accessor :thread_id
560
600
  attr_accessor :progress
601
+ attr_accessor :href
602
+ attr_accessor :feed
561
603
  end
562
604
  updater_thread.thread_id = i
563
605
  end
@@ -586,7 +628,9 @@ module FeedTools
586
628
  unless @@on_complete.nil?
587
629
  self.cloaker(&(@@on_complete)).bind(self).call(@feed_href_list)
588
630
  end
631
+ self.logger.level = 0
589
632
  self.logger.info("Finished updating.")
633
+ self.logger.level = self.updater_options[:log_level]
590
634
  end
591
635
  end
592
636
  end
@@ -2,7 +2,7 @@ module FeedTools
2
2
  module FEED_UPDATER_VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: feedupdater
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2006-04-17 00:00:00 -07:00
6
+ version: 0.2.1
7
+ date: 2006-04-20 00:00:00 -07:00
8
8
  summary: Automatic feed updater daemon.
9
9
  require_paths:
10
10
  - lib
@@ -32,6 +32,7 @@ files:
32
32
  - bin
33
33
  - CHANGELOG
34
34
  - config
35
+ - doc
35
36
  - example
36
37
  - lib
37
38
  - log