Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel queue:work and $mqqt->disconnect() #18

Closed
stijn1989 opened this issue Jan 8, 2022 · 3 comments
Closed

Laravel queue:work and $mqqt->disconnect() #18

stijn1989 opened this issue Jan 8, 2022 · 3 comments

Comments

@stijn1989
Copy link

This is a huge tip for the users of this library. It can safe some frustrated hours.

I have an API. When an endpoint is called, it starts a job on the queue. In this job, a MQTT message is published. The code below is the code that runs in the job.

$topic = sprintf("Vennens/MVA/%d/Silo/%d/Alternatieven", $this->plc, $this->silo);
$mqtt = MQTT::connection();
$mqtt->publish($topic, json_encode([
    'alternatief_1' => $this->alternatief_1,
    'alternatief_2' => $this->alternatief_2,
    'alternatief_3' => $this->alternatief_3,
    'alternatief_4' => $this->alternatief_4,
]), 2);

$mqtt->loop(true, true, 5);
$mqtt->disconnect();

The first job process goes great. But when I do a second request. The job failes. You get an exception that there is no connection.

image

The problem is that the Laravel queue worker keeps it's app state. In the MQTT class, it stores the connections in an array. When you call $mqtt->disconnect(). The connection is closed, but remains in the connections array.
When trying to reopen the connection on the second process, it fails. The socket is not open.

You have to call MQTT::disconnect(). This closes the socket and removes connection from the array.

$topic = sprintf("Vennens/MVA/%d/Silo/%d/Alternatieven", $this->plc, $this->silo);
$mqtt = MQTT::connection();
$mqtt->publish($topic, json_encode([
    'alternatief_1' => $this->alternatief_1,
    'alternatief_2' => $this->alternatief_2,
    'alternatief_3' => $this->alternatief_3,
    'alternatief_4' => $this->alternatief_4,
]), 2);

$mqtt->loop(true, true, 5);
MQTT::disconnect();

image

@Namoshek
Copy link
Collaborator

Namoshek commented Jan 8, 2022

Thank you for reporting this! I should be able to change the behavior of MQTT::connection() to reconnect, if disconnected previously.

In your case, it might be better to not disconnect at all and use an increased timeout to keep the connection alive, at least if you publish messages frequently (at most every 5 minutes). Connecting to the broker is a relatively expensive and time consuming task.

@stijn1989
Copy link
Author

stijn1989 commented Jan 8, 2022

I should be able to change the behavior of MQTT::connection() to reconnect, if disconnected previously.

That's also a solution for this problem. Great library, love it.

Connecting to the broker is a relatively expensive and time consuming task.

I'm using a middleware which checks if the MQTT is available (on certain routes) with connect/disconnect.
The job is executed on user requests. So it's not scheduled.

The backend is part of a system. The MQTT is just there to communicate with a PLC system. In my example, the user has changed some silo's and the PLC needs to know which ones.

@Namoshek
Copy link
Collaborator

Namoshek commented Jan 8, 2022

Sounds reasonable. The bug was fixed in #19 and released as v1.0.1.

@Namoshek Namoshek closed this as completed Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants