Answer the question
In order to leave comments, you need to log in
How to implement the functionality of an alarm clock in a chat bot?
Hello! I set myself a slightly strange goal to implement the functionality of an alarm clock in a chat bot. In general, the user sets a certain time, and when this time comes, he receives a certain message. In general, now I see it like this: I take the user's time and remember it in the database, then I create a script that will run every 5 seconds, it will run all the times of all users from the database and if the time matches or is equal to the current time + -5sec ( + - a couple of seconds for the duration of the script), then we send a message. Does this solution look adequate or is there some more elegant alternative? I have never used cron and I have vague doubts that running a task every 5 seconds is not comme il faut. Thank you all in advance!
Answer the question
In order to leave comments, you need to log in
The question is the following. How often will "alarm clocks" be added? Why choose PHP? Are there plans to further expand the system? Is it possible to run a script from the console with no runtime restrictions?
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} прождал {$seconds} cек\n ";
}
unset(self::$alarms[$dttm]);
}
sleep(1);
}
}
}
Delay::addInterval(5, 1);
Delay::addInterval(10, 2);
Delay::loop();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question