Skip to content

Instantly share code, notes, and snippets.

@leemason
Created May 14, 2016 16:12
Show Gist options
  • Save leemason/0090848380391c72c36194c74e02cf86 to your computer and use it in GitHub Desktop.
Save leemason/0090848380391c72c36194c74e02cf86 to your computer and use it in GitHub Desktop.
<?php
namespace App\Jobs\Domains;
use App\Events\Domains\ScreenshotUpdated;
use App\Jobs\Job;
use App\Models\Domain;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Symfony\Component\Process\Process;
class Screenshot extends Job implements ShouldQueue
{
use InteractsWithQueue;
protected $domain;
public function __construct(Domain $domain){
$this->domain = $domain;
}
public function handle(){
$p = new Process('phantomjs '. base_path('screenshot.js') . ' http://'.$this->domain->host.' ' . public_path('screenshots/' . $this->domain->host . '.png'));
// We use must run and throw exceptions without catching them to ensure this job gets
// re-tried and also goes into the failed jobs table after x attempts
$p->mustRun(function ($type, $buffer) {
if (Process::ERR === $type) {
echo 'ERR > '.$buffer;
} else {
echo 'OUT > '.$buffer;
}
});
if($p->isSuccessful()){
$this->domain->screenshot = url('screenshots/' . $this->domain->host . '.png');
$this->domain->screenshot_updated = new Carbon();
$this->domain->save();
event(new ScreenshotUpdated($this->domain));
}else{
throw new \Exception("Failed creating screenshot for: " . $this->domain->host);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment