0

I have a need for part of my application to make calls to Reddit asynchronously from my core application's workflow. I have implemented a semi-workable solution by using a Reddit API library I have built here. For those that are unaware, Reddit manages authentication via OAuth and returns a bearer and a token for a particular user that expires in 60 minutes after generation.

I have opted to use cookies to store this authorization information for the mentioned time period, as seen in the requestRedditToken() method here. If a cookie is not found (i.e. it has expired) when another request to Reddit needs to be made, another reddit token is generated. This seems like it would work just fine.

What I am having trouble with is conceptualizing how cookies are handled when integrated with a daemonized queue worker, furthermore, I need to understand why these calls are failing periodically.

The application I'm working with, as mentioned, makes calls to Reddit. These calls are created by a job class being handled: UpdateRedditLiveThreadJob, which you can see here.

These jobs are processed by a daemonized Artisan queue worker using Laravel Forge, you can see the details of the worker here. The queue driver in this case is Redis, and the workers are monitored by Supervisor.

Here is the intended workflow of my app:

  1. An UpdateRedditLiveThreadJob is created and thrown into the queue to be handled.
  2. The handle() method of the job is called.
  3. A Reddit client is instantiated, and a reddit token is requested if a cookie doesn't exist.
  4. My Reddit client successfully communicates with Reddit.
  5. The Job is considered complete.

What is actually happening:

  1. The job is created.
  2. Handle is called.
  3. Reddit client is instantiated, something odd happens here generally.
  4. Reddit client tries to communicate, but gets a 401 response which produces an Exception. This is indicative of a failed authorization.
  5. The task is considered 'failed' and loops back to step 2.

Here are my questions:

  1. Why does this flow work for the first hour, and then collapse as described above, after presumably, the cookie has expired?

  2. I've tried my best to understand how Laravel Queues work, but I fundamentally am having a hard time of conceptualizing the different types of queue management options available: queue:listen, queue:work, a daemonized queue:work running on Supervisor, etc. Is my current queue infrastructure compatible with using cookies to manage tokens?

  3. What adjustments do I need to make to my codebase to make the app function as intended?

  4. How will my workflow handle multiple users, who each potentially have multiple cookies?

  5. Why does the workflow magically start working again if I restart my queue worker?

Please let me know if I'm incorrectly describing anything here or need clarification, I've tried my best to explain the problem succinctly.

Community
  • 1
  • 1
marked-down
  • 9,958
  • 22
  • 87
  • 150

1 Answers1

1

Your logic is incorrect. A queue job is in fact a cli running php script. It has no interaction with a browser. Cookies are set in a browser, see this related thread for reference.

Seeing you're interacting with an API it would make more sense to set the token as a simple variable in the Job (or better yet in that wrapper) and then re-use that within that job.

TL:DR: your wrapper is not an API client.

I know this is not a complete answer to all your questions, but it's a push in the right direction. Because would I have answered all your questions - in the end - might not have given any solution to your issues ;)

Community
  • 1
  • 1
Luceos
  • 6,629
  • 1
  • 35
  • 65