/**
 * Takes some work orders from the process queue and performs them
 */
function processQueue()
{
    global $db, $config;
    //Only allows a few work orders being executed at once, so we can do this very often
    if (TaskQueue::getTaskQueueStatusCnt(ORDER_EXECUTING) >= $config['process']['process_limit']) {
        echo "TOO MUCH ACTIVE WORK, ABORTING\n";
        return;
    }
    $job = TaskQueue::getOldestEntry();
    if (!$job) {
        return;
    }
    //mark current job as "IN PROGRESS" so another process won't start on it aswell
    TaskQueue::markTask($job['entryId'], ORDER_EXECUTING);
    echo "\n\n-------------\n";
    switch ($job['orderType']) {
        case TASK_IMAGE_RECODE:
            echo 'IMAGE RECODE<br/>';
            if (!in_array($job['orderParams'], $h->files->image_mime_types)) {
                echo 'error: invalid mime type<br/>';
                $h->session->log('Process queue error - image conversion destination mimetype not supported: ' . $job['orderParams'], LOGLEVEL_ERROR);
                break;
            }
            $newId = $h->files->cloneFile($job['referId'], FILETYPE_CLONE_CONVERTED);
            $exec_start = microtime(true);
            $check = convertImage($h->files->findUploadPath($job['referId']), $h->files->findUploadPath($newId), $job['orderParams']);
            $exec_time = microtime(true) - $exec_start;
            echo 'Execution time: ' . shortTimePeriod($exec_time) . '<br/>';
            if (!$check) {
                $h->session->log('#' . $job['entryId'] . ': IMAGE CONVERT failed! format=' . $job['orderParams'], LOGLEVEL_ERROR);
                echo 'Error: Image convert failed!<br/>';
                break;
            }
            $h->files->updateFile($newId, $job['orderParams']);
            markQueueCompleted($job['entryId'], $exec_time);
            break;
        case TASK_AUDIO_RECODE:
            //Recodes source audio file into orderParams destination format
            $dst_audio_ok = array('ogg', 'wma', 'mp3');
            //FIXME: config item or $h->files->var
            if (!in_array($job['orderParams'], $dst_audio_ok)) {
                echo 'error: invalid mime type<br/>';
                $h->session->log('Process queue error - audio conversion destination mimetype not supported: ' . $job['orderParams'], LOGLEVEL_ERROR);
                break;
            }
            $file = $h->files->getFileInfo($job['referId']);
            if (!$file) {
                echo 'Error: no fileentry existed for fileId ' . $job['referId'];
                break;
            }
            $newId = $h->files->cloneFile($job['referId'], FILETYPE_CLONE_CONVERTED);
            echo 'Recoding source audio of "' . $file['fileName'] . '" (' . $file['fileMime'] . ') to format ' . $job['orderParams'] . " ...\n";
            switch ($job['orderParams']) {
                case 'application/x-ogg':
                    //FIXME hur anger ja dst-format utan filändelse? tvingas göra det i 2 steg nu
                    $dst_file = 'tmpfile.ogg';
                    $c = '/usr/local/bin/ffmpeg -i "' . $h->files->findUploadPath($job['referId']) . '" ' . $dst_file;
                    break;
                case 'audio/x-ms-wma':
                    $dst_file = 'tmpfile.wma';
                    $c = '/usr/local/bin/ffmpeg -i "' . $h->files->findUploadPath($job['referId']) . '" ' . $dst_file;
                    break;
                case 'audio/mpeg':
                case 'audio/x-mpeg':
                    //fixme: source & destination should not be able to be the same!
                    $dst_file = 'tmpfile.mp3';
                    $c = '/usr/local/bin/ffmpeg -i "' . $h->files->findUploadPath($job['referId']) . '" ' . $dst_file;
                    break;
                default:
                    die('unknown destination audio format: ' . $job['orderParams']);
            }
            echo 'Executing: ' . $c . "\n";
            $exec_time = exectime($c);
            echo 'Execution time: ' . shortTimePeriod($exec_time) . "\n";
            if (!file_exists($dst_file)) {
                echo '<b>FAILED - dst file ' . $dst_file . " dont exist!\n";
                break;
            }
            //FIXME: behöver inget rename-steg. kan skriva till rätt output fil i första steget
            rename($dst_file, $h->files->upload_dir . $newId);
            $h->files->updateFile($newId);
            markQueueCompleted($job['entryId'], $exec_time);
            break;
        case TASK_VIDEO_RECODE:
            echo "VIDEO RECODE:\n";
            $exec_start = microtime(true);
            if (convertVideo($job['referId'], $job['orderParams']) === false) {
                markQueue($job['entryId'], ORDER_FAILED);
            } else {
                markQueueCompleted($job['entryId'], microtime(true) - $exec_start);
            }
            break;
        case TASK_FETCH:
            echo "FETCH CONTENT\n";
            $fileName = basename($job['orderParams']);
            //extract filename part of url, used as "filename" in database
            $http = new HttpClient($job['orderParams']);
            $http->getHead();
            if ($http->getStatus() != 200) {
                // retry in 20 seconds if file is not yet ready
                retryQueueEntry($job['entryId'], 20);
                break;
            }
            $newFileId = FileList::createEntry(FILETYPE_PROCESS, 0, 0, $fileName);
            $c = 'wget ' . escapeshellarg($job['orderParams']) . ' -O ' . FileInfo::getUploadPath($newFileId);
            echo "\$ " . $c . "\n";
            $retval = 0;
            $exec_time = exectime($c, $retval);
            if (!$retval) {
                //TODO: process html document for media links if it is a html document
                TaskQueue::markTaskCompleted($job['entryId'], $exec_time, $newFileId);
                FileInfo::updateData($newFileId);
            } else {
                //wget failed somehow, delay work for 1 minute
                retryQueueEntry($job['entryId'], 60);
                $files->deleteFile($newFileId, 0, true);
                //remove failed local file entry
            }
            break;
        case TASK_CONVERT_TO_DEFAULT:
            echo "CONVERT TO DEFAULT\n";
            //referId is entryId of previous proccess queue order
            $params = unserialize($job['orderParams']);
            $prev_job = TaskQueue::getEntry($job['referId']);
            if ($prev_job['orderStatus'] != ORDER_COMPLETED) {
                retryQueueEntry($job['entryId'], 60);
                break;
            }
            $file = $files->getFileInfo($prev_job['referId']);
            $exec_start = microtime(true);
            $newId = false;
            switch ($file['mediaType']) {
                case MEDIATYPE_VIDEO:
                    $newId = convertVideo($prev_job['referId'], $h->files->default_video, !empty($params['callback']) ? false : true, !empty($params['watermark']) ? $params['watermark'] : '');
                    break;
                case MEDIATYPE_AUDIO:
                    $newId = convertAudio($prev_job['referId'], $h->files->default_audio);
                    break;
                default:
                    echo "UNKNOWN MEDIA TYPE " . $file['mediaType'] . ", MIME TYPE " . $file['fileMime'] . ", CANNOT CONVERT MEDIA!!!\n";
                    break;
            }
            if (!$newId) {
                markQueue($job['entryId'], ORDER_FAILED);
                return false;
            }
            markQueueCompleted($job['entryId'], microtime(true) - $exec_start);
            if (empty($params['callback'])) {
                break;
            }
            //'uri' isnt known before the new file is created so it is added at this point
            $uri = $config['core']['full_url'] . 'api/file.php?id=' . $newId;
            $params['callback'] .= (strpos($params['callback'], '?') !== false ? '&' : '?') . 'uri=' . urlencode($uri);
            $data = file_get_contents($params['callback']);
            echo "Performing callback: " . $params['callback'] . "\n\n";
            echo "Callback script returned:\n" . $data;
            storeCallbackData($job['entryId'], $data, $params);
            break;
        default:
            echo "Unknown ordertype: " . $job['orderType'] . "\n";
            d($job);
            die;
    }
}