Skip to content

Commit

Permalink
Remove timeLimit, and make temporary directory configurable. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell authored and dafeder committed Aug 26, 2019
1 parent 92de261 commit 2426fd7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/vendor/
composer.lock
.idea

43 changes: 24 additions & 19 deletions src/FileFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@

class FileFetcher extends Job
{

private $temporaryDirectory;
private $chunkSizeInBytes = (1024 * 100);
private $timeLimit;

public function __construct($filePath)
public function __construct($filePath, $temporaryDirectory = "/tmp")
{
parent::__construct();

$this->temporaryDirectory = $temporaryDirectory;

$state = [
'source' => $filePath,
'total_bytes_copied' => 0
'source' => $filePath,
'total_bytes_copied' => 0
];

$file = new \SplFileObject($filePath);
Expand All @@ -31,12 +32,11 @@ public function __construct($filePath)

$state['total_bytes'] = $file->isFile() ? $file->getSize() : $this->getRemoteFileSize($filePath);

$this->setState($state);
}
if (file_exists($state['destination'])) {
$state['total_bytes_copied'] = filesize($state['destination']);
}

public function setTimeLimit($seconds)
{
$this->timeLimit = $seconds;
$this->setState($state);
}

protected function runIt()
Expand Down Expand Up @@ -116,9 +116,11 @@ private function copy()
}

$destination_file = $this->getStateProperty('destination');
$time_limit = ($this->timeLimit) ? time() + $this->timeLimit : time() + PHP_INT_MAX;
$total = $this->getStateProperty('total_bytes_copied');

$time_limit = ($this->getTimeLimit()) ? time() + $this->getTimeLimit() : time() + PHP_INT_MAX;


while ($chunk = $this->getChunk()) {
if (!file_exists($destination_file)) {
$bytesWritten = file_put_contents($destination_file, $chunk);
Expand All @@ -128,8 +130,8 @@ private function copy()

if ($bytesWritten !== strlen($chunk)) {
throw new \RuntimeException(
"Unable to fetch {$this->setStateProperty('source')}. " .
" Reason: Failed to write to destination " . $dest->getPath(),
"Unable to fetch {$this->getStateProperty('source')}. " .
" Reason: Failed to write to destination " . $destination_file,
0
);
}
Expand All @@ -141,7 +143,7 @@ private function copy()
$this->setStateProperty('total_bytes_copied', $total);
throw new FileCopyInterruptedException(
"Stopped copying file after {$total} bytes. Time limit of " .
"{$this->timeLimit} second(s) reached."
"{$this->getTimeLimit()} second(s) reached."
);
}
}
Expand Down Expand Up @@ -198,7 +200,7 @@ private function getTemporaryFile(string $filename): string
*/
private function getTemporaryDirectory()
{
return "/tmp";
return $this->temporaryDirectory;
}

/**
Expand All @@ -225,7 +227,7 @@ public function setStateProperty($property, $value)

public function jsonSerialize()
{
return (object) ['timeLimit' => $this->timeLimit, 'result' => $this->getResult()];
return (object) ['timeLimit' => $this->getTimeLimit(), 'result' => $this->getResult(), 'temporaryDirectory' => $this->temporaryDirectory];
}

public static function hydrate($json)
Expand All @@ -237,15 +239,18 @@ public static function hydrate($json)

$reflector = new \ReflectionClass($object);

$p = $reflector->getProperty('timeLimit');
$p = $reflector->getParentClass()->getProperty('timeLimit');
$p->setAccessible(true);
$p->setValue($object, $data->timeLimit);

$class = $reflector->getParentClass();
$p = $class->getProperty('result');
$p = $reflector->getParentClass()->getProperty('result');
$p->setAccessible(true);
$p->setValue($object, Result::hydrate(json_encode($data->result)));

$p = $reflector->getProperty('temporaryDirectory');
$p->setAccessible(true);
$p->setValue($object, $data->temporaryDirectory);

return $object;
}
}

0 comments on commit 2426fd7

Please sign in to comment.