added example cache purge script

This commit is contained in:
Maksim Pechnikov 2020-05-16 15:16:33 +03:00
parent cb40602a16
commit 3f8d68bdf3
5 changed files with 64 additions and 16 deletions

View file

@ -382,7 +382,7 @@ config :pleroma, :media_proxy,
enabled: false, enabled: false,
provider: Pleroma.Web.MediaProxy.Invalidation.Script, provider: Pleroma.Web.MediaProxy.Invalidation.Script,
options: %{ options: %{
script_path: "" script_path: "./installation/nginx-cache-purge.example"
} }
], ],
proxy_opts: [ proxy_opts: [

View file

@ -0,0 +1,39 @@
#!/bin/bash
# A simple Bash script to delete an media from the Nginx cache.
SCRIPTNAME=${0##*/}
# NGINX cache directory
CACHE_DIRECTORY="/tmp/pleroma-media-cache"
function get_cache_files() {
local max_parallel=${3-16}
find $2 -maxdepth 1 -type d | xargs -P $max_parallel -n 1 grep -ERl "^KEY:.*$1" | sort -u
}
function purge_item() {
local cache_files
cache_files=$(get_cache_files "$1" "$2")
if [ -n "$cache_files" ]; then
for i in $cache_files; do
[ -f $i ] || continue
echo "Deleting $i from $2."
rm $i
done
else
echo "$1 is not cached."
fi
}
function purge() {
for url in "$@"
do
echo "$SCRIPTNAME delete $url from cache ($CACHE_DIRECTORY)"
purge_item $url $CACHE_DIRECTORY
done
}
purge $1

View file

@ -0,0 +1,16 @@
defmodule Pleroma.Web.MediaProxy.Invalidation.Http do
@behaviour Pleroma.Web.MediaProxy.Invalidation
@impl Pleroma.Web.MediaProxy.Invalidation
def purge(urls, opts) do
method = Map.get(opts, :http_method, :purge)
headers = Map.get(opts, :http_headers, [])
options = Map.get(opts, :http_options, [])
Enum.each(urls, fn url ->
Pleroma.HTTP.request(method, url, "", headers, options)
end)
{:ok, "success"}
end
end

View file

@ -1,12 +0,0 @@
defmodule Pleroma.Web.MediaProxy.Invalidation.Nginx do
@behaviour Pleroma.Web.MediaProxy.Invalidation
@impl Pleroma.Web.MediaProxy.Invalidation
def purge(urls, _opts) do
Enum.each(urls, fn url ->
Pleroma.HTTP.request(:purge, url, "", [], [])
end)
{:ok, "success"}
end
end

View file

@ -2,9 +2,14 @@ defmodule Pleroma.Web.MediaProxy.Invalidation.Script do
@behaviour Pleroma.Web.MediaProxy.Invalidation @behaviour Pleroma.Web.MediaProxy.Invalidation
@impl Pleroma.Web.MediaProxy.Invalidation @impl Pleroma.Web.MediaProxy.Invalidation
def purge(urls, %{script_path: script_path} = options) do def purge(urls, %{script_path: script_path} = _options) do
script_args = List.wrap(Map.get(options, :script_args, [])) args =
System.cmd(Path.expand(script_path), [urls] ++ script_args) urls
|> List.wrap()
|> Enum.uniq()
|> Enum.join(" ")
System.cmd(Path.expand(script_path), [args])
{:ok, "success"} {:ok, "success"}
end end
end end