Pre-init hook for container worker/class component?

16 views
Skip to first unread message

Scott Wittenburg

unread,
Aug 28, 2014, 1:48:26 PM8/28/14
to autob...@googlegroups.com
Hi All,

I have an ApplicationSession container like the following, and I'm wondering if there is any way to run some code before the class is constructed, which would be run with the configured "pythonpath".  I'd like to perform some static configuration on my class before it is actually constructed.  Or perhaps there is a better way of thinking about this?

{
   "controller": {
   },
   "workers": [
      {
         "type": "container",
         "options": {
            "pythonpath": ["/home/scott/projects/ParaView/build-make-debug/lib/site-packages","/home/scott/projects/ParaView/build-make-debug/lib"]
         },
         "components": [
            {
               "type": "class",
               "classname": "paraview.web.pv_web_visualizer._VisualizerServer",
               "realm": "vtkweb",
               "transport": {
                  "type": "websocket",
                  "endpoint": {
                     "type": "tcp",
                     "host": "127.0.0.1",
                     "port": 8080
                  },
                  "url": "ws://127.0.0.1:8080/ws",
                  "debug": true
               }
            }
         ]
      }
   ]
}

Thanks!
Scott

Tobias Oberstein

unread,
Aug 30, 2014, 2:43:18 AM8/30/14
to autob...@googlegroups.com
Hi Scott,

> I have an ApplicationSession container like the following, and I'm
> wondering if there is any way to run some code before the class is
> constructed, which would be run with the configured "pythonpath". I'd

Yes. In fact, Crossbar.io will be happy with anything "callable" instead
of class that takes a ComponentConfig and returns an object that derives
of ApplicationSession:


class _VisualizerServer(ApplicationSession):
...

def makeSession(config):

## tweak class _VisualizerServer here ..

session = _VisualizerServer(config)
return session


and then

"classname": "paraview.web.pv_web_visualizer._makeSession",

Does that do what you want?

Note: because of this option, "classname" is properly illnamed ..

Cheers,
/Tobias

PS: In case you really want to know how it's done, here are the relevant
code pointers for creating sessions running side-by-side in a router:

https://github.com/crossbario/crossbar/blob/master/crossbar/crossbar/worker/router.py#L470

https://github.com/crossbario/crossbar/blob/master/crossbar/crossbar/worker/router.py#L513

and here is similar for containers:

https://github.com/crossbario/crossbar/blob/master/crossbar/crossbar/worker/container.py#L186
https://github.com/crossbario/crossbar/blob/master/crossbar/crossbar/worker/container.py#L243
> --
> You received this message because you are subscribed to the Google
> Groups "Autobahn" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to autobahnws+...@googlegroups.com
> <mailto:autobahnws+...@googlegroups.com>.
> To post to this group, send email to autob...@googlegroups.com
> <mailto:autob...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/autobahnws/af87a47f-de12-43ed-8f6c-c7ae2c1d9410%40googlegroups.com
> <https://groups.google.com/d/msgid/autobahnws/af87a47f-de12-43ed-8f6c-c7ae2c1d9410%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Scott Wittenburg

unread,
Sep 2, 2014, 12:50:24 PM9/2/14
to autob...@googlegroups.com
Hi Tobias,

Responses inline below...


On Saturday, August 30, 2014 12:43:18 AM UTC-6, Tobias Oberstein wrote:
Hi Scott,

 > I have an ApplicationSession container like the following, and I'm
> wondering if there is any way to run some code before the class is
> constructed, which would be run with the configured "pythonpath".  I'd

Yes. In fact, Crossbar.io will be happy with anything "callable" instead
of class that takes a ComponentConfig and returns an object that derives
of ApplicationSession:


class _VisualizerServer(ApplicationSession):
    ...

def makeSession(config):

    ## tweak class _VisualizerServer here ..

    session = _VisualizerServer(config)
    return session


and then

"classname": "paraview.web.pv_web_visualizer._makeSession",

Does that do what you want?

I think this will do what I need, as long as I can add arbitrary json structure to the configuration object that I can make use of in my makeSession function.  I'll check it out and get back to you.
 

Note: because of this option, "classname" is properly illnamed ..

Cheers,
/Tobias

PS: In case you really want to know how it's done, here are the relevant
code pointers for creating sessions running side-by-side in a router:

https://github.com/crossbario/crossbar/blob/master/crossbar/crossbar/worker/router.py#L470

https://github.com/crossbario/crossbar/blob/master/crossbar/crossbar/worker/router.py#L513

and here is similar for containers:

https://github.com/crossbario/crossbar/blob/master/crossbar/crossbar/worker/container.py#L186
https://github.com/crossbario/crossbar/blob/master/crossbar/crossbar/worker/container.py#L243


Thanks for the code pointers, that makes it easier to get right to the relevant source.

Thanks again for all your help Tobias, it is making investigation much smoother :-)

Cheers,
Scott
 

Scott Wittenburg

unread,
Sep 2, 2014, 1:53:59 PM9/2/14
to autob...@googlegroups.com


On Tuesday, September 2, 2014 10:50:24 AM UTC-6, Scott Wittenburg wrote:
Hi Tobias,

Responses inline below...

On Saturday, August 30, 2014 12:43:18 AM UTC-6, Tobias Oberstein wrote:
Hi Scott,

 > I have an ApplicationSession container like the following, and I'm
> wondering if there is any way to run some code before the class is
> constructed, which would be run with the configured "pythonpath".  I'd

Yes. In fact, Crossbar.io will be happy with anything "callable" instead
of class that takes a ComponentConfig and returns an object that derives
of ApplicationSession:


class _VisualizerServer(ApplicationSession):
    ...

def makeSession(config):

    ## tweak class _VisualizerServer here ..

    session = _VisualizerServer(config)
    return session


and then

"classname": "paraview.web.pv_web_visualizer._makeSession",

Does that do what you want?

I think this will do what I need, as long as I can add arbitrary json structure to the configuration object that I can make use of in my makeSession function.  I'll check it out and get back to you.
 
It seems that an "extra" field is allowed inside the "class" type component, and can be some arbitrary json object.  Is this what we should use to pass some extra configuration that we can do what we want with? 

Thanks!
Scott

Tobias Oberstein

unread,
Sep 2, 2014, 2:02:04 PM9/2/14
to autob...@googlegroups.com
Hi Scott,

> I think this will do what I need, as long as I can add arbitrary
> json structure to the configuration object that I can make use of in
> my makeSession function. I'll check it out and get back to you.
>
> It seems that an "extra" field is allowed inside the "class" type
> component, and can be some arbitrary json object. Is this what we
> should use to pass some extra configuration that we can do what we want
> with?

Yes. You can pass arbitrary config (anything JSON) here, and Crossbar
will give it to your components as config.extra.

It's designed exactly for this purpose: pass arbitrary application
specific configuration from Crossbar configs.

This will also work when you define a "makeSession" function ..

To give you an example of how we use it, here is a WAMP component that
is an IRC bot configured like above:

{
"type": "wamplet",
"package": "clandeck",
"entrypoint": "irc",
"realm": "com.tavendo.clandeck",
"extra": {
"server": "tcp:irc.freenode.net:6667",
"nick": "clandeck",
"channels": ["arduino",
"autobahn",
"twisted",
"twisted-dev",
"cryptography-dev",
"websocketpp",
"pypy",
"foobar2323",
"foobar2324"]
},
"transport": {
"type": "rawsocket",
"serializer": "msgpack",
"endpoint": {
"type": "unix",
"path": "clandeck.sock"
}
}
}

Cheers,
/Tobias

Reply all
Reply to author
Forward
0 new messages