Added semaphore to resize_photo to prevent resource issues on server.

This commit is contained in:
Stuart Longland 2008-10-19 08:51:40 +10:00
parent 7dd8ccd135
commit aaa78c5888

View File

@ -8,6 +8,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <jpeg.h>
#include <settings.h>
@ -460,6 +461,22 @@ char* resize_photo( struct photo_meta* dest,
}
}
/*
* Check for semaphore... if it exists, wait for it to disappear.
* For this, we use the open() syscall with O_CREAT|O_EXCL, which
* will fail if the file already exists.
*/
char* sem_file = construct_path( "s/s/s", get_cgidir(),
CACHEDIR, "semaphore" );
int fd = open( sem_file, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR );
while( fd == -1 ) {
sleep(1);
fd = open( sem_file, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR );
dprintf( "resize_photo: waiting for semaphore %s (fd: %d)\n",
sem_file, fd );
}
close(fd);
MagickWand* wand = NewMagickWand();
dprintf( "resize_photo: wand created at %p\n", wand);
if (MagickReadImage( wand, dest->filepath ) == MagickFalse) {
@ -467,6 +484,11 @@ char* resize_photo( struct photo_meta* dest,
char* description=MagickGetException(wand,&severity);
dprintf("%s %s %ld %s\n",GetMagickModule(),description);
gcfree( cache_file );
/* Get rid of the semaphore */
unlink( sem_file );
gcfree( sem_file );
return NULL;
}
@ -496,11 +518,21 @@ char* resize_photo( struct photo_meta* dest,
ExceptionType severity;
char* description=MagickGetException(wand,&severity);
dprintf("%s %s %ld %s\n",GetMagickModule(),description);
/* Get rid of the semaphore */
unlink( sem_file );
gcfree( sem_file );
gcfree( cache_file );
cache_file = NULL;
}
DestroyMagickWand( wand );
/* Get rid of the semaphore */
unlink( sem_file );
gcfree( sem_file );
gcfree( cache_path );
dprintf( "resize_photo: resized photo at %s (%p)\n", cache_file, cache_file );
return cache_file;