<div dir="ltr">Hi Clive,<div><br></div><div>Your "my variable" should be equals to my "has _client".</div><div>Anyway, I found the issue. I was using Plack::Handler::Starman to start the app:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">my $server = new Plack::Handler::Starman(listen => ['0.0.0.0:5000'], proctitle => 0);</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">$server->run(MyWeb::API->to_app);</blockquote></blockquote><div>Doing it that way makes starman to preloads the app in the master process and does copy on write to the workers. That makes it use the same context for all the workers.</div><div><br></div><div>What I did is to use the same mechanism as when you run dancer2 gen -a MyWeb::App:</div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">use MyWeb::App;<br>MyWeb::App->to_app;</blockquote></div><div> </div><div>Then I can run it from cli using the starman command. This way it doesn't preload the app unless --preload-app option is used.</div><div>And now it's working.</div><div><br></div><div>Thank you very much for your time.</div><div><br></div><div>Regards,</div><div>Alfonso</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 13, 2017 at 8:11 PM, Clive Eisen <span dir="ltr"><<a href="mailto:clive@hildebrand.co.uk" target="_blank">clive@hildebrand.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">Do it the way I did it with a my variable outside to hold your handler. Then you will get one per child (well i do anyway)<br><br><div id="m_2075210416171661916AppleMailSignature">--<div>Clive</div></div><div><div class="h5"><div><br>On 13 Oct 2017, at 19:07, Alfonso Pinto <<a href="mailto:alfonso.pinto@gmail.com" target="_blank">alfonso.pinto@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Hi Clive,<div><br></div><div>Thanks for the answer. I found 2 things:</div><div>- <span style="font-size:12.8px"> </span><span style="font-size:12.8px">$client->set_identity = $plugin->config->{identity} . "-$$"; should be </span><span style="font-size:12.8px"> </span><span style="font-size:12.8px">$client->set_identity($<wbr>plugin->config->{identity} . "-$$");</span></div><div><span style="font-size:12.8px">- Using builders for the properties doesn't work, I've to use BUILD as you suggested.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Now I've this:</span></div><div><span style="font-size:12.8px"><br></span></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">package Dancer2::Plugin::ZMQ;</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"><br></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">use Dancer2::Plugin;</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"><br></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">our $version = '0.01';</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"><br></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">use ZMQ::FFI;</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">use ZMQ::FFI::Constants qw(ZMQ_REQ);</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"><br></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">has _context => (is => 'rw');</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">has _client => (is => 'rw', plugin_keyword => 'zmq');</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"><br></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">sub BUILD {</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"> my $plugin = shift;</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"> my $args = shift;</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"><br></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"> $plugin->_context(ZMQ::FFI-><wbr>new());</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"> $plugin->_client($plugin->_<wbr>context->socket(ZMQ_REQ));</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"> $plugin->_client->set_<wbr>identity($plugin->config->{<wbr>identity} . "-$$");</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"> $plugin->_client->connect($<wbr>plugin->config->{host});</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">}</span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px"><br></span></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">1;</span></blockquote></div><div><span style="font-size:12.8px"><br></span></div><div><div style="font-size:12.8px"><br></div></div><div style="font-size:12.8px">This works if you run the dancer app as a standalone process. If I run it with Starman that initialises several workers, it doesn't work as it tries to share the same instance between all the workers (you can't share ZMQ context between processes/threads).</div><div style="font-size:12.8px">Is there any way to create one instance per process?</div><div><br></div><div>Regards,</div><div>Alfonso.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 13, 2017 at 4:32 PM, Clive Eisen <span dir="ltr"><<a href="mailto:clive@hildebrand.co.uk" target="_blank">clive@hildebrand.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I do a very similar thing for a custom redis plugin I wrote<div><br></div><div>You have to declare the object outside the BUILD and set it inside</div><div><br></div><div>eg</div><div>package Dredis;</div><div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">use Redis::Fast;</span></div></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">use Dancer2::Plugin;</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">my $redii;</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">sub BUILD {</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"> my ($plugin) = @_;</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><span class="m_2075210416171661916m_5511639465378122148Apple-tab-span" style="white-space:pre-wrap"> </span><snip lots of setup stuff></span></div><div><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><span class="m_2075210416171661916m_5511639465378122148Apple-tab-span" style="white-space:pre-wrap"> </span>$redii->{$r}{$i} = Redis::Fast->new( %opts );</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">Then use $redi-> in the get/set methods</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">eg</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><font face="Monaco" style="font-size:10px">s</font>ub dredis {</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> my( $called, $plugin, $method, @args ) = @_;</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> </span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"> return $redii->{$called}->$method(@ar<wbr>gs);</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">1;</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></span></div></span></div><div><div>
--<br>Clive Eisen<br>GPG: 75056DD0<br><br><br><br><br><br>
</div>
<br><div><blockquote type="cite"><div><div class="m_2075210416171661916h5"><div>On 13 Oct 2017, at 14:29, Alfonso Pinto <<a href="mailto:alfonso.pinto@gmail.com" target="_blank">alfonso.pinto@gmail.com</a>> wrote:</div><br class="m_2075210416171661916m_5511639465378122148Apple-interchange-newline"></div></div><div><div><div class="m_2075210416171661916h5"><div dir="ltr">Dear all,<div><br></div><div>I'm trying to write a Dancer2::Plugin to maintain ZMQ socket and send some information to a backend when a request is received.</div><div><br></div><div>This is the plugin :</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><p class="m_2075210416171661916m_5511639465378122148gmail-p1">package Dancer2::Plugin::ZMQ;</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"><br></p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">use Dancer2::Plugin;</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"><br></p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">use ZMQ::FFI;</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">use ZMQ::FFI::Constants qw(ZMQ_REQ);</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"><br></p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">has host => (is => 'ro');</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">has identity => (is => 'ro');</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"><br></p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">has _context => (is => 'ro', lazy_build => 1);</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">sub _build_context {</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"> my $plugin = shift;</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"> return ZMQ::FFI->new();</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">}</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"><br></p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">has client => (is => 'ro', lazy_build => 1, plugin_keyword => 'zmq');</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">sub _build_client {</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"> my $plugin = shift;</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"><br></p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"> my $client = $plugin->_context->socket(ZMQ_<wbr>REQ);</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"> $client->set_identity = $plugin->config->{identity} . "-$$";</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"> $client->connect($plugin->conf<wbr>ig->{host});</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"> return $client;</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">}</p><p class="m_2075210416171661916m_5511639465378122148gmail-p1"><br></p><p class="m_2075210416171661916m_5511639465378122148gmail-p1">1;</p></blockquote><div> </div><div> This is my config.yml:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">plugins:<br> ZMQ:<br> identity: "api-gw"<br> host: "<a>tcp://proxy:6660</a>"</blockquote><div><br></div><div><br></div><div>And this is how I use it in one route:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">post '/' => sub {</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> my $message = body_parameters->get('message'<wbr>);</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> zmq->send(message =>$message);</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> my $reply = zmq->recv();</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> return {"reply" => $reply};</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">};</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><div><br></div><div>The issue is that I get this error:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Router exception: Can't call method "send" on an undefined </blockquote><div> <br></div><div>I don't understand why it's not keeping the client instantiated. What is wrong withmy approach? What I don't want is to create a socket for each request received.</div><div><br></div><div>And before someone asks, the backend is working and replying. A simple script like this one works in my setup:</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">use v5.10;<br>use ZMQ::FFI qw(ZMQ_REQ);<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">my $endpoint = "<a>tcp://proxy:6660</a>";<br>my $ctx = ZMQ::FFI->new();<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">my $s1 = $ctx->socket(ZMQ_REQ);<br>$s1->connect($endpoint);<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">$s1->send('ohhai');<br>print $s1->recv();</blockquote><div><br></div><div>Thanks in advance.</div><div>Regards,</div><div>Alfonso </div></div></div></div></div>
______________________________<wbr>_________________<br>dancer-users mailing list<br><a href="mailto:dancer-users@dancer.pm" target="_blank">dancer-users@dancer.pm</a><br><a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" target="_blank">http://lists.preshweb.co.uk/ma<wbr>ilman/listinfo/dancer-users</a><br></div></blockquote></div><br></div></div></div><br>______________________________<wbr>_________________<br>
dancer-users mailing list<br>
<a href="mailto:dancer-users@dancer.pm" target="_blank">dancer-users@dancer.pm</a><br>
<a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" rel="noreferrer" target="_blank">http://lists.preshweb.co.uk/ma<wbr>ilman/listinfo/dancer-users</a><br>
<br></blockquote></div><br></div>
</div></blockquote><blockquote type="cite"><div><span>______________________________<wbr>_________________</span><br><span>dancer-users mailing list</span><br><span><a href="mailto:dancer-users@dancer.pm" target="_blank">dancer-users@dancer.pm</a></span><br><span><a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" target="_blank">http://lists.preshweb.co.uk/<wbr>mailman/listinfo/dancer-users</a></span><br></div></blockquote></div></div></div><br>______________________________<wbr>_________________<br>
dancer-users mailing list<br>
<a href="mailto:dancer-users@dancer.pm">dancer-users@dancer.pm</a><br>
<a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" rel="noreferrer" target="_blank">http://lists.preshweb.co.uk/<wbr>mailman/listinfo/dancer-users</a><br>
<br></blockquote></div><br></div>