如何在Ruby中执行数据库写入运行状况检查?

问题描述

我想为ruby应用设置MySQL数据库运行状况检查,基本上响应应该是

futures = []

for id_ in id_list:
    futures.append(scrape.remote(id_,str(id_.split('/')[-1]),id_list.index(id_),len(id_list),target_path))

all_results = ray.get(futures)

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

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

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

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

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

解决方法

最后实现它,发布答案,以便稍后可以帮助某人,为此,您必须在数据库中创建一个表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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...