lot of temp magick files created in temporary folder

前端 未结 3 1027
走了就别回头了
走了就别回头了 2021-01-07 10:23

I am using the imagick library to resizing and cropping the images in a http handler. Which doesn\'t write anything in /tmp folder. But as i can a lot of these

相关标签:
3条回答
  • 2021-01-07 10:36

    You can set a cron which runs in a day or after an hour cleaning up your temp folder for all temporary magick files. you can use this on line command to delete all magick files as well

    sudo find /tmp/ -name "magick-*" -type f -delete

    0 讨论(0)
  • 2021-01-07 10:44

    Actually it should be included in the func main() not in the func serveHTTP(). It is meant to be called once for a long running application and it solved my problem.

    func main() {   
    
        imagick.Initialize() 
        defer imagick.Terminate()                                        
        myMux := http.NewServeMux()
        myMux.HandleFunc("/", serveHTTP)
    
        if err := http.ListenAndServe(":8085", myMux); err != nil {
            logFatal("Error when starting or running http server: %v", err)
        }       
    
    }
    
    func serveHTTP(w http.ResponseWriter, r *http.Request) {
    
    }
    
    0 讨论(0)
  • 2021-01-07 10:55

    Temporary / intermediate files should be automatically cleaned-up by the calling ImageMagick thread. The pattern-behavior you are describing hints at a bug in the application using ImageMagick.

    I would suggest...

    1. Check error logs of application.
    2. Evaluate error reporting of calling code.
    3. Ensure proper imagick.Initialize & imagick.Terminate routines are called

    And if nothing else works, use the environment variable MAGICK_TMPDIR to control where the imagemagick artifacts are written to.

    Update

    The imagick.Initialize wraps the underlying MagickCoreGenesis, and imagick.Terminate the MagickCoreTerminus routine. They are important for managing the environment in which ImageMagick operates. They should be called on the worker thread that will handle any ImageMagick tasks, so in your case, serveHTTP method. BUT it would not be optimal to perform such routines with every HTTP request, and an additional condition should be evaluated -- if possible.

    func serveHTTP(w http.ResponseWriter, r *http.Request) {
       // if request will do image work
       imagick.Initialize() 
       defer imagick.Terminate()
       // ... image methods ...
       // end if
    }
    
    0 讨论(0)
提交回复
热议问题