| 28934d7 by Johan Sørensen at 2008-03-30 | 1 | #!/usr/bin/env ruby |
| 2 | ||
| a260e9e by David A. Cuadrado at 2008-04-12 | 3 | require 'tmpdir' |
| 4 | require "fileutils" |
|
| 5 | ||
| 6 | LOCK_FILE_PATH = File.join(Dir.tmpdir, "gitorious_graphgenerator_lockfile") |
|
| 7 | if File.exist?(LOCK_FILE_PATH) |
|
| 8 | $stderr.puts "Lockfile '#{LOCK_FILE_PATH}' exists!" |
|
| 9 | exit(1) |
|
| 10 | end |
|
| 11 | ||
| 899296a by Simon Hausmann at 2008-11-28 | 12 | ENV["PATH"] = "/usr/local/bin/:/opt/local/bin:#{ENV["PATH"]}" |
| 13 | ||
| a260e9e by David A. Cuadrado at 2008-04-12 | 14 | FileUtils.touch(LOCK_FILE_PATH) |
| 15 | ||
| 28934d7 by Johan Sørensen at 2008-03-30 | 16 | ENV["RAILS_ENV"] ||= "production" |
| 17 | require File.dirname(__FILE__) + "/../config/environment" |
|
| 18 | ||
| 19 | log = Logger.new(File.join(RAILS_ROOT, "log", "graph_generator.log")) |
|
| 20 | log.formatter = Logger::Formatter.new |
|
| 21 | log.level = Logger::INFO |
|
| 22 | log.formatter.datetime_format = "%Y-%m-%d %H:%M:%S" |
|
| 23 | ||
| 19be11b by David A. Cuadrado at 2008-04-12 | 24 | STATUS_FILE = File.join(RAILS_ROOT, "tmp", "graph_generator.status") |
| 25 | update_status = !File.exist?(STATUS_FILE) |
|
| d586d18 by David A. Cuadrado at 2008-04-12 | 26 | tmpdir = File.join(RAILS_ROOT, "tmp", "graph_generator") |
| 27 | if !File.directory?(tmpdir) |
|
| 28 | FileUtils.mkdir_p(tmpdir) |
|
| 19be11b by David A. Cuadrado at 2008-04-12 | 29 | elsif !update_status |
| 30 | mtime = File.mtime(STATUS_FILE).utc |
|
| 31 | ||
| 32 | days = (Time.now.utc - mtime) / (3600*24) |
|
| 33 | if days >= 5 |
|
| 34 | log.info "Cleaning '#{tmpdir}'..." |
|
| 35 | Dir.glob(File.join(tmpdir, "*.status")) do |file| |
|
| 36 | FileUtils.rm(file) |
|
| 37 | end |
|
| 38 | ||
| 39 | update_status = true |
|
| 40 | end |
|
| 41 | end |
|
| 42 | ||
| 43 | if update_status |
|
| 44 | FileUtils.touch(STATUS_FILE) |
|
| d586d18 by David A. Cuadrado at 2008-04-12 | 45 | end |
| 46 | ||
| 0ec5103 by Johan Sørensen at 2008-03-30 | 47 | log.info "Starting graph generation run..." |
| 28934d7 by Johan Sørensen at 2008-03-30 | 48 | Repository.find(:all).each_with_index do |repository, index| |
| 49 | begin |
|
| 50 | Gitorious::Graphs::Builder.generate_all_for(repository) |
|
| 51 | if index % 5 == 0 |
|
| 52 | # GC every five cycles |
|
| 53 | GC.start |
|
| 54 | end |
|
| 55 | sleep(0.1) |
|
| 56 | rescue => e |
|
| 57 | log.fatal "Error generating graphs for repo##{repository.id}" |
|
| 58 | exception_backtrace_string = "#{e.class.name} #{e.message}\n#{e.backtrace.join("\n ")}" |
|
| 59 | log.fatal exception_backtrace_string |
|
| 60 | $stderr.puts exception_backtrace_string |
|
| 61 | # exit(1) |
|
| 62 | end |
|
| 63 | end |
|
| a260e9e by David A. Cuadrado at 2008-04-12 | 64 | log.info "Done with graph generation run" |
| 65 | ||
| 66 | FileUtils.rm(LOCK_FILE_PATH) |

