dev vouch dev on at. thats about it atvouch.dev
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

add a mutex guard for comment processing

authored by

Luna and committed by tangled.org c8a33be6 d557ead6

+20 -16
+1
appview/lib/atvouch/application.ex
··· 10 10 children = 11 11 repos() ++ 12 12 [ 13 + {Mutex, name: Atvouch.Mutex}, 13 14 {Bandit, plug: Atvouch.Router, port: port, ip: {127, 0, 0, 1}}, 14 15 Atvouch.Tinycron.new(Atvouch.CounterTask, every: 60, jitter: -5..5) 15 16 ] ++ tangled_children() ++ tap_children()
+17 -15
appview/lib/atvouch/tap_handler.ex
··· 401 401 402 402 comment = Atvouch.Tangled.CommentBuilder.build_comment(author_did, author_handle, maintainer_routes, handle_map) 403 403 404 - # Record the comment first to prevent races between duplicate events 405 - now = DateTime.utc_now() |> DateTime.to_iso8601() 406 - 407 - case Atvouch.BotComment.create(%{ 408 - repo_at_uri: repo_at_uri, 409 - pull_number: pull_number, 410 - author_did: author_did, 411 - commented_at: now 412 - }) do 413 - {:ok, _} -> 404 + Mutex.with_lock(Atvouch.Mutex, repo_at_uri, fn -> 405 + # Re-check after acquiring the lock in case another process already commented 406 + if Atvouch.BotComment.exists?(repo_at_uri, pull_number) do 407 + Logger.debug("Comment already exists for #{repo_handle}/#{repo_name}##{pull_number}, skipping") 408 + :ok 409 + else 414 410 case Atvouch.Tangled.Client.post_comment(repo_handle, repo_name, pull_number, comment) do 415 411 :ok -> 412 + now = DateTime.utc_now() |> DateTime.to_iso8601() 413 + 414 + Atvouch.BotComment.create(%{ 415 + repo_at_uri: repo_at_uri, 416 + pull_number: pull_number, 417 + author_did: author_did, 418 + commented_at: now 419 + }) 420 + 416 421 Logger.info("Posted vouch comment on #{repo_handle}/#{repo_name}##{pull_number}") 417 422 :ok 418 423 ··· 420 425 Logger.warning("Failed to post comment on #{repo_handle}/#{repo_name}##{pull_number}: #{inspect(reason)}") 421 426 :ok 422 427 end 423 - 424 - {:error, %Ecto.Changeset{} = changeset} -> 425 - Logger.debug("Comment already recorded for #{repo_handle}/#{repo_name}##{pull_number}, skipping (#{inspect(changeset.errors)})") 426 - :ok 427 - end 428 + end 429 + end) 428 430 end 429 431 430 432 end
+2 -1
appview/mix.exs
··· 37 37 {:gun, "~> 2.0"}, 38 38 {:cowlib, "~> 2.12", override: true}, 39 39 {:atex, "~> 0.7", app: false}, 40 - {:floki, "~> 0.38.0"} 40 + {:floki, "~> 0.38.0"}, 41 + {:mutex, "~> 3.0"} 41 42 ] 42 43 end 43 44