Skip to content

Commit 8e91089

Browse files
committed
Add failed status and retry the job
1 parent 768c5dc commit 8e91089

5 files changed

Lines changed: 13 additions & 9 deletions

File tree

lib/google_crawler/search.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ defmodule GoogleCrawler.Search do
7272
def create_and_search_keyword(attrs \\ %{}, user) do
7373
case create_keyword(attrs, user) do
7474
{:ok, %Keyword{} = keyword} ->
75-
search_task = fn -> GoogleCrawler.Search.SearchKeywordTask.perform(keyword) end
76-
Task.Supervisor.start_child(GoogleCrawler.TaskSupervisor, search_task)
75+
GoogleCrawler.Task.perform(GoogleCrawler.Search.SearchKeywordTask, keyword)
7776
{:ok, %Keyword{}}
7877

7978
{:error, %Ecto.Changeset{} = changeset} ->

lib/google_crawler/search/keyword.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import EctoEnum
22

3-
defenum(GoogleCrawler.Search.Keyword.Status, in_queue: 0, in_progress: 1, completed: 2)
3+
defenum(GoogleCrawler.Search.Keyword.Status, in_queue: 0, in_progress: 1, completed: 2, failed: 3)
44

55
defmodule GoogleCrawler.Search.Keyword do
66
use Ecto.Schema

lib/google_crawler/search/page_fetcher.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ defmodule GoogleCrawler.Search.PageFetcher do
22
@url "https://www.google.com/search?q="
33

44
def fetch(keyword) do
5-
case HTTPoison.get(@url <> keyword) do
5+
IO.puts "Performing search ... #{@url <> URI.encode(keyword)}"
6+
case HTTPoison.get(@url <> URI.encode(keyword)) do
67
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
78
{:ok, body}
89

lib/google_crawler/search/search_keyword_task.ex

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@ defmodule GoogleCrawler.Search.SearchKeywordTask do
55
alias GoogleCrawler.Search.PageScrapper
66

77
def perform(%Keyword{} = keyword) do
8-
# Update the status to in progress
98
Search.update_keyword(keyword, %{status: :in_progress})
109

1110
case PageFetcher.fetch(keyword.keyword) do
1211
{:ok, body} ->
13-
# Store the result to the db
1412
result = PageScrapper.scrap(body)
15-
IO.inspect result
1613
Search.update_keyword(keyword, %{
1714
status: :completed,
1815
raw_html_result: result.raw_html_result
1916
})
2017

2118
{:error, reason} ->
22-
# TODO: Retry the task
23-
IO.inspect(reason)
19+
Search.update_keyword(keyword, %{ status: :failed })
20+
raise "Keyword search failed: #{reason}"
2421
end
2522
end
2623
end

lib/google_crawler/task.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule GoogleCrawler.Task do
2+
def perform(task, args) do
3+
Task.Supervisor.start_child(GoogleCrawler.TaskSupervisor, fn ->
4+
task.perform(args)
5+
end, restart: :transient)
6+
end
7+
end

0 commit comments

Comments
 (0)