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, :failure], &handle_event/4, nil)
19 :telemetry.attach("oban-monitor-success", [:oban, :success], &handle_event/4, nil)
25 GenServer.call(__MODULE__, :stats)
28 def handle_event([:oban, status], %{duration: duration}, meta, _) do
29 GenServer.cast(__MODULE__, {:process_event, status, duration, meta})
33 def handle_call(:stats, _from, state) do
34 {:reply, state, state}
38 def handle_cast({:process_event, status, duration, meta}, state) do
41 |> Map.update!(:workers, fn workers ->
43 |> Map.put_new(meta.worker, %{})
44 |> Map.update!(meta.worker, &update_worker(&1, status, meta, duration))
46 |> Map.update!(:queues, fn workers ->
48 |> Map.put_new(meta.queue, @queue)
49 |> Map.update!(meta.queue, &update_queue(&1, status, meta, duration))
51 |> Map.update!(:processed_jobs, &(&1 + 1))
56 defp update_worker(worker, status, meta, duration) do
58 |> Map.put_new(meta.args["op"], @operation)
59 |> Map.update!(meta.args["op"], &update_op(&1, status, meta, duration))
62 defp update_op(op, :enqueue, _meta, _duration) do
64 |> Map.update!(:enqueued, &(&1 + 1))
67 defp update_op(op, status, _meta, _duration) do
69 |> Map.update!(:processed_jobs, &(&1 + 1))
70 |> Map.update!(status, &(&1 + 1))
73 defp update_queue(queue, status, _meta, _duration) do
75 |> Map.update!(:processed_jobs, &(&1 + 1))
76 |> Map.update!(status, &(&1 + 1))