Detect if Python3 is installed before launching PythonPort Genserver

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-11-08 12:14:13 +01:00
parent df21729ba0
commit ea276fbe73
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
4 changed files with 30 additions and 12 deletions

View File

@ -51,7 +51,6 @@ defmodule Mobilizon do
# workers
Guardian.DB.Token.SweeperServer,
ActivityPub.Federator,
Mobilizon.PythonWorker,
TzWorld.Backend.DetsWithIndexCache,
cachex_spec(:feed, 2500, 60, 60, &Feed.create_cache/1),
cachex_spec(:ics, 2500, 60, 60, &ICalendar.create_cache/1),
@ -73,6 +72,13 @@ defmodule Mobilizon do
] ++
task_children(@env)
children =
if Mobilizon.PythonPort.python_exists?() do
children ++ [Mobilizon.PythonWorker]
else
children
end
ErrorReporting.configure()
# Only attach the telemetry logger when we aren't in an IEx shell

View File

@ -3,10 +3,8 @@ defmodule Mobilizon.Service.Export.Participants.ODS do
Export a list of participants to ODS
"""
alias Mobilizon.Events
alias Mobilizon.{Events, Export, PythonPort, PythonWorker}
alias Mobilizon.Events.Event
alias Mobilizon.Export
alias Mobilizon.PythonWorker
alias Mobilizon.Storage.Repo
alias Mobilizon.Web.Gettext, as: GettextBackend
import Mobilizon.Web.Gettext, only: [gettext: 2]
@ -91,7 +89,7 @@ defmodule Mobilizon.Service.Export.Participants.ODS do
@spec dependencies_ok? :: boolean
def dependencies_ok? do
PythonWorker.has_module("pyexcel_ods3")
PythonPort.python_exists?() && PythonWorker.has_module("pyexcel_ods3")
end
@spec enabled? :: boolean

View File

@ -3,10 +3,8 @@ defmodule Mobilizon.Service.Export.Participants.PDF do
Export a list of participants to PDF
"""
alias Mobilizon.Events
alias Mobilizon.{Events, Export, PythonPort, PythonWorker}
alias Mobilizon.Events.Event
alias Mobilizon.Export
alias Mobilizon.PythonWorker
alias Mobilizon.Storage.Repo
alias Mobilizon.Web.ExportView
alias Mobilizon.Web.Gettext, as: GettextBackend
@ -105,7 +103,7 @@ defmodule Mobilizon.Service.Export.Participants.PDF do
@spec dependencies_ok? :: boolean
def dependencies_ok? do
PythonWorker.has_module("weasyprint")
PythonPort.python_exists?() && PythonWorker.has_module("weasyprint")
end
@spec enabled? :: boolean

View File

@ -5,15 +5,23 @@ defmodule Mobilizon.PythonPort do
use Export.Python
@python_path "/usr/bin/python3"
@doc """
Whether Python3 is installed
"""
@spec python_exists? :: boolean
def python_exists? do
File.exists?(python_path())
end
@doc """
## Parameters
- path: directory to include in python path
"""
@spec python_instance(String.t()) :: pid
def python_instance(path) do
python = "/usr/bin/python3"
{:ok, pid} = Python.start(python: python, python_path: path)
{:ok, pid} = Python.start(python: python_path(), python_path: path)
pid
end
@ -25,4 +33,12 @@ defmodule Mobilizon.PythonPort do
def call_python(pid, module, function, arguments \\ []) do
Python.call(pid, module, function, arguments)
end
@spec python_path :: String.t()
defp python_path do
case get_in(Application.get_env(:mobilizon, __MODULE__), [:path]) do
path when is_binary(path) -> path
nil -> @python_path
end
end
end