【问题标题】:How to perform DB write health checks in Ruby?如何在 Ruby 中执行数据库写入健康检查?
【发布时间】:2020-09-08 23:05:56
【问题描述】:

我想为一个 ruby​​ 应用设置 MySQL 数据库健康检查,基本上响应应该是

{
    "read_success": true,
    "write_success": true,
    "exception": null
}

我的健康检查应该执行以下操作:

从数据库中读取一个表 写一些东西到数据库 如果上述任何操作失败,它应该抛出响应中提到的异常。

module API
  ApplicationName.controllers :health_check do
    get :index do
      status = {
          read_success: read_successful,
          write_success: write_successful,
          exception: check_exception
      }
       [200, {}, [status.to_json]]
    end
  end
end

def read_successful
  begin
    ActiveRecord::Base.connection.execute("SELECT 1")
    true
  rescue
    false
  end
end

def write_successful
  begin
    # logic to check write to database, which table should i write to?
    true
  rescue
    false
  end
end

def check_exception
  begin
    ActiveRecord::Base.connection.execute("SELECT 1")
    nil
  rescue Exception => e
    return e.message
  end
  begin
    # logic to check write to database, which table should i write to?
    nil
  rescue Exception => e
    return e.message
  end
end

我已经尝试实现上述读取健康检查,但不知道如何实现写入健康检查?有没有什么方法可以实现写健康检查,而不需要在数据库中创建一个新的健康检查表。

实现写入健康检查的逻辑应该是什么?

【问题讨论】:

  • 虽然很好,但是写健康检查是多余的。当您遇到数据库问题时,100 次中有 96 次是连接问题,因此读取运行状况检查绰绰有余。在最初出现写入问题(即磁盘已满)的极少数情况下,很快就会出现连接问题。
  • 所以你的意思是说如果我能读数据库,我就一定能写?有什么理由支持这个说法?
  • 如果你会阅读,你很有可能会写作。经验丰富。
  • 好的明白了,但是你能帮我写一个写数据库健康检查吗?
  • 从已知表中选择已知行或从已知表中选择计数。如果您得到正确的值,则检查通过。

标签: mysql ruby-on-rails ruby health-check


【解决方案1】:

终于实现了它,发布答案以便以后可能对某人有所帮助,为此,您必须在数据库中创建一个表 health_check。

CREATE TABLE `health_check` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `date` varchar(40) NOT NULL,   `status` varchar(40) NOT NULL,   PRIMARY KEY (`id`) );

API

ApplicationName.controllers :deep_health_check do
  get :index do
    time_stamp = Time.now.strftime('%Y-%m-%d %H:%M:%S')
    read_error_msg = "Exception While Reading To Database: "
    write_error_msg = "Exception While Writing to Database: "
    read_success = read_successful
    write_success = write_successful(time_stamp)
    exception = nil

    str = String.new("")

    if read_success != true && write_success != true
      str = read_error_msg + read_success + "  and  " + write_error_msg + write_success
      read_success = false
      write_success = false

    elsif read_success != true && write_success == true
      str = read_error_msg + read_success
      read_success = false
    else
      if read_success == true && write_success != true
        str = write_error_msg + write_success
        write_success = false

      end
    end

    if str != ""
      exception = str
    end


    status = {
        read_success: read_success,
        write_success: write_success,
        exception: exception
    }
    [200, {}, [status.to_json]]
  end
end

def read_successful
  begin
    ActiveRecord::Base.connection.execute("SELECT 1")
    return true
  rescue Exception => e
    return e.message
  end
end

def write_successful(time_stamp)
  begin
    write_to_a_table(time_stamp)
    return true
  rescue Exception => e
    return e.message
  end
end

def write_to_a_table(time_stamp)
  ActiveRecord::Base.transaction do
    ActiveRecord::Base.connection.execute("INSERT INTO health_check (date,status) VALUES ('#{time_stamp}', 'fine');")
  end
end

【讨论】:

    猜你喜欢
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 2022-10-14
    • 1970-01-01
    • 2014-10-02
    • 2020-07-22
    相关资源
    最近更新 更多