質問

I'm working on full AJAX mailing application (SPA) written with Laravel4 and AngularJS where users can create messages and sends it to their defined contacts group.

The problem is that a task responsible for sends mailing might take very long time so I have to run it in the background.

The main functionality I want to provide is the ability to "start" and "stop" the whole mailing task and in this moment I'm stuck...

I thinking about two approaches:

1. Send mailing as long as application is opened in the browser through "ajaxes"

The process of sending might look like that:

  1. a user clicks "start" button
  2. the application runs ajax request for send one message
  3. when previous request is succeeded the next one is fired up (after checking if user not stoped it before) and so on
  4. when user clicks "stop" button the application sends another request for marking the mailing to stop

This approach can give me more control about "start" and "stop" the whole sending process and some of statistics for example to update a progressbar of whole sending for the user.

But the main limit is what about the sending for a very long time... suppose more than one hour or like that...? The user should not be forced to still have opened browser, cause otherwise the mailing will be stoped... :( Or what about page refresh... :/ (I know I can use the onunload event in javascript, but it's not elegant solution)

2. Run artisan command through ajax with > /dev/null 2>/dev/null & (no waiting for command output)

This time it might look like this:

  1. the user click "start" button
  2. the application make ajax request with:

    exec('php '.base_path().'/artisan mailing:send 1 > /dev/null 2>/dev/null &');

  3. the application returns 200 response code for informing that the shell task was run succeeded

In this approach I have no idea how can I stop the previously started task shell... Is there any way of somethink like take "pid" of runnable process from previous action??? And I do not want to "kill" that cause it might be corrupt the sending data...

3. Well known cron jobs

With this one I'm affraid of overlapping tasks when interval of running cronjobs will be to short... and unused resouces when the iterval will be to long...

役に立ちましたか?

解決

To me, the best way is using a background, long-running task (like a daemon), if you have access to a shell in the server. You can communicate with the task by using some kind of command queue, for example through a in memory table on the database.

If you have everything already written in PHP (or want to use a single language), you can just use php-cli.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top