public
Description: Gitorious aims to provide a great way of doing distributed opensource code collaboration.
Homepage: http://gitorious.org/projects/gitorious
Clone URL: git://github.com/dysinger/gitorious.git
gitorious / script / task_performer
100755 47 lines (39 sloc) 1.149 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
#!/usr/bin/env ruby
 
require 'tmpdir'
require "fileutils"
 
LOCK_FILE_PATH = File.join(Dir.tmpdir, "gitorious_task_lockfile")
ENV["PATH"] = "/usr/local/bin/:/opt/local/bin:#{ENV["PATH"]}"
 
if File.exist?(LOCK_FILE_PATH)
    $stderr.puts "Task lockfile '#{LOCK_FILE_PATH}' exists!"
    exit(1)
end
 
ENV["RAILS_ENV"] ||= "production"
require File.dirname(__FILE__) + "/../config/environment"
 
 
class TaskLockError < StandardError; end
 
def with_lock(&block)
  begin
    if File.exist?(LOCK_FILE_PATH)
      raise TaskLockError
    end
    FileUtils.touch(LOCK_FILE_PATH)
    yield
    FileUtils.rm(LOCK_FILE_PATH)
  rescue TaskLockError
    $stderr.puts "Task lockfile exists"
    exit(1)
  end
end
 
with_lock do
  log = Logger.new(File.join(RAILS_ROOT, "log", "tasks.log"))
  log.formatter = Logger::Formatter.new
  log.level = Logger::INFO
  log.formatter.datetime_format = "%Y-%m-%d %H:%M:%S"
  begin
    Task.perform_all_pending!(log)
  rescue => e
    exception_backtrace_string = "#{e.class.name} #{e.message}\n#{e.backtrace.join("\n ")}"
    log.fatal exception_backtrace_string
    $stderr.puts exception_backtrace_string
    exit(1)
  end
end