Module: Datadog::CI::Utils::Stateful

Included in:
Remote::Component, TestManagement::Component, TestOptimisation::Component, TestVisibility::Component
Defined in:
lib/datadog/ci/utils/stateful.rb

Overview

Module for components that need to persist and restore state

Instance Method Summary collapse

Instance Method Details

#load_component_stateObject

Load component state



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/datadog/ci/utils/stateful.rb', line 24

def load_component_state
  # Check for DDTest cache first
  if TestRun.test_optimization_data_cached?
    Datadog.logger.debug { "DDTest cache found" }
    return true if restore_state_from_datadog_test_runner
  end

  test_visibility_component = Datadog.send(:components).test_visibility
  return false unless test_visibility_component.client_process?

  state = Utils::FileStorage.retrieve(storage_key)
  unless state
    Datadog.logger.debug { "No component state found in file storage (key=#{storage_key})" }
    return false
  end

  restore_state(state)
  Datadog.logger.debug { "Loaded component state from file storage (key=#{storage_key})" }

  true
end

#load_json(file_name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/datadog/ci/utils/stateful.rb', line 63

def load_json(file_name)
  file_path = File.join(Ext::DDTest::TESTOPTIMIZATION_CACHE_PATH, file_name)

  unless File.exist?(file_path)
    Datadog.logger.debug { "JSON file not found: #{file_path}" }
    return nil
  end

  content = File.read(file_path)
  JSON.parse(content)
rescue JSON::ParserError => e
  Datadog.logger.debug { "Failed to parse JSON file #{file_path}: #{e.message}" }
  nil
rescue => e
  Datadog.logger.debug { "Failed to load JSON file #{file_path}: #{e.message}" }
  nil
end

#restore_state(state) ⇒ Object

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/datadog/ci/utils/stateful.rb', line 51

def restore_state(state)
  raise NotImplementedError, "Components must implement #restore_state"
end

#restore_state_from_datadog_test_runnerObject



59
60
61
# File 'lib/datadog/ci/utils/stateful.rb', line 59

def restore_state_from_datadog_test_runner
  false
end

#serialize_stateObject

These methods must be implemented by including classes

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/datadog/ci/utils/stateful.rb', line 47

def serialize_state
  raise NotImplementedError, "Components must implement #serialize_state"
end

#storage_keyObject

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/datadog/ci/utils/stateful.rb', line 55

def storage_key
  raise NotImplementedError, "Components must implement #storage_key"
end

#store_component_stateObject

Store component state



14
15
16
17
18
19
20
21
# File 'lib/datadog/ci/utils/stateful.rb', line 14

def store_component_state
  state = serialize_state

  res = Utils::FileStorage.store(storage_key, state)
  Datadog.logger.debug { "Stored component state (key=#{storage_key}): #{res}" }

  res
end