Skip to content

Commit

Permalink
#84 The thumbail should now be updated max every 5 mins, not every mi…
Browse files Browse the repository at this point in the history
…nute. Thumbnail is now updated when a killzone or route is updated in some form or another (after half an hour with no change, that is).
  • Loading branch information
Wotuu committed Dec 11, 2018
1 parent 5b17fda commit cac0161
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Expand Up @@ -30,7 +30,7 @@ protected function schedule(Schedule $schedule)

// $schedule->command('inspire')
// ->hourly();
$schedule->call(new FindOutdatedThumbnails);
$schedule->call(new FindOutdatedThumbnails)->everyFiveMinutes();
Log::channel('scheduler')->debug("Finished scheduler");
}

Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/APIKillZoneController.php
Expand Up @@ -73,6 +73,9 @@ function store(Request $request, DungeonRoute $dungeonroute)

// Bulk insert
KillZoneEnemy::insert($killZoneEnemies);

// Touch the route so that the thumbnail gets updated
$dungeonroute->touch();
}

$result = ['id' => $killZone->id, 'enemy_forces' => $dungeonroute->getEnemyForcesAttribute()];
Expand Down Expand Up @@ -102,6 +105,9 @@ function delete(Request $request, DungeonRoute $dungeonroute, KillZone $killzone
// Refresh the killzones relation
$dungeonroute->load('killzones');

// Touch the route so that the thumbnail gets updated
$dungeonroute->touch();

$result = ['result' => 'success', 'enemy_forces' => $dungeonroute->getEnemyForcesAttribute()];
} catch (\Exception $ex) {
$result = response('Not found', Http::NOT_FOUND);
Expand Down
11 changes: 9 additions & 2 deletions app/Http/Controllers/APIRouteController.php
Expand Up @@ -66,6 +66,9 @@ function store(Request $request)

// Bulk insert
RouteVertex::insert($vertices);

// Touch the route so that the thumbnail gets updated
$dungeonRoute->touch();
}

$result = ['id' => $route->id];
Expand All @@ -82,15 +85,19 @@ function delete(Request $request)
$route = Route::findOrFail($request->get('id'));

// @TODO WTF why does $route->dungeonroute not work?? It will NOT load the relation despite everything being OK?
$dungeonroute = DungeonRoute::findOrFail($route->dungeon_route_id);
$dungeonRoute = DungeonRoute::findOrFail($route->dungeon_route_id);
// If we're not the author, don't delete anything
// @TODO handle this in a policy?
if ($dungeonroute->author_id !== Auth::user()->id && !Auth::user()->hasRole('admin')) {
if ($dungeonRoute->author_id !== Auth::user()->id && !Auth::user()->hasRole('admin')) {
throw new Exception('Unauthorized');
}

$route->delete();
$route->deleteVertices();

// Touch the route so that the thumbnail gets updated
$dungeonRoute->touch();

$result = ['result' => 'success'];
} catch (\Exception $ex) {
$result = response('Not found', Http::NOT_FOUND);
Expand Down
11 changes: 7 additions & 4 deletions app/Logic/Scheduler/FindOutdatedThumbnails.php
Expand Up @@ -31,10 +31,13 @@ function __invoke()
$updatedAt = Carbon::createFromTimeString($dungeonRoute->updated_at);
$thumbnailUpdatedAt = Carbon::createFromTimeString($dungeonRoute->thumbnail_updated_at);

// If the route has been updated in the past 30 minutes...
if ($updatedAt->addMinute(30)->isPast() &&
// Updated at is greater than the thumbnail updated at (don't keep updating thumbnails..
$updatedAt->greaterThan($thumbnailUpdatedAt)) {
if ((// Updated at is greater than the thumbnail updated at (don't keep updating thumbnails..)
$updatedAt->greaterThan($thumbnailUpdatedAt) &&
// If the route has been updated in the past x minutes...
$updatedAt->addMinute(config('keystoneguru.thumbnail_refresh_min'))->isPast())
||
// Update every month regardless
$updatedAt->addMonth(1)->isPast()) {

if (!$this->isJobQueuedForModel('App\Jobs\ProcessRouteFloorThumbnail', $dungeonRoute)) {
Log::channel('scheduler')->debug(sprintf('Queueing job for route %s (%s floors)', $dungeonRoute->public_key, $dungeonRoute->dungeon->floors->count()));
Expand Down
36 changes: 19 additions & 17 deletions app/Models/DungeonRoute.php
Expand Up @@ -78,23 +78,6 @@ public function getRouteKeyName()
return 'public_key';
}

/**
* @return string Generates a random public key that is displayed to the user in the URL.
*/
public static function generateRandomPublicKey()
{
do {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$newKey = '';
for ($i = 0; $i < 7; $i++) {
$newKey .= $characters[rand(0, $charactersLength - 1)];
}
} while (DungeonRoute::all()->where('public_key', '=', $newKey)->count() > 0);

return $newKey;
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
Expand Down Expand Up @@ -529,6 +512,8 @@ public function isFavoritedByCurrentUser()
return $result;
}



/**
* @param null $user
* @return bool
Expand All @@ -543,6 +528,23 @@ public function isOwnedByUser($user = null)
return $user !== null && $this->author_id === $user->id;
}

/**
* @return string Generates a random public key that is displayed to the user in the URL.
*/
public static function generateRandomPublicKey()
{
do {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$newKey = '';
for ($i = 0; $i < 7; $i++) {
$newKey .= $characters[rand(0, $charactersLength - 1)];
}
} while (DungeonRoute::all()->where('public_key', '=', $newKey)->count() > 0);

return $newKey;
}

public static function boot()
{
parent::boot();
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config/keystoneguru.php
Expand Up @@ -51,7 +51,7 @@
'view_time_threshold_mins' => 30,

/**
* The amount of time to give the route thumbnail processor to complete its processing.
* The amount of time in minutes that must pass before a thumbnail is generated again from a changed dungeon route.
*/
'route_thumbnail_virtual_time_budget' => 10000
'thumbnail_refresh_min' => 30
];
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -16,8 +16,9 @@
"dependencies": {
"@fortawesome/fontawesome-free": "^5.6.0",
"ajv": "^6.6.1",
"alpha-shape": "^1.0.0",
"bootstrap": "^4.1.3",
"bootstrap-select": "^1.13.3",
"bootstrap-select": "^1.13.4",
"bootswatch": "^4.1.3",
"color-interpolate": "^1.0.4",
"cross-env": "^5.2.0",
Expand Down

0 comments on commit cac0161

Please sign in to comment.