Skip to content

Commit

Permalink
fix: 修复Oban Worker在后台执行任务时因为map的key类型的不同而出错
Browse files Browse the repository at this point in the history
  • Loading branch information
shuiRong committed Dec 25, 2024
1 parent 51057c3 commit 8394545
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ config :honyaku, HonyakuWeb.Endpoint,
config :honyaku, dev_routes: true

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"
config :logger, :console, format: "[$level] $message\n", level: :info

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
Expand Down
4 changes: 4 additions & 0 deletions lib/honyaku/contexts/feeds/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ defmodule Honyaku.Feeds.Parser do
_ -> ""
end

Logger.info("translate_and_save_feed_field: #{field} #{target_lang} #{source_lang}")

with {:ok, translated_text} <- translate(text, target_lang, source_lang),
{:ok, _translation} <-
Repo.insert(%Translation{
Expand Down Expand Up @@ -416,6 +418,8 @@ defmodule Honyaku.Feeds.Parser do
_ -> ""
end

Logger.info("translate_and_save_article_field: #{field} #{target_lang} #{source_lang}")

with {:ok, translated_text} <- translate(text, target_lang, source_lang),
{:ok, _translation} <-
Repo.insert(%Translation{
Expand Down
56 changes: 50 additions & 6 deletions lib/honyaku/job/translate_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,72 @@ defmodule Honyaku.TranslateJob do
@impl Oban.Worker
def perform(%Oban.Job{
args: %{
"saved_feed" => saved_feed,
"saved_feed" => %{
"id" => id,
"title" => title,
"subtitle" => subtitle
},
"field" => field,
"target_lang" => target_lang,
"source_lang" => source_lang
}
}) do
Parser.translate_and_save_feed_field(saved_feed, field, target_lang, source_lang)
case Parser.translate_and_save_feed_field(
%{
id: id,
title: title,
subtitle: subtitle
},
field,
target_lang,
source_lang
) do
{:ok, translated_text} ->
Logger.info("Feed 翻译成功 - #{field}: #{inspect(translated_text)}")
:ok

:ok
error ->
Logger.error("Feed 翻译失败 - #{field}: #{inspect(error)}")
error
end
end

@impl Oban.Worker
def perform(%Oban.Job{
args: %{
"saved_article" => saved_article,
"saved_article" => %{
"id" => id,
"title" => title,
"content" => %{"value" => content},
"summary" => %{"value" => summary}
},
"field" => field,
"target_lang" => target_lang,
"source_lang" => source_lang
}
}) do
Parser.translate_and_save_article_field(saved_article, field, target_lang, source_lang)
case Parser.translate_and_save_article_field(
%{
id: id,
title: title,
content: %{
"value" => content
},
summary: %{
"value" => summary
}
},
field,
target_lang,
source_lang
) do
{:ok, translated_text} ->
Logger.info("Article 翻译成功 - #{field}: #{inspect(translated_text)}")
:ok

:ok
error ->
Logger.error("Article 翻译失败 - #{field}: #{inspect(error)}")
error
end
end
end

0 comments on commit 8394545

Please sign in to comment.