I have following scenario. When any complaint opened for job isn't completed within 48 hours and if that complaint is nearing 48 hours without completion, popup or notification should be there in backend before the time limit(say 1 hour before). I am thinking of doing cron job(run every hour).My problem is how to do the notification in cron job. Any suggestion will be welcome.
-
1How about adding the cron job code to your question – Machavity Aug 09 '16 at 12:25
-
what do you mean by "notification"? do you mean send them an email? Show something on a page? – Funk Forty Niner Aug 09 '16 at 12:26
-
No, I have already done email and sms. In this case I have to show popup in backend with sound and info of that job. – samjhana joshi Aug 09 '16 at 12:27
-
By cron do you mean push notifications? – vishwakarma09 Aug 09 '16 at 12:28
-
Where would the `popup` display? The backened usually doesn't have a UI.. – chris85 Aug 09 '16 at 12:29
-
popup will display at the backend(I mean cms of the project). – samjhana joshi Aug 09 '16 at 12:30
-
a user would need to be logged in, in order to know of that notification. I can't see how a cron job would work without one. Are you using a database? If so, then you would need to write to it (update) and for that user/complaint ticket in particular. That's the best I can come up with. – Funk Forty Niner Aug 09 '16 at 12:33
-
@Fred-ii- yes, of course the user will be logged into cms. When logged into cms and if there is some uncompleted complaint nearing its time limit popup/notification with complaint detail should be displayed. – samjhana joshi Aug 09 '16 at 12:35
-
then you can use a datetime stamp for the user and check if it is within a set date/time limit. That would work. all this done with conditional statements. The cron would include code that would query the db and check if it is within 48 hours. If so, `{ // do something }`. There's your answer ;-) The (ball) coding is now in your court, as it were. – Funk Forty Niner Aug 09 '16 at 12:38
-
May be you're looking for Server-sent events. https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events – Alok Patel Aug 09 '16 at 12:39
-
A (mysql) trigger or an event would also work, if that is what your API is. Links http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html --- https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html in place of a cronjob. – Funk Forty Niner Aug 09 '16 at 12:42
-
comments given and an answer below now. what would you like to do with the question at this point? has any of my comments help to solve this? Since an answer has now been given, the question needs to be closed somehow, so the ball is again, in your court @samjhanajoshi otherwise, I will have to vote as "too broad". I for one can't go writing code for you. I've suggested logic/functions. However those will only work if a user is logged in. You would probably be best to email them also. Otherwise, your method stands to fail. – Funk Forty Niner Aug 09 '16 at 12:49
-
@Fred-ii- Thanks for the info about mysql triggers/schedules. Didn't know that was even an option. – BeetleJuice Aug 09 '16 at 12:53
-
@BeetleJuice Anytime ;-) However, and (my) comments given so far have yet to be answered as to what they'd like to do with the question. Personally, I think the user should also be emailed, because a front-end method would fail and I've already said it a few times as to "why" it would ;-) I think you know what that is *lol* well, I shall now move on now. – Funk Forty Niner Aug 09 '16 at 12:57
1 Answers
The cron job should not be connecting to the pages; it should be the reverse: use the backend pages themselves to check for job completion. You can use Server-Sent Events (SSE) for this. The process would be:
1 Backend pages (Javascript) subscribe to an SSE channel and show a popup when the PHP script emits an event
var source = new EventSource("job_checker.php");
source.onmessage = function(event) {
//event contains the job info and deadline. you can display it
var jobDetails = JSON.parse(event);
...
};
2 The PHP job_checker.php can run through a loop for a set amount of time, checking for job completion:
<?php
$time_limit = 300; //300 seconds
$time_spent = 0;
while($time_spent < $time_limit){
sleep(30); //sleep for 30 seconds
$time_spent += 30;
//check DB for job completion and deadline.
//if you find a job, emit an SSE to the browser
if($event_found){
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo "data: ". json_encode($event_found)."\n\n";
flush();
}
}
The PHP script will keep working for 300 seconds. If it finds an event, it will emit data and the browser will get it. After 300 seconds, the script will die. If the user still has the admin page open, the browser will automatically reconnect. If the user has closed the page, the resources will be freed.
More docs from Mozilla: (thanks to Alok Patel for the link)

- 1
- 1

- 39,516
- 19
- 105
- 165
-
A possible problem with this method, is that a user would need to be logged in, in order to see the notification. If and when it does goes past their 48 hour limit and they have some type of procedure that deletes something, then guess what? Someone stands at not being a very happy camper ;-) – Funk Forty Niner Aug 09 '16 at 12:53
-
@Fred-ii- You're right that this alone won't catch all expiring deadlines. If I read the OP correctly, (s)he already emails and sms expiring jobs but needs a UI popup while admins are browsing the back pages. For that, this would do unless I'm missing something. – BeetleJuice Aug 09 '16 at 12:56
-
@BeetleJuice .. Thank you for the suggestion. I think it will work(using SSE) but the problem here is client used IE :( – samjhana joshi Aug 10 '16 at 04:21
-
@samjhanajoshi you can use AJAX polling then to have the front page ask the PHP script every 60 seconds whether there is a new alert to display. Or long polling/COMET where the front page asks the server, but the server doesn't respond until there is news. Search *"ajax polling"* or *"ajax long polling"* – BeetleJuice Aug 10 '16 at 04:29