codedge-llc / pigeon

iOS and Android push notifications for Elixir
https://hex.pm/packages/pigeon
MIT License
646 stars 146 forks source link

Error when sending pushes from a scheduled task #32

Closed tomwilsn closed 7 years ago

tomwilsn commented 8 years ago

I am getting this error when trying to send messages from a task that was scheduled with quantum (https://github.com/c-rack/quantum-elixir)

2016-09-01T07:30:00.443676253Z app[web.1]: 07:30:00.443 [error] GenServer :apns_worker terminating
2016-09-01T07:30:00.443742038Z app[web.1]: ** (stop) exited in: :gen_fsm.sync_send_all_state_event(#PID<0.246.0>, {:new_stream, #PID<0.245.0>})
2016-09-01T07:30:00.443768527Z app[web.1]:     ** (EXIT) no process
2016-09-01T07:30:00.443778382Z app[web.1]:     (stdlib) gen_fsm.erl:249: :gen_fsm.sync_send_all_state_event/2
2016-09-01T07:30:00.443814673Z app[web.1]:     src/h2_client.erl:128: :h2_client.send_request/3
2016-09-01T07:30:00.443826612Z app[web.1]:     (pigeon) lib/pigeon/apns_worker.ex:102: Pigeon.APNSWorker.send_push/3
2016-09-01T07:30:00.443835772Z app[web.1]:     (stdlib) gen_server.erl:601: :gen_server.try_dispatch/4
2016-09-01T07:30:00.443841878Z app[web.1]:     (stdlib) gen_server.erl:667: :gen_server.handle_msg/5
2016-09-01T07:30:00.443848000Z app[web.1]:     (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

Any ideas?

hpopp commented 8 years ago

What does your scheduled task look like?

tomwilsn commented 8 years ago

It works fine if I call it myself, but if the server sits there all day when it comes time to run it I get the error above : (

  def send_reminders() do
    Logger.info("Sending reminders")

    Repo.all(PushToken, [type: "apns"])
    |> Enum.map(&(%{push_token: &1, notification: Pigeon.APNS.Notification.new(nil, &1.token, "topic") |> put_content_available}))
    |> Enum.each(fn(item) ->
      Pigeon.APNS.push(item.notification, fn(x) ->
        case x do

          {:ok, notification}  ->
            Logger.debug("Push successful!")

          {:error, :bad_device_token, message} ->
            delete_token(item.push_token)
          {:error, :unregistered, message} ->
            delete_token(item.push_token)

          {:error, reason, message} ->
            Logger.error("Some other error happened: #{inspect message}")
        end
      end)
    end)
  end

  @doc "Deletes the specified token"
  def delete_token(push_token) do
    case Repo.delete(push_token) do
      {:ok, struct} ->
        Logger.info("Deleted invalid token")
      {:error, changeset} ->
        Logger.error("Error deleting invalid token: #{changeset.id}")
    end
  end
hpopp commented 8 years ago

Does it throw this error every time it gets called from quantum? Looks like the HTTP2 socket might be timing out.

Edit @arkadiyk I just now found your issue at https://github.com/joedevivo/chatterbox/issues/89. I'm guessing chatterbox doesn't keep the connection alive on its own.

arkadiyk commented 8 years ago

Yeah, chatterbox doesn't do anything to keep the connection alive. Although in my case the symptoms are different. When connection times out the error message comes few minutes later and it's easy to overlook it.

Probably worth to try using version with the ping just to see if it fixes the problem:

{:pigeon, git: "https://github.com/codedge-llc/pigeon.git", ref: "fb7ff8d6d6"},

Btw, I am still using this version in prod.

tomwilsn commented 8 years ago

That seems to be working 👍