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.

Reply via email to