I have an issue that I have no idea how to approach... I have two processes running - one of them listens to messages, sorts them, and converts them, and another process runs other management things and sends out regular messages plus converted ones... How do I unify all of this into one script?
class Sender {
private $messageQueue;
public function run() {
// open stream
while (true) {
$this->send($this->messageQueue->pop());
if ($this->lastMessageTimestamp + 20 < time) {
$this->keepalive();
}
}
// close stream
}
public function pushMessage(Message $message) {
$this->messageQueue->push($message);
}
private function send(Message $message) {
// send through stream
$this->lastMessageTimestamp = time();
}
private function keepalive() {
$this->messageQueue->push(new KeepAliveMessage());
}
}
The gist is that the sender should open a connection and keep it alive, sending alive messages each 20 (plus extra unmentioned things), and pops other messages that were pushed to it from outside via pushMessage.
class Listener {
public function run() {
while(true) {
$message = $this->listen();
$this->messages->push($message);
$this->processMessages();
}
}
private function processMessages() {
// processing logic, sorting, comparing, analyzing, transforming
$this->echo(Message $processedMessage);
}
public function echo(Message $processedMessage) {
return $processedMessage;
}
}
While the listener also does its job listening (maybe reading from file) for other kind of messages, then processes them and forms a correct message for the sender to send.
My issue is combining all of this into one functionality where I could run both of those non-blocking each other (forks? callbacks?) where I could pass the processed message from Listener to Sender to send, while not blocking whatever else Listener and Sender are doing while running....
Any help (pseudo code is good) would be appreciated...
(All of this is running off a Laravel command, but that's not really relevant)
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire