数据库备份脚本中怀疑内存泄漏

时间:2016-07-27 12:05:27

标签: memory-leaks elixir phoenix-framework

我有以下数据库备份脚本,每小时运行一次。

我最近开始看到我的停靠部署的Elixir应用程序崩溃,并显示以下错误消息:

Crash dump is being written to: erl_crash.dump...done

2016-07-27T11:05:39.754630931Z app[web.1]: binary_alloc: Cannot allocate 124512968 bytes of memory (of type "binary").

我的数据库一直在稳步增长,现在大约为130mb。

我使用下面的脚本进行备份并上传到So.这里是否有明显的故障导致内存崩溃?

defmodule MyApp.Backup do
  require Logger
  alias MyApp.{ Repo,  BackupUploader, S3 }

  @database System.get_env("DATABASE_URL")
  @bucket Application.get_env(:arc, :bucket)
  @folder "backups"

  def start do
    Logger.info "*** Initiating database backup ***"
    backup = %BackupRequest{}

    backup
    |> dump_database
    |> upload_to_s3
  end

  defp dump_database(%BackupRequest{} = backup) do
    Logger.info "*** Dumping database ***"
    command = "pg_dump"
    args = [@database]
    {data, 0} = System.cmd command, args

    %{backup | data: data, status: "dumped"}
  end

  defp upload_to_s3(%BackupRequest{data: data} = backup) do
    Logger.info "*** Uploading to S3 bucket ***"
    key = get_s3_key
    ExAws.S3.put_object!(@bucket, key, data)

    Logger.info "*** Backup complete ***"
  end

  # Helpers
  #
  #

  defp get_s3_key do
    {{year, month, day}, {hour, minute, _seconds}} = :os.timestamp |> :calendar.now_to_datetime

    hash = SecureRandom.urlsafe_base64(32)
    date = "#{day}-#{month}-#{year}-#{hour}:#{minute}"
    key  = @folder <> "/#{date}_#{hash}_#{Mix.env}"

    key
  end

end

0 个答案:

没有答案