Class: Datadog::Core::Cloudwise::HostIdWorker

Inherits:
Worker
  • Object
show all
Includes:
Workers::Polling
Defined in:
lib/datadog/core/cloudwise/host_id_worker.rb

Overview

Worker that generates Host ID and retries every 30 seconds on failure Host ID 生成失败时,探针不采集数据

Constant Summary collapse

RETRY_INTERVAL =

30 seconds interval for retry

30
CODE_SUCCESS =

成功的状态码

1000

Constants included from Workers::Polling

Workers::Polling::DEFAULT_SHUTDOWN_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Worker

#task

Instance Method Summary collapse

Methods included from Workers::Polling

#enabled=, #enabled?, included, #stop

Constructor Details

#initialize(client:, logger:, probe_state:, **options) ⇒ HostIdWorker

Returns a new instance of HostIdWorker.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/datadog/core/cloudwise/host_id_worker.rb', line 22

def initialize(client:, logger:, probe_state:, **options)
  @client = client
  @logger = logger
  @probe_state = probe_state
  @host_id_generated = false

  # Workers::Async::Thread settings
  self.fork_policy = options.fetch(:fork_policy, Workers::Async::Thread::FORK_POLICY_STOP)

  # Workers::IntervalLoop settings
  self.loop_base_interval = RETRY_INTERVAL

  self.enabled = options.fetch(:enabled, true)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



20
21
22
# File 'lib/datadog/core/cloudwise/host_id_worker.rb', line 20

def client
  @client
end

Instance Method Details

#host_id_generated?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/datadog/core/cloudwise/host_id_worker.rb', line 75

def host_id_generated?
  @host_id_generated
end

#performObject



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
66
67
68
69
70
71
72
73
# File 'lib/datadog/core/cloudwise/host_id_worker.rb', line 37

def perform
  # DEBUG: 添加详细日志来诊断重试问题
  # 如果已经生成,则停止
  if @host_id_generated
    Cloudwise.log_info { "[WORKER DEBUG] Host ID already generated, stopping loop" }
    stop_loop
    return true
  end

  Cloudwise.log_debug { 'Cloudwise: Generating Host ID' }

  result = client.generate_host_id

  # 检查是否成功且 code == 1000
  if result[:success] && result[:code] == CODE_SUCCESS && client.
    @host_id_generated = true
    Cloudwise.log_debug { "Cloudwise: Host ID generated successfully, account_id: #{client.}" }

    # 标记 Host ID 就绪
    probe_state.mark_host_id_ready!

    # Stop this worker as we only need to generate once
    stop_loop
  else
    # 失败或 code 不等于 1000,数据不采集
    error_msg = result[:error] || "code=#{result[:code]} (expected #{CODE_SUCCESS})"
    Cloudwise.log_warn { "Cloudwise: Host ID generation failed: #{error_msg}, will retry in #{RETRY_INTERVAL}s" }

    # 确保状态标记为未就绪
    probe_state.mark_host_id_failed!
  end
  true
rescue => e
  Cloudwise.log_error { "Cloudwise: Host ID worker error: #{e.class.name} #{e.message}" }
  probe_state.mark_host_id_failed!
  true # Continue running for retry
end