Class: Bricolage::FileTaskQueue

Inherits:
TaskQueue show all
Defined in:
lib/bricolage/taskqueue.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TaskQueue

#consume_each, #deq, #each, #empty?, #enq, #next, #size

Constructor Details

#initialize(path) ⇒ FileTaskQueue

Returns a new instance of FileTaskQueue.



82
83
84
85
# File 'lib/bricolage/taskqueue.rb', line 82

def initialize(path)
  super()
  @path = path
end

Class Method Details

.restore_if_exist(path) ⇒ Object



76
77
78
79
80
# File 'lib/bricolage/taskqueue.rb', line 76

def FileTaskQueue.restore_if_exist(path)
  q = new(path)
  q.restore if q.queued?
  q
end

Instance Method Details

#lockObject



120
121
122
# File 'lib/bricolage/taskqueue.rb', line 120

def lock
  FileUtils.touch lock_file_path
end

#lock_file_pathObject



128
129
130
# File 'lib/bricolage/taskqueue.rb', line 128

def lock_file_path
  Pathname.new("#{@path}.LOCK")
end

#locked?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/bricolage/taskqueue.rb', line 116

def locked?
  lock_file_path.exist?
end

#queued?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/bricolage/taskqueue.rb', line 87

def queued?
  @path.exist?
end

#restoreObject



110
111
112
113
114
# File 'lib/bricolage/taskqueue.rb', line 110

def restore
  File.foreach(@path) do |line|
    enq JobTask.deserialize(line)
  end
end

#saveObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bricolage/taskqueue.rb', line 91

def save
  if empty?
    @path.unlink if @path.exist?
    return
  end
  FileUtils.mkdir_p @path.dirname
  tmpname = "#{@path}.tmp.#{Process.pid}"
  begin
    File.open(tmpname, 'w') {|f|
      each do |task|
        f.puts task.serialize
      end
    }
    File.rename tmpname, @path
  ensure
    FileUtils.rm_f tmpname
  end
end

#unlockObject



124
125
126
# File 'lib/bricolage/taskqueue.rb', line 124

def unlock
  FileUtils.rm_f lock_file_path
end

#unlock_helpObject



132
133
134
# File 'lib/bricolage/taskqueue.rb', line 132

def unlock_help
  "remove the file: #{lock_file_path}"
end