1

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 files are being created in that folder and it's size is growing on a daily basis and currently it's consuming around 90% of partition size. Moreover, i am not able to read their content also. So, why these files are being created and would they be delete after a while or do i need to manually delete them.

    -rw------- 1 ubuntu ubuntu   16M Feb 22 09:46 magick-d6ascKJV
    -rw------- 1 ubuntu ubuntu   16M Feb 22 09:46 magick-46ccZIfq
    -rw------- 1 ubuntu ubuntu  1.8M Feb 22 09:47 magick-vUET7vyh
    -rw------- 1 ubuntu ubuntu  1.8M Feb 22 09:47 magick-OLkGWTX8
    -rw------- 1 ubuntu ubuntu   15M Feb 22 09:48 magick-LNMV7YvE
    -rw------- 1 ubuntu ubuntu   16M Feb 22 09:49 magick-0LMYt6Kc
    -rw------- 1 ubuntu ubuntu   16M Feb 22 09:50 magick-ceNxX5CY
    -rw------- 1 ubuntu ubuntu   16M Feb 22 09:50 magick-nQ1M3y6I

Edit :

I am not using the following two lines in my http Handler. Reason being i couldn't find any explanation to do so. Moreover, the go http handler works fine. So, what is the purpose of these statements?

imagick.Initialize() 
defer imagick.Terminate()

I am assuming there would be some reason to include them in the code. So, in go http handler. Where it should be included inside func main() or inside serveHTTP ?

func main() {                                           
    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) {

}
Naresh
  • 5,073
  • 12
  • 67
  • 124

3 Answers3

1

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
}
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Actually, I am not using these lines in my go http handler imagick.Initialize() defer imagick.Terminate(). Reason being i couldn't understand what was the role of these and code runs perfectly fine without giving any error. Kindly check the question i have updated it. – Naresh Feb 22 '16 at 13:31
  • You should add those environment set-up & tear-down methods. They are important. The terminate method is the one that will clean up the environment. See [C code](http://www.imagemagick.org/api/MagickCore/magick_8c_source.html#l01365) – emcconville Feb 22 '16 at 13:32
  • Could you please tell me where should i add it. If, I am using the go Htt handler. Should i include it in `func main()` or in `serveHTTP` . Becasue if i include it in the `func main()` defer will never be called. Reason being server will start only for once. – Naresh Feb 22 '16 at 13:45
  • but it gives the following error. If i include it in the serveHTTP method. Where as it doesn't give error while declared in `func main()` Error : `Memory allocation failed Success' @ fatal/tiff.c/UnregisterTIFFImage/2378.` – Naresh Feb 22 '16 at 14:31
  • 1
    These calls should not be used per worker. Just like they are used in the official ImageMagick C-API, they are meant to be called once for a long running application. It is not hard to understand where they should be called, if you just look at any of the included examples, both in the Go bindings and in the official C-API: https://github.com/gographics/imagick/blob/master/examples/resize/main.go – jdi Feb 24 '16 at 10:17
  • @jdi That's what i was confused @emcconville asked to do in the `serveHTTP` where as it should be included in `func main()`, right? – Naresh Feb 24 '16 at 10:30
  • @Naresh Yea, you want it called exactly once. So main() would be ideal, before any http handlers needs to make imagick calls. – jdi Feb 24 '16 at 10:38
1

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

Kuldeep Dangi
  • 4,126
  • 5
  • 33
  • 56
0

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) {

}
Naresh
  • 5,073
  • 12
  • 67
  • 124