You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tinkerpop.apache.org by Stephen Mallette <sp...@gmail.com> on 2015/05/21 18:12:55 UTC
Gremlin Server LifeCycleHook
Dylan Millikin pointed out that Gremlin Server init scripts were firing
each time a new session was created. Technically, that's just what they
were supposed to do, as the init is really for the ScriptEngine and not
just for Gremlin Server and its life cycle. Of course, there are scenarios
where you'd like to see a script that executes once at startup and,
consequently, at shutdown.
To do this, I didn't want to over-complicate the already established yaml
file with more settings so it made me think of a different approach that
hid the details of start/stop scripts from folks who didn't want to deal
with them.
So, I created an interface called LifeCycleHook. It has two methods:
onStartUp and onShutDown. Gremlin Server will call back to this methods on
those life cycle events. To get Gremlin Server to recognize your hook, you
just create an instance in your init scripts:
hook = [
onStartUp: { ctx ->
ctx.logger.info("Loading 'modern' graph data.")
TinkerFactory.generateModern(graph)
}
] as LifeCycleHook
You can see that this hook generates the "modern" graph into the awaiting
and empty TinkerGraph instance. Note that there is no onShutDown method
implemented - both are optional method thanks to groovy magic.
So, this should solve Dylan's problem - new sessions will not re-execute
the hook code. This feature also introduces some interesting ideas. I
remember several individuals talking to me about using Gremlin Server init
scripts to start up processes that executed on some kind of background
thread or schedule but they had no way to shut them down gracefully with
Gremlin Server shutdown. Well - now that capability is there.
hook = [
process: null,
onStartUp: { ctx ->
ctx.logger.info("Loading 'modern' graph data.")
TinkerFactory.generateModern(graph)
process = Thread.start {
// do something cool in a background thread for
// the life of Gremlin Server
}
},
onShutDown: { ctx ->
process.interrupt()
}
] as LifeCycleHook
That's a bit of a rough sketch of how you would want to do something like
that, but it shows that the LifeCycleHook can handle stuff like that if you
really wanted to do it.
Re: Gremlin Server LifeCycleHook
Posted by Dylan Millikin <dy...@gmail.com>.
That's perfect then.
On Tue, May 26, 2015 at 10:21 AM, Stephen Mallette <sp...@gmail.com>
wrote:
> I'ts described here:
>
> http://tinkerpop.incubator.apache.org/docs/3.0.0-SNAPSHOT/#gremlin-server
>
>
>
> On Tue, May 26, 2015 at 10:06 AM, Dylan Millikin <dylan.millikin@gmail.com
> >
> wrote:
>
> > This works great. Just tested.
> > Is this in the docs at this point? If not I could add it if it frees up a
> > bit of your time.
> >
> > On Thu, May 21, 2015 at 12:12 PM, Stephen Mallette <spmallette@gmail.com
> >
> > wrote:
> >
> > > Dylan Millikin pointed out that Gremlin Server init scripts were firing
> > > each time a new session was created. Technically, that's just what
> they
> > > were supposed to do, as the init is really for the ScriptEngine and not
> > > just for Gremlin Server and its life cycle. Of course, there are
> > scenarios
> > > where you'd like to see a script that executes once at startup and,
> > > consequently, at shutdown.
> > >
> > > To do this, I didn't want to over-complicate the already established
> yaml
> > > file with more settings so it made me think of a different approach
> that
> > > hid the details of start/stop scripts from folks who didn't want to
> deal
> > > with them.
> > >
> > > So, I created an interface called LifeCycleHook. It has two methods:
> > > onStartUp and onShutDown. Gremlin Server will call back to this
> methods
> > on
> > > those life cycle events. To get Gremlin Server to recognize your hook,
> > you
> > > just create an instance in your init scripts:
> > >
> > > hook = [
> > > onStartUp: { ctx ->
> > > ctx.logger.info("Loading 'modern' graph data.")
> > > TinkerFactory.generateModern(graph)
> > > }
> > > ] as LifeCycleHook
> > >
> > > You can see that this hook generates the "modern" graph into the
> awaiting
> > > and empty TinkerGraph instance. Note that there is no onShutDown
> method
> > > implemented - both are optional method thanks to groovy magic.
> > >
> > > So, this should solve Dylan's problem - new sessions will not
> re-execute
> > > the hook code. This feature also introduces some interesting ideas. I
> > > remember several individuals talking to me about using Gremlin Server
> > init
> > > scripts to start up processes that executed on some kind of background
> > > thread or schedule but they had no way to shut them down gracefully
> with
> > > Gremlin Server shutdown. Well - now that capability is there.
> > >
> > > hook = [
> > > process: null,
> > > onStartUp: { ctx ->
> > > ctx.logger.info("Loading 'modern' graph data.")
> > > TinkerFactory.generateModern(graph)
> > > process = Thread.start {
> > > // do something cool in a background thread for
> > > // the life of Gremlin Server
> > > }
> > > },
> > > onShutDown: { ctx ->
> > > process.interrupt()
> > > }
> > > ] as LifeCycleHook
> > >
> > > That's a bit of a rough sketch of how you would want to do something
> like
> > > that, but it shows that the LifeCycleHook can handle stuff like that if
> > you
> > > really wanted to do it.
> > >
> >
>
Re: Gremlin Server LifeCycleHook
Posted by Stephen Mallette <sp...@gmail.com>.
I'ts described here:
http://tinkerpop.incubator.apache.org/docs/3.0.0-SNAPSHOT/#gremlin-server
On Tue, May 26, 2015 at 10:06 AM, Dylan Millikin <dy...@gmail.com>
wrote:
> This works great. Just tested.
> Is this in the docs at this point? If not I could add it if it frees up a
> bit of your time.
>
> On Thu, May 21, 2015 at 12:12 PM, Stephen Mallette <sp...@gmail.com>
> wrote:
>
> > Dylan Millikin pointed out that Gremlin Server init scripts were firing
> > each time a new session was created. Technically, that's just what they
> > were supposed to do, as the init is really for the ScriptEngine and not
> > just for Gremlin Server and its life cycle. Of course, there are
> scenarios
> > where you'd like to see a script that executes once at startup and,
> > consequently, at shutdown.
> >
> > To do this, I didn't want to over-complicate the already established yaml
> > file with more settings so it made me think of a different approach that
> > hid the details of start/stop scripts from folks who didn't want to deal
> > with them.
> >
> > So, I created an interface called LifeCycleHook. It has two methods:
> > onStartUp and onShutDown. Gremlin Server will call back to this methods
> on
> > those life cycle events. To get Gremlin Server to recognize your hook,
> you
> > just create an instance in your init scripts:
> >
> > hook = [
> > onStartUp: { ctx ->
> > ctx.logger.info("Loading 'modern' graph data.")
> > TinkerFactory.generateModern(graph)
> > }
> > ] as LifeCycleHook
> >
> > You can see that this hook generates the "modern" graph into the awaiting
> > and empty TinkerGraph instance. Note that there is no onShutDown method
> > implemented - both are optional method thanks to groovy magic.
> >
> > So, this should solve Dylan's problem - new sessions will not re-execute
> > the hook code. This feature also introduces some interesting ideas. I
> > remember several individuals talking to me about using Gremlin Server
> init
> > scripts to start up processes that executed on some kind of background
> > thread or schedule but they had no way to shut them down gracefully with
> > Gremlin Server shutdown. Well - now that capability is there.
> >
> > hook = [
> > process: null,
> > onStartUp: { ctx ->
> > ctx.logger.info("Loading 'modern' graph data.")
> > TinkerFactory.generateModern(graph)
> > process = Thread.start {
> > // do something cool in a background thread for
> > // the life of Gremlin Server
> > }
> > },
> > onShutDown: { ctx ->
> > process.interrupt()
> > }
> > ] as LifeCycleHook
> >
> > That's a bit of a rough sketch of how you would want to do something like
> > that, but it shows that the LifeCycleHook can handle stuff like that if
> you
> > really wanted to do it.
> >
>
Re: Gremlin Server LifeCycleHook
Posted by Dylan Millikin <dy...@gmail.com>.
This works great. Just tested.
Is this in the docs at this point? If not I could add it if it frees up a
bit of your time.
On Thu, May 21, 2015 at 12:12 PM, Stephen Mallette <sp...@gmail.com>
wrote:
> Dylan Millikin pointed out that Gremlin Server init scripts were firing
> each time a new session was created. Technically, that's just what they
> were supposed to do, as the init is really for the ScriptEngine and not
> just for Gremlin Server and its life cycle. Of course, there are scenarios
> where you'd like to see a script that executes once at startup and,
> consequently, at shutdown.
>
> To do this, I didn't want to over-complicate the already established yaml
> file with more settings so it made me think of a different approach that
> hid the details of start/stop scripts from folks who didn't want to deal
> with them.
>
> So, I created an interface called LifeCycleHook. It has two methods:
> onStartUp and onShutDown. Gremlin Server will call back to this methods on
> those life cycle events. To get Gremlin Server to recognize your hook, you
> just create an instance in your init scripts:
>
> hook = [
> onStartUp: { ctx ->
> ctx.logger.info("Loading 'modern' graph data.")
> TinkerFactory.generateModern(graph)
> }
> ] as LifeCycleHook
>
> You can see that this hook generates the "modern" graph into the awaiting
> and empty TinkerGraph instance. Note that there is no onShutDown method
> implemented - both are optional method thanks to groovy magic.
>
> So, this should solve Dylan's problem - new sessions will not re-execute
> the hook code. This feature also introduces some interesting ideas. I
> remember several individuals talking to me about using Gremlin Server init
> scripts to start up processes that executed on some kind of background
> thread or schedule but they had no way to shut them down gracefully with
> Gremlin Server shutdown. Well - now that capability is there.
>
> hook = [
> process: null,
> onStartUp: { ctx ->
> ctx.logger.info("Loading 'modern' graph data.")
> TinkerFactory.generateModern(graph)
> process = Thread.start {
> // do something cool in a background thread for
> // the life of Gremlin Server
> }
> },
> onShutDown: { ctx ->
> process.interrupt()
> }
> ] as LifeCycleHook
>
> That's a bit of a rough sketch of how you would want to do something like
> that, but it shows that the LifeCycleHook can handle stuff like that if you
> really wanted to do it.
>