Class: CodexNotify::HookStore

Inherits:
Object
  • Object
show all
Defined in:
lib/codex_notify/hook_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ HookStore

Returns a new instance of HookStore.



11
12
13
# File 'lib/codex_notify/hook_store.rb', line 11

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

Instance Method Details

#advance_generation(session_id) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/codex_notify/hook_store.rb', line 57

def advance_generation(session_id)
  update_state do |data|
    key = session_id.to_s
    data['threads'].delete(key)
    data['generations'][key] = data['generations'].fetch(key, 0).to_i + 1
  end
end

#clear_suppressed_session(session_id) ⇒ Object



47
48
49
50
51
# File 'lib/codex_notify/hook_store.rb', line 47

def clear_suppressed_session(session_id)
  update_state do |data|
    data['suppressed_sessions'].delete(session_id.to_s)
  end
end

#clear_thread(session_id) ⇒ Object



25
26
27
28
29
# File 'lib/codex_notify/hook_store.rb', line 25

def clear_thread(session_id)
  update_state do |data|
    data['threads'].delete(session_id.to_s)
  end
end

#generation_for(session_id) ⇒ Object



53
54
55
# File 'lib/codex_notify/hook_store.rb', line 53

def generation_for(session_id)
  state.fetch('generations', {}).fetch(session_id.to_s, 0).to_i
end

#save_thread_ts(session_id, thread_ts) ⇒ Object



19
20
21
22
23
# File 'lib/codex_notify/hook_store.rb', line 19

def save_thread_ts(session_id, thread_ts)
  update_state do |data|
    data['threads'][session_id.to_s] = thread_ts.to_s
  end
end

#suppress_session(session_id, reason) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/codex_notify/hook_store.rb', line 31

def suppress_session(session_id, reason)
  update_state do |data|
    key = session_id.to_s
    data['threads'].delete(key)
    data['generations'][key] = data['generations'].fetch(key, 0).to_i + 1
    data['suppressed_sessions'][key] = {
      'reason' => reason.to_s,
      'suppressed_at' => Time.now.utc.iso8601
    }
  end
end

#suppressed_session?(session_id) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/codex_notify/hook_store.rb', line 43

def suppressed_session?(session_id)
  state.fetch('suppressed_sessions', {}).key?(session_id.to_s)
end

#thread_ts_for(session_id) ⇒ Object



15
16
17
# File 'lib/codex_notify/hook_store.rb', line 15

def thread_ts_for(session_id)
  state.dig('threads', session_id.to_s)
end

#with_session_lock(session_id) ⇒ Object

Keep each hook event for a session atomic, including any Slack calls made between reading and updating the state file. Callers must acquire this session lock before an operation that takes the state lock.



68
69
70
71
# File 'lib/codex_notify/hook_store.rb', line 68

def with_session_lock(session_id)
  digest = Digest::SHA256.hexdigest(session_id.to_s)
  with_file_lock(session_lock_dir.join("#{digest}.lock")) { yield }
end