-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
405 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
database/migrations/2019_07_19_130724_add_url_status_to_redirects_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddUrlStatusToRedirectsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('redirects', function (Blueprint $table) { | ||
$table->boolean('status')->default(0); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('redirects', function (Blueprint $table) { | ||
$table->dropColumn('status'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
use Arbory\Base\Jobs\UpdateRedirectUrlStatus; | ||
use Illuminate\Foundation\Bus\DispatchesJobs; | ||
|
||
class RedirectHealthCommand extends Command | ||
{ | ||
use DispatchesJobs; | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'arbory.redirect-health | ||
{ids=[] : The array of IDs from redirects table to check (if not provided then would be selected all redirects table entries)} | ||
{--errors : Show curl request errors}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Runs an URL healthcheck to verify the redirects table `to_url` is available and update `status` field in table'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
try { | ||
$ids = $this->getIDs(); | ||
$job = new UpdateRedirectUrlStatus($ids); | ||
|
||
$this->info('Start to check '.count($ids).' entries...'); | ||
|
||
$this->dispatchNow($job); | ||
$result = $job->getResult(); | ||
|
||
if (! empty($result) && count($result->getInvalidUrlList())) { | ||
$this->warn("\nInvalid URLs list:"); | ||
foreach ($result->getInvalidUrlList() as $url) { | ||
$this->warn($url); | ||
} | ||
} | ||
|
||
if ($this->option('errors') && ! empty($result) && count($result->getErrors())) { | ||
foreach ($result->getErrors() as $url => $err) { | ||
$this->error("Request to $url - $err"); | ||
} | ||
} | ||
|
||
$this->warn("\nInvalid entries: {$result->getInvalidCount()}"); | ||
$this->info("Valid entries: {$result->getValidCount()}"); | ||
} catch (\Exception $e) { | ||
$this->error('Redirects healthcheck failed with an exception'); | ||
$this->error($e->getMessage()); | ||
|
||
return 2; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function selectAllRedirectIds() | ||
{ | ||
$results = DB::table('redirects')->where('id', '>', 0)->pluck('id')->toArray(); | ||
|
||
return $results; | ||
} | ||
|
||
/** | ||
* @param array $entryIds | ||
* @param int $status | ||
*/ | ||
public function setStatus(array $entryIds, int $status) | ||
{ | ||
DB::table('redirects')->whereIn('id', $entryIds)->update(['status' => $status]); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function getIDs() | ||
{ | ||
$param = $this->argument('ids'); | ||
|
||
$final_ids = []; | ||
foreach (explode(',', $param) as $id) { | ||
if (is_numeric($id)) { | ||
$final_ids[] = $id; | ||
} | ||
} | ||
|
||
if (count($final_ids)) { | ||
return $final_ids; | ||
} | ||
|
||
return $this->selectAllRedirectIds(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Jobs; | ||
|
||
use Illuminate\Log\Logger; | ||
use Illuminate\Support\Facades\DB; | ||
use Arbory\Base\RedirectHealthChecker; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
|
||
class UpdateRedirectUrlStatus implements ShouldQueue | ||
{ | ||
/** | ||
* @var Logger | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $redirectIds; | ||
|
||
/** @var RedirectHealthChecker */ | ||
private $redirectHealthChecker; | ||
|
||
/** | ||
* UpdateRedirectUrlStatus constructor. | ||
* @param array $redirectIds | ||
*/ | ||
public function __construct(array $redirectIds) | ||
{ | ||
$this->redirectIds = $redirectIds; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @param Logger $logger | ||
* @return void | ||
*/ | ||
public function handle(Logger $logger) | ||
{ | ||
$this->logger = $logger; | ||
|
||
$this->checkAndUpdateRedirectStatus(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
private function checkAndUpdateRedirectStatus() | ||
{ | ||
try { | ||
$redirects = $this->selectRedirects($this->redirectIds); | ||
|
||
$redirectHealthChecker = new RedirectHealthChecker($redirects); | ||
$redirectHealthChecker->check(); | ||
|
||
$this->updateStatusBulk($redirectHealthChecker->getValidIds(), true); | ||
$this->updateStatusBulk($redirectHealthChecker->getInvalidIds(), false); | ||
|
||
$this->redirectHealthChecker = $redirectHealthChecker; | ||
} catch (\Exception $e) { | ||
$this->logger->warning($e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $ids | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public function selectRedirects(array $ids) | ||
{ | ||
$results = DB::table('redirects')->whereIn('id', $ids)->get(['id', 'to_url']); | ||
|
||
return $results; | ||
} | ||
|
||
/* | ||
* @return void | ||
*/ | ||
public function updateStatusBulk(array $entryIds, int $status) | ||
{ | ||
DB::table('redirects')->whereIn('id', $entryIds)->update(['status' => $status]); | ||
} | ||
|
||
/** | ||
* @return RedirectHealthChecker|null | ||
*/ | ||
public function getResult() | ||
{ | ||
return $this->redirectHealthChecker ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Arbory\Base\Observers; | ||
|
||
use Arbory\Base\Pages\Redirect; | ||
use Arbory\Base\Jobs\UpdateRedirectUrlStatus; | ||
use Illuminate\Foundation\Bus\DispatchesJobs; | ||
|
||
class RedirectObserver | ||
{ | ||
use DispatchesJobs; | ||
|
||
/** | ||
* Handle the redirect "saved" event. | ||
* | ||
* @param Redirect $redirect | ||
* @return void | ||
*/ | ||
public function saved(Redirect $redirect) | ||
{ | ||
$job = new UpdateRedirectUrlStatus([$redirect->id]); | ||
$this->dispatchNow($job); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ class Redirect extends Model | |
protected $fillable = [ | ||
'from_url', | ||
'to_url', | ||
'status', | ||
]; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.