Page 1 of 1

Fork a Script Session?

PostPosted: Wed Aug 10, 2016 9:51 pm
by lando.wark
I've created a python script that passes selected images to a queue where they are processed by a program on the server. Since the processing takes longer than a user would likely be logged in I have the script exit once all the images are queued.

I'd like to have the resulting image uploaded to the same dataset as the raw image, however since the script has exited I can't use that session to provide user credentials for the upload.

Does anyone know of anyway to fork the script session and pass its key to the processing program to hold so it can use this to upload the resulting image?

Thanks in advance.

Re: Fork a Script Session?

PostPosted: Thu Aug 11, 2016 5:16 am
by atarkowska
Hi,

Here is Python example how to join an existing session https://github.com/openmicroscopy/openm ... ion.py#L91

If you show us your script we could advice more

Ola

Re: Fork a Script Session?

PostPosted: Fri Aug 12, 2016 1:52 am
by lando.wark
The code of my script is below. I'm not sure I understand the example. Which module is it located in?

Code: Select all
import omero, omero.scripts as scripts
import omero.util.script_utils as scriptUtil
from omero.gateway import BlitzGateway
from omero.rtypes import rlong, rstring
import subprocess

if __name__ == "__main__":
  # Define the script name & description, and a single 'required' parameter
  dataTypes = [rstring('Image')]
  client = scripts.client(
     'Batch_Deconvolution.py', """Deconvolves images by leveraging ImageJ Cookbook plugin in a video-free buffer.""",
     scripts.String(
         "Data_Type", optional=False, grouping="1",
         description="Choose source of images (only Image supported)",
         values=dataTypes, default="Image"),
     scripts.List(
         "IDs", optional=False, grouping="2",
         description="List of Image IDs to process.").ofType(rlong(0)),
     version="0.1",
     authors=["Landon Wark"],
     institutions=["Harbinger Analytics"],
     contact="lando.wark@gmail.com",
      )
  try:
    # Use the Python Blitz Gateway for convenience
    conn = BlitzGateway(client_obj=client)
    user_name = conn.getUser().getName()
    sessionKey = str(client.getSession())
    sessionKey = sessionKey[sessionKey.index('-')+1:sessionKey.index('/')]
    scriptParams = client.getInputs(unwrap=True)
    images, logMessage = scriptUtil.getObjects(conn, scriptParams)
    for img in images:
      img_path = "/" + img.getImportedImageFilePaths()['client_paths'][0]
      datasetId = str(img.getParent().getId())
      #pass information to deconvolution bash script
      subprocess.call("bash /home/landon/Documents/Scripts/deconchek.sh " + img_path + " " + datasetId + " " + sessionKey, shell=True)
    client.setOutput("Message", rstring("Selected images have been added to the deconvolution queue. You should receive an email when decon is complete."))
  finally:
    pass
    # Cleanup
    #client.closeSession()

Re: Fork a Script Session?

PostPosted: Fri Aug 12, 2016 9:32 am
by wmoore
Hi,

I'm afraid that only Admin users can createSession().
We are currently planning on how to extend various roles, so other users can do some admin tasks
https://trello.com/c/t0nT7KYa/133-new-role but this is not available yet.

So your only option is either to try and keep the existing session alive OR to store credentials in some safe location.

- A similar use-case to yours is discussed at viewtopic.php?f=6&t=8072&p=17204. If you log out of webclient the session is killed (https://trello.com/c/yc8YNhUa/117-omero ... -on-logout). But, if you can simply avoid logging out and don't kill the session in the script itself then you may be OK. You would also need to keep this session alive for as long as it's needed. You can set a timeout..... In looking this up just now I also see that there's a createUserSession(), so I need to do a bit of investigation and get back to you:
http://downloads.openmicroscopy.org/ome ... serSession

- Alternatively, if you are happy to store an Admin's credentials somewhere safe that your processing can access then you can login and create a session as another user when needed.

I will investigate further...

Will.

Re: Fork a Script Session?

PostPosted: Fri Aug 12, 2016 11:18 am
by wmoore
Hi,

It turns out that you need to have your password in hand for a user to createUserSession().
You could request that users enter their password when they run the script (if this isn't too much of an issue).
I need to check whether there's any logging of script params or other problems with this (will get back to you).

E.g.

Code: Select all
groupName = conn.getEventContext().groupName   # or you could look-up group that data is in
timeToLive = 0
timeToIdle = 6000000    # max value - 100 mins
conn.c.sf.setSecurityPassword(PASSWORD)
newSession = conn.getSessionService().createUserSession(timeToLive, timeToIdle, groupName)
newUuid = newSession.uuid.val

# Later, reuse the uuid to join session into the correct group

connection = BlitzGateway('OMERO.script')
connection.connect(sUuid=newUuid)


From the docs (assuming these parameters behave the same in all methods they are used)
timeToLiveMilliseconds:
The time that this model::Session has until destruction. Setting the value to 0 will prevent destruction unless the session remains idle.
timeToIdleMilliseconds:
The time that this model::Session can remain idle before being destroyed. Setting the value to 0 will prevent idleness based destruction (NB: this failed when I tried it)

See http://downloads.openmicroscopy.org/ome ... ssion.html

Let us know if that's an option, and I'll look into logging of parameters etc.

Will.

Re: Fork a Script Session?

PostPosted: Fri Aug 12, 2016 1:35 pm
by wmoore
So, it seems that the script parameters are logged in a couple of places.
E.g. with a Password field, after running script and entering "secret2":

Code: Select all
$ grep secret2 -r dist/var/log/
dist/var/log//Blitz-0.log:2016-08-12 14:29:46,206 INFO  [        ome.services.util.ServiceHandler] (.Server-20)  Rslt:   {IDs=(16783), Data_Type=Image, Password=secret2}
dist/var/log//Blitz-0.log:2016-08-12 14:29:46,765 INFO  [        ome.services.util.ServiceHandler] (.Server-18)  Rslt:   {IDs=(16783), Data_Type=Image, Password=secret2}
dist/var/log//OMEROweb.log:2016-08-12 14:29:46,079 DEBUG [                omeroweb.webclient.views] (proc.84462) script_run():4383 Script: run with request.POST: <QueryDict: {u'csrfmiddlewaretoken': [u'eNoVq528bOqlhQqbCzKuviODTRX3PUO2'], u'Password': [u'secret2'], u'IDs': [u'16783'], u'Data_Type': [u'Image']}>
dist/var/log//OMEROweb.log:    _val = secret2


So if you do want to try this option you'd need to disable that logging first.
Let us know your thoughts.

Regards,

Will