Class: Processor

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/flickrup/processor.rb

Instance Method Summary collapse

Methods included from Logging

logger, #logger, pre_init

Constructor Details

#initialize(config) ⇒ Processor

Returns a new instance of Processor.



15
16
17
# File 'lib/flickrup/processor.rb', line 15

def initialize(config)
  @config = config
end

Instance Method Details

#pausedObject



19
20
21
# File 'lib/flickrup/processor.rb', line 19

def paused
  Dir["#{@config[:watch_dir]}/paused"].length > 0
end

#run(all_files = Dir["#{@config[:watch_dir]}/*.*"]) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/flickrup/processor.rb', line 23

def run(all_files = Dir["#{@config[:watch_dir]}/*.*"])

  logger.info("Processing #{all_files.length} files")

  if paused
    logger.info("Paused detected, so skipping processing...")
    return 0
  end

  # some files may have been removed by now: https://bitbucket.org/jgilbert/flickrup/issue/1
  files = all_files.map { |x|
    if File.exist? x
      TaggedFile.create(x)
    else
      logger.debug("File #{x} already removed, skipping.")
    end
  }.compact

  tagged = files.sort_by { |x|
    x.date_created
  }.take_while { |x|
    !(x.tags.nil? || x.tags.empty?)
  }

  logger.info("Selected #{tagged.length} files for upload")

  uploader = get_uploader

  if @config[:dryrun]
    logger.info("Would now upload & move #{tagged.length} files, pausing for 5 secs instead")
    sleep 5
  else
    tagged.each do |x|
      begin
        logger.info("Beginning upload for #{x.filename}...")
        x.doUpload(get_preupload_handlers, uploader)
        logger.info("Upload complete for #{x.filename}")
        logger.info("Beginning archive for #{x.filename}...")
        x.archive(get_archiver, @config[:archive_dir])
        logger.info("Processing complete for #{x.filename}")
      rescue => e
        logger.error("Failed to process #{x.filename} due to exception: #{e}")
        logger.info(e.backtrace.join("\n"))
      end
    end
  end

  logger.info("Processed #{tagged.length} valid files")

  tagged.length
end