#!/opt/local/bin/php
<?php

chdir('../..');
require_once 'common.php';

$db = Db::getInstance();
$vk = new Vkontakte();

$continue = true;
while ($continue) {
	$queue = $db->getRows($db->q("SELECT * FROM beathaven.queue WHERE status=0 OR status=2 ORDER BY priority DESC, times_failed ASC LIMIT 10"));
	if (!$queue || count($queue) == 0) {
		$continue = false;
	} else {
		foreach ($queue as $t) {
			echo "#{$t['track_id']} {$t['track_title']}\n";
			
			$vk->parse($t['track_title']);
			echo $vk->getHtml();
			
			$db->q("UPDATE beathaven.queue SET status=1 WHERE track_id=". $t['track_id']);
			} else {
				echo "FAILED\n\n";
				$db->q("UPDATE beathaven.queue SET status = 2, times_failed = times_failed + 1 WHERE track_id=". $t['track_id']);
			}
			$t2 = microtime(true);
			if ($t2 - $t1 < 5) {
				sleep(ceil(5 - ($t2 - $t1)));
			}
		}
	}
}
