Class: Cohere::Transcribe::State::OutputSetLock

Inherits:
Object
  • Object
show all
Defined in:
lib/cohere/transcribe/state/locking.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, handle, key) ⇒ OutputSetLock

Returns a new instance of OutputSetLock.



74
75
76
77
78
79
# File 'lib/cohere/transcribe/state/locking.rb', line 74

def initialize(target, handle, key)
  @target = target
  @handle = handle
  @key = key
  @released = false
end

Class Attribute Details

.activeObject (readonly)

Returns the value of attribute active.



24
25
26
# File 'lib/cohere/transcribe/state/locking.rb', line 24

def active
  @active
end

.guardObject (readonly)

Returns the value of attribute guard.



24
25
26
# File 'lib/cohere/transcribe/state/locking.rb', line 24

def guard
  @guard
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



72
73
74
# File 'lib/cohere/transcribe/state/locking.rb', line 72

def target
  @target
end

Class Method Details

.acquire(target, blocking: false, operation_hook: nil) ⇒ Object



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
# File 'lib/cohere/transcribe/state/locking.rb', line 26

def acquire(target, blocking: false, operation_hook: nil)
  guard.synchronize do
    handle = nil
    acquired = false
    key = target.path.to_s
    if active.key?(key)
      raise TranscriptionRuntimeError,
            "Another transcription job owns output set #{target.identity}"
    end

    handle = State.open_lock_file(target.path)
    operation = File::LOCK_EX
    operation |= File::LOCK_NB unless blocking
    unless handle.flock(operation)
      handle.close
      raise TranscriptionRuntimeError,
            "Another transcription process owns output set #{target.identity} (lock #{target.path})"
    end
    operation_hook&.call(:after_flock, target)
    State.verify_lock_identity!(
      target.path,
      handle,
      message: "Output lock changed while acquiring #{target.identity}"
    )

    lock = new(target, handle, key)
    active[key] = lock
    acquired = true
    lock
  rescue Errno::EWOULDBLOCK, Errno::EAGAIN
    handle&.close
    raise TranscriptionRuntimeError,
          "Another transcription process owns output set #{target.identity} (lock #{target.path})"
  rescue Interrupt, SystemExit, StandardError
    handle&.close
    raise
  ensure
    handle.close if handle && !acquired && !handle.closed?
  end
end

.release_allObject



67
68
69
# File 'lib/cohere/transcribe/state/locking.rb', line 67

def release_all
  guard.synchronize { active.values.dup }.each(&:release)
end

Instance Method Details

#releaseObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cohere/transcribe/state/locking.rb', line 81

def release
  self.class.guard.synchronize do
    return if @released

    begin
      @handle.flock(File::LOCK_UN)
    ensure
      @handle.close
      @released = true
      self.class.active.delete(@key)
    end
  end
  nil
end

#released?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/cohere/transcribe/state/locking.rb', line 96

def released?
  @released
end