Hello, I have a Telegram Bot which is running in a server and several days after deployment I’m getting an error that’s crashing my application but I don’t really know what can be the issue, It has happened like 3-4 times but it always happens after some days running, let’s say, 3-5 days. I’m using nadia
as the wrapper for Telegram, this is the stacktrace:
[error] Task #PID<0.477.0> started from Euridime.Supervisor terminating
** (Poison.SyntaxError) Unexpected token at position 0: <
(poison) lib/poison/parser.ex:57: Poison.Parser.parse!/2
(poison) lib/poison.ex:83: Poison.decode!/2
(nadia) lib/nadia/api.ex:40: Nadia.API.decode_response/1
(nadia) lib/nadia/api.ex:30: Nadia.API.process_response/2
(euridime) lib/euridime/telegram/task.ex:7: Euridime.Task.pull_updates/1
(elixir) lib/task/supervised.ex:85: Task.Supervised.do_apply/2
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Function: &Euridime.Task.pull_updates/0
Args: []
Same error for 3 different PIDs, the function that pulls the updates looks like this:
def pull_updates(offset \\ -1) do
case Nadia.get_updates(offset: offset) do
{:ok, updates} when length(updates) > 0 ->
Dispatcher.dispatch(updates)
:timer.sleep(1000)
pull_updates(List.last(updates).update_id + 1)
_ ->
:timer.sleep(500)
pull_updates(offset)
end
end
And the relevant part of my supervision tree looks like this:
def start(_type, _args) do
import Supervisor.Spec
# Define workers and child supervisors to be supervised
children = [
# Start the Ecto repository
supervisor(Euridime.Repo, []),
# Start the endpoint when the application starts
supervisor(Euridime.Web.Endpoint, []),
worker(Task, [Euridime.Task, :pull_updates, []], id: :pull_updates),
worker(Euridime.DETS, []),
worker(Euridime.Emailer, []),
]
...
I’m using Phoenix alongside the bot.
And the function that pulls the updates from the wrapper looks like this if it matters.
def request(method, options \\ [], file_field \\ nil) do
method
|> build_url
|> HTTPoison.post(build_request(options, file_field), [], build_options(options))
|> process_response(method)
end
...
defp process_response(response, method) do
case decode_response(response) do
{:ok, true} -> :ok
{:ok, result} -> {:ok, Nadia.Parser.parse_result(result, method)}
%{ok: false, description: description} -> {:error, %Error{reason: description}}
{:error, %HTTPoison.Error{reason: reason}} -> {:error, %Error{reason: reason}}
end
end
...
defp decode_response(response) do
with {:ok, %HTTPoison.Response{body: body}} <- response,
%{result: result} <- Poison.decode!(body, keys: :atoms),
do: {:ok, result}
end
Now my question is, why is it crashing the whole app, isn’t it meant to just be restarted? Or it goes deeper than that? Probably I should change the way I’m pulling updates from Telegram? I don’t really know what may be causing this problem.