A
A
Alixx2019-03-21 22:10:35
Message Queues
Alixx, 2019-03-21 22:10:35

How php (particularly on Laravel) sites implement events when the desired date / time occurs?

Hello!
It was necessary now to implement the execution of many different tasks when a certain date/time occurs (each task has this date/time specified) on the site.
1 type of events: Users can turn on some events, i.e. they press the button, the server performs some actions, calculations, updates the data in the database (postgresql) by putting in them the start date / time and the calculated end date / time for this event and this user.
Event type 2: There are already prepared events that are called at a certain time, for example. every Tuesday at 15:15 give all users such and such a message, or update certain data in the database, or something else.
You need to: keep track of whether the date/time of these events has come up; when approached - call the method corresponding to the event, update the data in the database, if necessary - issue notifications to one or more users or launch some other event that is also tracked.
That is, as I see it so far, every second (or a minute, until the task duration is a multiple of a second or a minute has been decided), you need to check the entire list of available tasks and compare their date / time with the current time on the server. If it matches, then complete this task.
How is this implemented on php sites, in particular on Laravel?
I was advised to queue with redis, I read it, but it seems that they do not allow you to complete tasks on time, but simply perform them one after another. Or is this not the case and you can adjust them in time? And if not, what then is appropriate for solving this case? There are more events in the lara, maybe this is what you need? Tell me please. this is the first time I've encountered this.
I use Laravel 5.4, postgresql 11.1, xampp - php 7.2.0, apache 2.4.29, windows 10, not in production, I do it locally.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
Evgeny Romashkan, 2019-03-21
@alixx

Easy )
Cron commands start

A
Anya Smirnova, 2019-03-22
@MrsDA

I can’t say for sure that I understood the essence of the question, but if we are talking about events at a certain time, then you can use this option:
Instead of checking the exact date and time
( " if ($date == ... )" )
, you need to use it so that the date is checked, as it were, "over" the desired value
That is, given, for example, the task completion date is September 15 at 6:00.
In MySQL, we write this DATETIME together, or in a separate cell, a year, a separate hour, and so on
. As soon as the User enters the site, we check not the date with the date, but if the month is greater than the specified month, if the year is greater than the specified year, and so on:

// Получаем текущую дату
$Y = date("Y");
$m = date("m");
$d = date("d");

// Из запроса MySQL Select берём дату мероприятия
$event_year = $myrow['year'];
...

if ($event_year < $Y || $event_month < $m || $event_day < $d) {
echo 'Событие "Капитаны" уже проводится/проводилось';
}

If you wrote the whole date in MySQL, then everything is the same, just cut it off
$event_date = $myrow['date'];
            
$event_year = substr($event_date, 0, 4);
$event_month = substr($event_date, 5, 2);
$event_day = substr($event_date, 8, 2);

This does NOT work:
If you need to send a notification about the start of an event to Email and the like.
When the User enters the site, then there is a check, but the event appears.
With a little digging, you can do the same for the end of the event

K
Konstantin, 2019-03-22
@potkot

From the comments I realized that you do not have a Linux server. If so, then windows has a task scheduler (both in the server version and in the normal one). This is the same as cron in fact. Runs tasks at a certain time with a certain periodicity.

B
bro-dev, 2019-03-23
@xPomaHx

If the question is "how?" theoretical, then WordPress, for example, has its own cron, and for each request it checks whether something needs to be launched, that is, if you have a task for the night, and a current person enters the main page in the morning, then the task will start in the morning.
This is not difficult to implement on the chest if something ready has not already been done.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question