1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.JobQueueMonitor do
8 @initial_state %{workers: %{}, queues: %{}, processed_jobs: 0}
9 @queue %{processed_jobs: 0, success: 0, failure: 0}
10 @operation %{processed_jobs: 0, success: 0, failure: 0}
13 GenServer.start_link(__MODULE__, @initial_state, name: __MODULE__)
18 :telemetry.attach("oban-monitor-failure", [:oban, :job, :exception], &handle_event/4, nil)
19 :telemetry.attach("oban-monitor-success", [:oban, :job, :stop], &handle_event/4, nil)
25 GenServer.call(__MODULE__, :stats)
28 def handle_event([:oban, :job, event], %{duration: duration}, meta, _) do
31 {:process_event, mapping_status(event), duration, meta}
36 def handle_call(:stats, _from, state) do
37 {:reply, state, state}
41 def handle_cast({:process_event, status, duration, meta}, state) do
44 |> Map.update!(:workers, fn workers ->
46 |> Map.put_new(meta.worker, %{})
47 |> Map.update!(meta.worker, &update_worker(&1, status, meta, duration))
49 |> Map.update!(:queues, fn workers ->
51 |> Map.put_new(meta.queue, @queue)
52 |> Map.update!(meta.queue, &update_queue(&1, status, meta, duration))
54 |> Map.update!(:processed_jobs, &(&1 + 1))
59 defp update_worker(worker, status, meta, duration) do
61 |> Map.put_new(meta.args["op"], @operation)
62 |> Map.update!(meta.args["op"], &update_op(&1, status, meta, duration))
65 defp update_op(op, :enqueue, _meta, _duration) do
67 |> Map.update!(:enqueued, &(&1 + 1))
70 defp update_op(op, status, _meta, _duration) do
72 |> Map.update!(:processed_jobs, &(&1 + 1))
73 |> Map.update!(status, &(&1 + 1))
76 defp update_queue(queue, status, _meta, _duration) do
78 |> Map.update!(:processed_jobs, &(&1 + 1))
79 |> Map.update!(status, &(&1 + 1))
82 defp mapping_status(:stop), do: :success
83 defp mapping_status(:exception), do: :failure