How to implement the functionality of the alarm in a chat bot?

Hello! Asked a little strange to realize the functionality of the alarm in a chat bot. In General, the user sets a certain time and when that time comes, it comes a particular message. In General now I see it: I take time and remember in the database, then create a script that will run every 5 seconds, it will run all times of all users from the database and if the time matches or equal to the current time+-5sec(+-a couple of seconds at the time of the script), then send a message. Adequate if such a solution looks or is there some more elegant alternative? Never used cron, and plagued by vague doubts that to run a task every 5 seconds is not comme Il faut. Thank you all in advance!
March 19th 20 at 08:29
1 answer
March 19th 20 at 08:31
Solution
The question is. How often will added "alarms"? Why the choice fell on PHP? Do you plan further expansion of the system? If running a script from console without restrictions on run-time?

class Delay {

 private static $alarms = [];

 public static function addInterval($seconds, $userId) {
 self::$alarms[time() + $seconds][$userId] = $seconds;
}

 public static function loop() {
 while (true) {
 $dttm = time();

 if (isset(self::$alarms[$dttm])) {
 foreach (self::$alarms[$dttm] as $userId => $seconds) {
 self::$alarms[$dttm + $seconds][$userId] = $seconds;
 echo "User #{$userId} waited {$seconds} seconds\n ";
}

unset(self::$alarms[$dttm]);
}

sleep(1);
}
}
}

Delay::addInterval(5, 1);
Delay::addInterval(10, 2);
Delay::loop();
I think that not often will be added, as it is planned to use only for their own purposes. But maybe to someone it will be useful in the future. Chose php for the banal reason - because studying and want to get more practice. The extension is planned as part of adding irrelevant features according to the principle "introduced a certain team got a certain record from the database", that is, without automatism, as is the case with notifications. Run time is in principle not limited. - douglas.Johns commented on March 19th 20 at 08:34
@douglas.Johns, Well, then you can. Just asked why, so as to ensure asynchronously PHP poorly adapted and implementation will be more difficult for him. But if timeouts are added not often, and restarting the script each time you add, I will update the answer. Well, then adapt for yourself. - consuelo.Macejkovic commented on March 19th 20 at 08:37
@consuelo.Macejkovic, well, I use Apache, it is because of this asynchrony and provides if I'm not mistaken. In addition to php know python, but could not explain to himself what he's better at solving this problem. Thanks for the code! But I'm still looking for some other solution, without vayl true) - douglas.Johns commented on March 19th 20 at 08:40
@douglas.Johns, Any alternative will work on the same principle using vayl true, but maybe it is not so clearly understandable code. The demon crown works also using while(true). If you need to reach asynchronously, you need one service which will run in a loop and check the condition, and a separate service that will provide CRUD operations on jobs. If I did do it in GoLang using goroutines. And after that you will do them is another question. You can try using a Ratchet - consuelo.Macejkovic commented on March 19th 20 at 08:43
@consuelo.Macejkovic, thank you! Will stop on your variant - douglas.Johns commented on March 19th 20 at 08:46

Find more questions by tags Designing software