Scratch Workspace cleanup in SOE service

950
5
06-14-2013 05:20 AM
SimonLeo
New Contributor III
Hi,

I have a SOE service that does some data processing work. Each time when a new session is created, the service will create a Scratch FileGDB workspace using FileGDBScratchWorkspaceFactory. From Esri document, it says
Second, when the last reference to the workspace is released, the workspace is automatically deleted from the disk.
http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//001m00000039000000

But I noticed in temp folder where these GDB files locate, the files are still there including the ones created days ago. Basically it is not deleted automatically.

I am wondering in SOE, where should I do the clean up work? here are the codes:

public void Construct(IPropertySet props) //this is the function in SOE main file that reads properties
        {
            configProps = props;
            //load properties
           //....

            //Create Scratch Workspace  
           Type factoryType = Type.GetTypeFromProgID(
                "esriDataSourcesGDB.FileGDBScratchWorkspaceFactory");
            IScratchWorkspaceFactory2 scratchWorkspaceFactory = (IScratchWorkspaceFactory2)
                Activator.CreateInstance(factoryType);

            //Get default workspace.
            IWorkspace scratchWorkspace = scratchWorkspaceFactory.DefaultScratchWorkspace;

            if (scratchWorkspace == null) //create a new one
            {
                scratchWorkspace = scratchWorkspaceFactory.CreateNewScratchWorkspace();
            }
        }



What's a good way to clean up scratch workspace? is following method ok?

1. Create a CleanUpScratchWorkspace function: to loop up scratch workspace and delete all datasets within.
2. Put this function inside SOE main file's ShutDown()

I'd like to hear about others' suggestions and appreciate any inputs.

thanks!
Tags (2)
0 Kudos
5 Replies
nicogis
MVP Frequent Contributor
I haven't tried your method (perahps you have some reference) but if you want try an alternative method. With InMemory I haven't had problem

/// <summary>
        /// create a memory workspace
        /// </summary>
        /// <returns>memory workspace</returns>
        public static IWorkspace CreateInMemoryWorkspace()
        {
            // Create an in-memory workspace factory.
            Type factoryType = Type.GetTypeFromProgID(
              "esriDataSourcesGDB.InMemoryWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
              Activator.CreateInstance(factoryType);

            // Create an in-memory workspace.
            IWorkspaceName workspaceName = workspaceFactory.Create(string.Empty, "memoryWorkspace", null, 0);

            // Cast for IName and open a reference to the in-memory workspace through the name object. Guid.NewGuid().ToString()
            IName name = (IName)workspaceName;
            IWorkspace workspace = (IWorkspace)name.Open();
            return workspace;
        }
0 Kudos
SimonLeo
New Contributor III
thank you for your reply.

I used InMemoryWorkspace in previous tries. It doesn't have this cleanup issue. But due to we are dealing with large data manipulation (even though I deleted temporary tables once finish using it), I noticed the results are somehow clipped which means only a small set is returned. Once we changed it to FileGDB based workspace, everything that we want is returned.


I haven't tried your method (perahps you have some reference) but if you want try an alternative method. With InMemory I haven't had problem

/// <summary>
        /// create a memory workspace
        /// </summary>
        /// <returns>memory workspace</returns>
        public static IWorkspace CreateInMemoryWorkspace()
        {
            // Create an in-memory workspace factory.
            Type factoryType = Type.GetTypeFromProgID(
              "esriDataSourcesGDB.InMemoryWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)
              Activator.CreateInstance(factoryType);

            // Create an in-memory workspace.
            IWorkspaceName workspaceName = workspaceFactory.Create(string.Empty, "memoryWorkspace", null, 0);

            // Cast for IName and open a reference to the in-memory workspace through the name object. Guid.NewGuid().ToString()
            IName name = (IName)workspaceName;
            IWorkspace workspace = (IWorkspace)name.Open();
            return workspace;
        }
0 Kudos
nicogis
MVP Frequent Contributor
yes I know InMemory is suitable for little dataset but my suggest is for see if you had lock. I normally create file geodatabase with name _ags_{guid} in output so the are deleted from arcgis server.
0 Kudos
SimonLeo
New Contributor III
yes I know InMemory is suitable for little dataset but my suggest is for see if you had lock. I normally create file geodatabase with name _ags_{guid} in output so the are deleted from arcgis server.


thanks again.

could you elaborate little more on 'create file geodatabase with name _ags_{guid} in output'? In code, I am using 'scratchWorkspaceFactory.CreateNewScratchWorkspace()' to create a new file GDB scratch workspace. is this something different from what you suggested?
0 Kudos
nicogis
MVP Frequent Contributor
Code kind:

     IMapServerInit mapServerInit = this.mapServer as IMapServerInit;

     
     string pathOutputAGS = (new System.IO.DirectoryInfo(mapServerInit.PhysicalOutputDirectory)).Parent.FullName;
     string fileName = System.IO.Path.ChangeExtension(string.Format("_ags_{0}", Guid.NewGuid().ToString()), "gdb");
            Type factoryType = Type.GetTypeFromProgID(
        "esriDataSourcesGDB.FileGDBWorkspaceFactory");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
        (factoryType);
    IWorkspaceName workspaceName = workspaceFactory.Create(pathOutputAGS, fileName, null,
        0);

    IName name = (IName)workspaceName;
    IWorkspace workspace = (IWorkspace)name.Open();


0 Kudos