25

I am sending push notifications from my server and want it to simply be executed in a background process. I've read the Laravel docs and I know about database driver and some other options as well. I've got it working with database driver on my local machine but one thing is bugging me that I've to start a background thread to listen for jobs which gets added to the queue using php artisan queue:listen --deamon.

The point is, it is always consuming some of my resources and memory by running like a 'crone' task. I only want to create a new process when I trigger the push notification and it should start execution as soon it is added and after that that process should be closed. While on the other hand with laravel jobs I always have to run a background process which I want to avoid and also I am using a shared hosting which doesn't allow me to install "supervisor" on my server to monitor my jobs execution.

Can anyone clear this ambiguity?? What will be the better way to handle this scenario?

tmw
  • 1,424
  • 1
  • 15
  • 26
  • 6
    The sync driver executes the process on the main execution thread instead of a background worker. Which is useful for debugging – Sander Visser Apr 18 '17 at 08:52
  • @SanderVisser what is the better way then? – tmw Apr 18 '17 at 09:00
  • You can use the database driver, but then you need to have a background process running. Or you can use the sync driver but that one executes on the main thread. In PHP world it's hard to spawn a background process from the main thread and it comes with a lot issues. But if you really want to you can create your own driver that spawns a background task. – Sander Visser Apr 18 '17 at 09:19

2 Answers2

37

After some research, I've decided to go with database driver. There are some other great options as well, but the choice will depend on your system workload.

The point is, sync driver uses the main thread for execution of tasks which is useful for only when you are in development. If you have a production system then you might need to consider some other option to run your queue. The main idea of queuing long-running tasks is to be able to execute them in some background process so your main application thread won't block and you can serve your client requests more quickly.

For further information on different drivers and help please visit Laravel docs

tmw
  • 1,424
  • 1
  • 15
  • 26
  • 3
    Don't go with the database if you're going to run the application in a production environment, best go with beanstalkd or something similar as database isn't exactly write/read safe when it comes to fast writing/reading – jlos Jul 16 '18 at 18:54
  • 1
    @JanVansteenlandt you are right about production environment but this was for starter application. – tmw Jul 17 '18 at 07:24
  • 2
    `Redis` is one of the best option instead of the database. – Ganesh K Nov 19 '18 at 02:42
  • 1
    Yes but that will depend on your application requirements. – tmw Nov 19 '18 at 10:29
1

Queue sync run in same thread with http request or thread now. Deamon not run, i will use sync queue in local test, yo use dd(), show all data to screen