Post-startup hook after user's session is spawned

I’d like to run some operations in user’s session context (with all the env vars set up and so on) just as soon as their session is created. This has to do with some LDAP group checks and kernel custom modifications, anyway - is there any hook this can be done? If so, where is a proper place to call such a code?

It depends on how your JupyterHub is setup. For example, if you’re using Kubernetes you can add a lifecycle hook as in this example:
https://zero-to-jupyterhub.readthedocs.io/en/latest/jupyterhub/customizing/user-environment.html#about-user-storage-and-adding-files-to-it

If you give us more information about your deployment we can probably come up with some ideas.

No, we’re not using Kubernetes. JupyterHub user session is spawner based on a custom Spawner called from jupyterhub_config.py (JH node’s OS: Red Hat 7.8)

Any ideas how to handle this in non-k8s environment?

You could use a custom command, e.g. a shell script that does any setup you need, before executing jupyterhub-singleuser "$@":

@manics Thanks for the hint. But would be already in the user context? I mean - would I have at least user ID in the environment variables at that stage? The main reason I need this user id to be already in the vars is that I need to check in LDAP groups which kernels I should show or hide for the user.

It should do- the singleuser server is run inside the user’s environment. If you run env in a JupyterLab/notebook terminal most if not all of those variables should be available to your startup script.

There is a Spawner.pre_spawn_hook, but there is no Spawner.post_spawn_hook that for example would trigger when the user server has become marked as ready after spawn, and the user being redirected to it.

Technically, would Spawner.progress_ready_hook work here?

progress_ready would be risky because it would only run if something is subscribed to the progress events at launch time. It could also run potentially several times.

I think we are due for a more comprehensive and systematic approach to hooks. They are a bit haphazard and not super consistent at the moment.