Files
sentinel/systemd_notifier.rb
T

54 lines
1.7 KiB
Ruby

# <<*>> -^v- SNOWARE APPLICATION
# usage: Notify systemd.
# author: S.A.
# time: 2026-06-07
# Copyright (c) [2026] [S.A. SNOWARE-SCU]
# [MineSentinel] is licensed under Mulan PubL v2.
# You can use this software according to the terms and conditions of the Mulan PubL v2.
# You may obtain a copy of Mulan PubL v2 at:
# http://license.coscl.org.cn/MulanPubL-2.0
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PubL v2 for more details.
# frozen_string_literal: true
class SystemdNotifier
require_relative 'method_executor'
public
@notify_socket = ENV["NOTIFY_SOCKET"]
@watchdog_usec = (ENV["WATCHDOG_USEC"] || '0').to_i
unless @notify_socket && File.socket?(@notify_socket)
MethodExecutor.record("error", "heartbeat", "NOTIFY_SOCKET is not set or is not a socket, am I a service?")
@enable_heartbeat = false
else
MethodExecutor.record("debug", "heartbeat", "Found NOTIFY_SOCKET, enabling heartbeat")
@enable_heartbeat = true
@socket = UNIXSocket.new(@notify_socket)
MethodExecutor.record("debug", "heartbeat", "Heartbeat socket opened")
end
def enabled?
@enable_heartbeat
end
def notify_heartbeat(message)
return unless @enable_heartbeat
begin
@socket.send(message, 0)
MethodExecutor.record("debug", "heartbeat", "Heartbeat sent: #{message}")
rescue => e
MethodExecutor.error("heartbeat", "Failed to send heartbeat: #{e}")
@enable_heartbeat = false
end
end
def self.notify(message)
MethodExecutor.record("info", "systemd", message)
end
end