Browse Source

On arrete avec les lock globaux, plus verif code retour

master
Cabillot Julien 6 years ago
parent
commit
bcadfaf72f
1 changed files with 15 additions and 59 deletions
  1. 74
      lib/Download.php

74
lib/Download.php

@ -92,6 +92,8 @@ class Download {
echo 'le fichier existe deja, exit';
exit();
}
// On le creer tout de suite pour eviter des conflits de process
touch DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename;
$fp = fopen(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename, 'w+');
$ch = curl_init($this->premURL);
@ -103,9 +105,6 @@ class Download {
curl_close($ch);
fclose($fp);
rename(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename, DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$this->filename);
// TODO : verifier le retour ?
$this->addEvent([
'timestamp' => time(),
'state' => 'downloaded',
@ -120,6 +119,18 @@ class Download {
'starttransfer_time' => $retour['starttransfer_time']
]
]);
if (200 !== $retour['http_code']) {
$db = DbMongo::get();
$coll = $db->download;
$dbRet = $coll->update(
['_id' => $this->_id],
['$set' => ['cstate' => 'error']]
);
return false;
}
rename(DOWNLOADINGDIR.DIRECTORY_SEPARATOR.$this->filename, DOWNLOADEDDIR.DIRECTORY_SEPARATOR.$this->filename);
$db = DbMongo::get();
$coll = $db->download;
@ -131,70 +142,15 @@ class Download {
return $retour;
}
/**
* Retourne si l'on peut lancer un nouveau dl ou si un lock est deja en place
*
* @return bool
*/
static function lockOk()
{
if (file_exists(DOWNLOADLOCK)) {
return false;
} else {
return true;
}
}
/**
* Mets en place un lock, si err -> false
*
* @return bool
*/
static function putLock()
{
if (!touch(DOWNLOADLOCK)) {
return false;
}
return true;
}
/**
* Supprime le lock
*/
static function removeLock()
{
if (!file_exists(DOWNLOADLOCK)) {
echo 'le fichier de lock n\'existe plus !!! exit';
exit();
}
if (!unlink(DOWNLOADLOCK)) {
echo 'impossible de supprimer le fichier de lock !!! exit';
exit();
}
}
/**
* Lance le telechargement du prochain fichier a dl
*/
static function launchNextDownload()
{
if (!self::lockOk()) {
echo json_encode('deja lock');
exit();
}
if (!self::putLock()) {
echo json_encode('putlock err');
exit();
}
echo json_encode('Ok');
echo json_encode('Traitement');
fastcgi_finish_request();
$toDownload = self::findNextJob();
$toDownload->get();
self::removeLock();
}
}
Loading…
Cancel
Save