<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($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->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->_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_identity($plugin->config->{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($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_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_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(@<wbr>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">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="h5"><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_5511639465378122148Apple-interchange-newline"></div></div><div><div><div class="h5"><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_5511639465378122148gmail-p1">package Dancer2::Plugin::ZMQ;</p><p class="m_5511639465378122148gmail-p1"><br></p><p class="m_5511639465378122148gmail-p1">use Dancer2::Plugin;</p><p class="m_5511639465378122148gmail-p1"><br></p><p class="m_5511639465378122148gmail-p1">use ZMQ::FFI;</p><p class="m_5511639465378122148gmail-p1">use ZMQ::FFI::Constants qw(ZMQ_REQ);</p><p class="m_5511639465378122148gmail-p1"><br></p><p class="m_5511639465378122148gmail-p1">has host => (is => 'ro');</p><p class="m_5511639465378122148gmail-p1">has identity => (is => 'ro');</p><p class="m_5511639465378122148gmail-p1"><br></p><p class="m_5511639465378122148gmail-p1">has _context => (is => 'ro', lazy_build => 1);</p><p class="m_5511639465378122148gmail-p1">sub _build_context {</p><p class="m_5511639465378122148gmail-p1">    my $plugin = shift;</p><p class="m_5511639465378122148gmail-p1">    return ZMQ::FFI->new();</p><p class="m_5511639465378122148gmail-p1">}</p><p class="m_5511639465378122148gmail-p1"><br></p><p class="m_5511639465378122148gmail-p1">has client => (is => 'ro', lazy_build => 1, plugin_keyword => 'zmq');</p><p class="m_5511639465378122148gmail-p1">sub _build_client {</p><p class="m_5511639465378122148gmail-p1">    my $plugin = shift;</p><p class="m_5511639465378122148gmail-p1"><br></p><p class="m_5511639465378122148gmail-p1">    my $client = $plugin->_context->socket(ZMQ_<wbr>REQ);</p><p class="m_5511639465378122148gmail-p1">    $client->set_identity = $plugin->config->{identity} . "-$$";</p><p class="m_5511639465378122148gmail-p1">    $client->connect($plugin-><wbr>config->{host});</p><p class="m_5511639465378122148gmail-p1">    return $client;</p><p class="m_5511639465378122148gmail-p1">}</p><p class="m_5511639465378122148gmail-p1"><br></p><p class="m_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/<wbr>mailman/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">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>