Writing out temp files - How to cleanly delete when done?

Writing out temp files - How to cleanly delete when done?

Postby Chris Fullmer » Wed Aug 24, 2011 5:34 am

I am planning on writing out a bunch of temp files while the user is making the model. So far so good (thanks to Thom and TIG). I am trying to think through how to delete these temp files when they close out of the model, or open a new model, or SketchUp crashes on them, etc. Is this just dooming myself for a terrible mess of sloppy leftover temp files? Is there a reliable to know that SU has ended/crashed/etc and then erase all those files?

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: Writing out temp files - How to cleanly delete when done

Postby Dan Rathbun » Wed Aug 24, 2011 8:05 am

1) Keep a list of every filename you create. On the next load.. check to see if the files exist, if they do.. Sketchup crashed, so delete them. Then remove the filenames from the "cleanup" list.

2) You can try using an AppObserver's onQuit() callback to delete files. Unfortunately.. there's no onCloseModel() callback yet, a bummer for the Mac, but onOpenModel() can serve that purpose on the PC.
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Writing out temp files - How to cleanly delete when done

    Postby thomthom » Sun Sep 04, 2011 6:56 pm

    I don't seem to get AppObserver.onQuit to trigger under OSX. (Not 100% confirmed as its hard to debug during that process. But I can't make it clean up temp files I made during the session. But it does work under Windows.)
    The reason I try to use AppObserver.onQuit is that the Garbage collector doesn't seem to trigger when SketchUp exits under OSX.

    Even tried ModelObserver.onDeleteModel - but I can't get that to trigger at all.
    0
    Thomas Thomassen — SketchUp Monkey & Coding addict
    List of my plugins and link to the CookieWare fund
    User avatar
    thomthom 
    PluginStore Author
    PluginStore Author
     

    Re: Writing out temp files - How to cleanly delete when done

    Postby Dan Rathbun » Mon Sep 05, 2011 4:14 am

    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Writing out temp files - How to cleanly delete when done

      Postby thomthom » Mon Sep 05, 2011 8:15 am

      Dan Rathbun wrote:
        try:

        at_exit { block }

        Well, that worked on Windows. Will try on my Mac when I get home. I guess this will be the ultimate test to see if SketchUp Ruby is killed before it can exit properly.
        0
        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund
        User avatar
        thomthom 
        PluginStore Author
        PluginStore Author
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby Jim » Mon Sep 05, 2011 11:30 am

        at_exit reminded me of Signal#trap.
        0
        Hi

        Jim 
        Global Moderator
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby thomthom » Mon Sep 05, 2011 11:44 am

        Anyone with a Mac at hand able to test any of these? Getting some sort of notification when SketchUp is terminated under OSX?
        ( Mine is at home. :( )
        0
        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund
        User avatar
        thomthom 
        PluginStore Author
        PluginStore Author
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby driven » Mon Sep 05, 2011 11:53 am

        have you got a test script, I can run but not write...
        0
        learn from the mistakes of others, you may not live long enough to make them all yourself...

        driven 
        PluginStore Author
        PluginStore Author
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby Jim » Mon Sep 05, 2011 12:08 pm

        Does this look right? I am not getting any signal output - just one excepton:

        SIGVTALRM reserved for Thread; can't set handler

        Code: Select all
        $out = File.open(File.expand_path("~/signals"), "a")

        Signal.list.keys.each {|s|
        begin
        trap(s) { $out.puts(s) }
        rescue => e
        $out.puts e.inspect
        end
        }
        at_exit{$out.puts("at_exit")}
        0
        Hi

        Jim 
        Global Moderator
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby thomthom » Mon Sep 05, 2011 12:46 pm

        And that exception comes from when you tried to hook up all signals?

        What happens if you try just at_exit ?
        0
        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund
        User avatar
        thomthom 
        PluginStore Author
        PluginStore Author
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby Jim » Mon Sep 05, 2011 1:04 pm

        thomthom wrote:And that exception comes from when you tried to hook up all signals?


        No, just on the indicated signal. The exception occurs once in the output file.

        thomthom wrote:What happens if you try just at_exit ?


        I get nothing.

        Note this is SU7.1.6859 on Mac OSX 10.4.11 :?

        On Windows XP, I get an EXIT signal and at_exit by opening and closing SketchUp.
        0
        Hi

        Jim 
        Global Moderator
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby thomthom » Mon Sep 05, 2011 1:45 pm

        It's like SketchUp Ruby under OSX is killed before it's allowed to exit properly... everything seem mute. Observers and normal Ruby callbacks are all mute... :(
        0
        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund
        User avatar
        thomthom 
        PluginStore Author
        PluginStore Author
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby thomthom » Mon Sep 05, 2011 6:45 pm

        I tried at_exit under OSX 10.5, SU8. Also nothing. :(
        0
        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund
        User avatar
        thomthom 
        PluginStore Author
        PluginStore Author
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby Chris Fullmer » Mon Sep 05, 2011 7:29 pm

        Lame. I don't mind cleaning up the files the next time SU opens, but I'm concerned about deleting files that are supposed to be open - like 2 SketchUp widnows up simultaneously. I don't want to delete files for another model that is active.
        0
        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written
        User avatar
        Chris Fullmer 
        SketchUp Team
        SketchUp Team
         

        Re: Writing out temp files - How to cleanly delete when done

        Postby Dan Rathbun » Tue Sep 06, 2011 2:30 am

        Chris Fullmer wrote:... I'm concerned about deleting files that are supposed to be open - like 2 SketchUp widnows up simultaneously. I don't want to delete files for another model that is active.

        Ok the first step in solving this, is to include the Process.pid (aka $$,) number as part of the tempfile name.

        Part two will involve checking to see if that process id is currently in use. If the file's pid, is not THIS Ruby's pid, and is not found as ANOTHER sketchup.exe's pid, then the file is safe to delete.

        Jim and I were discussing how to check for pids in another topic, I think in regard to his Launcher applet (Ballista).
        One method is to use a command line utility that comes with Windows.
        tlist.exe is one. (But I remember a gotcha for certain version of XP, I think it may not come with Home, but the user can get it by installing the "Support Tools" from their install CD.

        The other way is to make a WinAPI system call, using the Win32API.so library. (Which is basically what tlist.exe does.)

        You can try it from the Console... type a backquoted 'tlist` or use %x[tlist], you should see the list of all running programs preceeded by their pid.

        so:
        pidlist = %x[tlist]
        safe_to_delete = true
        pidlist.each_line{|line| break (safe_to_delete = false) if( line =~ /(sketchup.exe)/i && line.include?(filepid.to_s) )}

        You can test it at the Console using filepid set to the current Skectup's pid.
        The each_line block should break out, and set safe_to_delete to false.
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: Writing out temp files - How to cleanly delete when done

          Postby Dan Rathbun » Tue Sep 06, 2011 11:26 pm

            Ok I found my old message between Jim & I:
            ... using tlist.exe (XP) or tasklist.exe (Vista/Win7) to check when the SU process ended. (The catch is that XP Home doesn't come with tlist.exe, it must be ... installed separately*. It comes installed standard on XP Pro, or Win Server 2003+.

            * is referring to the separate "Support Tools" installer on the Windows install CD.

            AND obviously... you need to know what equivalent utility to use on Mac OSX.
            0
              I'm not here much anymore. But a PM will fire email notifications.
              User avatar
              Dan Rathbun 
              PluginStore Author
              PluginStore Author
               

              Re: Writing out temp files - How to cleanly delete when done

              Postby Chris Fullmer » Wed Sep 07, 2011 12:45 am

              Yeah, the Mac side of things always throws me for a loop. I never know how to do things on a Mac. Thanks for the tips on this Dan.

              OH, yes now I remember. Can ruby on the Mac access multiple model objects? Would there be a way to check through SketchUp's active model or something?
              0
              Lately you've been tan, suspicious for the winter.
              All my Plugins I've written
              User avatar
              Chris Fullmer 
              SketchUp Team
              SketchUp Team
               

              Re: Writing out temp files - How to cleanly delete when done

              Postby Dan Rathbun » Wed Sep 07, 2011 1:04 am

              Chris Fullmer wrote:OH, yes now I remember. Can ruby on the Mac access multiple model objects? Would there be a way to check through SketchUp's active model or something?

              Yes the Mac edition is an MDI. Each open model has it's OWN object_id, but the API does not have a Models collection.

              However .. ALL the open model objects belong to the current Sketchup process id.

              I did BTW request a Models collection and various other MDI methods and observers in the last beta round.
              0
                I'm not here much anymore. But a PM will fire email notifications.
                User avatar
                Dan Rathbun 
                PluginStore Author
                PluginStore Author
                 

                Re: Writing out temp files - How to cleanly delete when done

                Postby Chris Fullmer » Fri Sep 09, 2011 6:41 am

                ok, how about this. I create the folder in SketchUp1. Then I open SketchUp 2 and try to delete the folder. But it won't let me since SketchUp1 already has the folder open. So I would just try to delete the folder, if I can delete then I can assume that no other SketchUp process had it opened. If I can't delete, then I assume that it is because there is another active SketchUp model.

                Is that foolproof? Or is that bound to run into issues?
                0
                Lately you've been tan, suspicious for the winter.
                All my Plugins I've written
                User avatar
                Chris Fullmer 
                SketchUp Team
                SketchUp Team
                 

                Re: Writing out temp files - How to cleanly delete when done

                Postby Dan Rathbun » Fri Sep 09, 2011 7:30 am

                I am not sure if apps can "lock" folders... like they can files. But you should get an error if the folder is not empty.

                I say KISS. Have each Sketchup process create and use it's own temp folder (with a pid as part of the name.). Just eliminate the possibility of two instances fighting over the same folder. That just complicates things.

                At least you are cleaning up after yourself.. not like most archival utilities, that leave a temp folder behind for every dang zip file they extract.
                0
                  I'm not here much anymore. But a PM will fire email notifications.
                  User avatar
                  Dan Rathbun 
                  PluginStore Author
                  PluginStore Author
                   

                  Re: Writing out temp files - How to cleanly delete when done

                  Postby Chris Fullmer » Fri Sep 09, 2011 8:00 am

                  Currently, on Vista 64 when I create the folder in sketchup ruby, if I try to delete the folder through the windows explorer it tells me the folder is currently being used in another app. And I get an error as well when I try to delete the open folder through another instance of SketchUp. I just don't know how it will react in other windows versions and on a Mac, especially since SU on OS is MDI. It might not be able to differentiate which instance of SU created the folder and which is trying to delete it. But it would be interesting to test.
                  0
                  Lately you've been tan, suspicious for the winter.
                  All my Plugins I've written
                  User avatar
                  Chris Fullmer 
                  SketchUp Team
                  SketchUp Team
                   

                  Re: Writing out temp files - How to cleanly delete when done

                  Postby Dan Rathbun » Fri Sep 09, 2011 8:56 am

                  Definitely... inquiring minds wish to know... all platform specific quirks are good knowledge. (I actually just logged on to ask a question about the Mac.)

                  So... can a separate Sketchup instance, even be opened on the Mac? Usually MDI apps try to keep the user working with the instance that is already open.. but sometimes a user can force a new instance to be opened.

                  (I recently found that this is true of Notepad++, by accident. If the app window is smaller than the screen, and you grab one particular filetab, and drag it out of the app and drop it on the desktop, it opens a new Notepad++ instance, with only that file open. And if you drag a file from an instance that only has that file open, into another instance, and the second instance closes automatically. So i'd be interested to know if Mac Sketchup has this behavior.)
                  0
                    I'm not here much anymore. But a PM will fire email notifications.
                    User avatar
                    Dan Rathbun 
                    PluginStore Author
                    PluginStore Author
                     

                    Re: Writing out temp files - How to cleanly delete when done

                    Postby AdamB » Mon Sep 12, 2011 3:39 pm

                    Chris Fullmer wrote:Currently, on Vista 64 when I create the folder in sketchup ruby, if I try to delete the folder through the windows explorer it tells me the folder is currently being used in another app. And I get an error as well when I try to delete the open folder through another instance of SketchUp. I just don't know how it will react in other windows versions and on a Mac, especially since SU on OS is MDI. It might not be able to differentiate which instance of SU created the folder and which is trying to delete it. But it would be interesting to test.


                    Chris,

                    Not being able to delete a folder/file on Windows typically means there is a process somewhere with a 'handle' to the file/folder. Yes, it is pathetic that Windows still cannot handle stuff like this - every other OS can.. but anyway..

                    First double check you really are releasing the File Descriptor handle you use in Ruby. If you the file isn't really closed, you won't be able to delete it.

                    Adam
                    0
                    Developer of LightUp Click for website
                    User avatar
                    AdamB 
                    LightUp Support
                    LightUp Support
                     

                    SketchUcation One-Liner Adverts

                    by Ad Machine » 5 minutes ago



                    Ad Machine 
                    Robot
                     



                     

                    Return to Developers' Forum

                    Who is online

                    Users browsing this forum: No registered users and 6 guests

                    Visit our sponsors: