Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Version 1.6.3 (#65)
Browse files Browse the repository at this point in the history
This version attempts to resolve major outages in using seat-groups >1.6.0 if not used with seat-notifications installed.

* Refactored GroupSync job to use events for missing refreshtoken, successful attaches/detached role updates and failed syncs
* Events have two listeners: one for writing a log entry and one to dispatch a notification
* `MissingRefreshTokenNotification` for discord contains a link to the configuration page of that user to quicker resolve false positive messages.

log events are queued into the default queue whereas notifications are prioritized. Thank you @lawin for pointing out this bug.
  • Loading branch information
herpaderpaldent authored Jan 9, 2019
1 parent 0fd982b commit 57871fe
Show file tree
Hide file tree
Showing 14 changed files with 532 additions and 79 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#Version 1.6.3
This version attempts to resolve major outages in using seat-groups >1.6.0 if not used with seat-notifications installed.

* Refactored GroupSync job to use events for missing refreshtoken, successful attaches/detached role updates and failed syncs
* Events have two listeners: one for writing a log entry and one to dispatch a notification
* `MissingRefreshTokenNotification` for discord contains a link to the configuration page of that user to quicker resolve false positive messages.

log events are queued into the default queue whereas notifications are prioritized. Thank you @Lawin for pointing out this bug.

# Version 1.6.2
Refactoring some of the logic regarding permission checks.

Expand Down
30 changes: 30 additions & 0 deletions src/Events/GroupSyncFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 08.01.2019
* Time: 20:20.
*/

namespace Herpaderpaldent\Seat\SeatGroups\Events;

use Illuminate\Queue\SerializesModels;
use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Web\Models\Group;

class GroupSyncFailed
{
use SerializesModels;

public $group;

public $main_character;

public $sync;

public function __construct(Group $group, CharacterInfo $main_character)
{
$this->group = $group;
$this->main_character = $main_character;
}
}
31 changes: 31 additions & 0 deletions src/Events/GroupSynced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 08.01.2019
* Time: 20:20.
*/

namespace Herpaderpaldent\Seat\SeatGroups\Events;

use Illuminate\Queue\SerializesModels;
use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Web\Models\Group;

class GroupSynced
{
use SerializesModels;

public $group;

public $main_character;

public $sync;

public function __construct(Group $group, CharacterInfo $main_character, array $sync)
{
$this->group = $group;
$this->main_character = $main_character;
$this->sync = $sync;
}
}
28 changes: 28 additions & 0 deletions src/Events/MissingRefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 08.01.2019
* Time: 22:40.
*/

namespace Herpaderpaldent\Seat\SeatGroups\Events;

use Illuminate\Queue\SerializesModels;
use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Web\Models\User;

class MissingRefreshToken
{
use SerializesModels;

public $user;

public $main_character;

public function __construct(User $user, CharacterInfo $main_character)
{
$this->user = $user;
$this->main_character = $main_character;
}
}
24 changes: 24 additions & 0 deletions src/GroupsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

namespace Herpaderpaldent\Seat\SeatGroups;

use Herpaderpaldent\Seat\SeatGroups\Events\GroupSynced;
use Herpaderpaldent\Seat\SeatGroups\Events\GroupSyncFailed;
use Herpaderpaldent\Seat\SeatGroups\Events\MissingRefreshToken;
use Herpaderpaldent\Seat\SeatGroups\Listeners\CreateSyncedSeatLogsEntry;
use Herpaderpaldent\Seat\SeatGroups\Listeners\CreateSyncFailedLogsEntry;
use Herpaderpaldent\Seat\SeatGroups\Listeners\GroupSyncedNotification;
use Herpaderpaldent\Seat\SeatGroups\Listeners\GroupSyncFailedNotification;
use Herpaderpaldent\Seat\SeatGroups\Listeners\MissingRefreshTokenLogsEntry;
use Herpaderpaldent\Seat\SeatGroups\Listeners\MissingRefreshTokenNotification;
use Herpaderpaldent\Seat\SeatGroups\Observers\RefreshTokenObserver;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
Expand All @@ -24,6 +33,9 @@ public function boot()
$this->loadMigrationsFrom(__DIR__ . '/database/migrations/');

RefreshToken::observe(RefreshTokenObserver::class);

$this->add_events();

}

/**
Expand Down Expand Up @@ -71,6 +83,18 @@ private function addTranslations()
$this->loadTranslationsFrom(__DIR__ . '/lang', 'seatgroups');
}

private function add_events()
{
$this->app->events->listen(GroupSynced::class, CreateSyncedSeatLogsEntry::class);
$this->app->events->listen(GroupSynced::class, GroupSyncedNotification::class);

$this->app->events->listen(GroupSyncFailed::class, CreateSyncFailedLogsEntry::class);
$this->app->events->listen(GroupSyncFailed::class, GroupSyncFailedNotification::class);

$this->app->events->listen(MissingRefreshToken::class, MissingRefreshTokenLogsEntry::class);
$this->app->events->listen(MissingRefreshToken::class, MissingRefreshTokenNotification::class);
}

/**
* Merge the given configuration with the existing configuration.
* https://medium.com/@koenhoeijmakers/properly-merging-configs-in-laravel-packages-a4209701746d.
Expand Down
87 changes: 9 additions & 78 deletions src/Jobs/GroupSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@

namespace Herpaderpaldent\Seat\SeatGroups\Jobs;

use Herpaderpaldent\Seat\SeatGroups\Events\GroupSynced;
use Herpaderpaldent\Seat\SeatGroups\Events\GroupSyncFailed;
use Herpaderpaldent\Seat\SeatGroups\Events\MissingRefreshToken;
use Herpaderpaldent\Seat\SeatGroups\Models\Seatgroup;
use Herpaderpaldent\Seat\SeatGroups\Models\SeatgroupLog;
use Herpaderpaldent\Seat\SeatGroups\Models\SeatGroupNotification;
use Herpaderpaldent\Seat\SeatGroups\Notifications\SeatGroupErrorNotification;
use Herpaderpaldent\Seat\SeatGroups\Notifications\SeatGroupUpdateNotification;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Redis;
use Seat\Web\Models\Acl\Role;
use Seat\Web\Models\Group;
use Seat\Web\Models\User;

Expand Down Expand Up @@ -46,11 +43,6 @@ class GroupSync extends SeatGroupsJobBase
*/
public $tries = 1;

/**
* @var \Herpaderpaldent\Seat\SeatGroups\Models\SeatGroupNotification
*/
protected $recipients;

/**
* ConversationOrchestrator constructor.
*
Expand All @@ -76,14 +68,15 @@ public function __construct(Group $group)
$this->group->users->map(function ($user) { return $user->name; })->implode(', ')));
}

$this->recipients = SeatGroupNotification::all();
$this->roles = collect();

}

public function handle()
{

var_dump('test');

// in case no main character has been set, throw an exception and abort the process
if (is_null($this->main_character))
throw new MissingMainCharacterException($this->group);
Expand Down Expand Up @@ -182,20 +175,7 @@ private function catchMissingRefreshToken()
$seatgroup->member()->detach($this->group->id);
});

$message = sprintf('The RefreshToken of %s in user group of %s (%s) is missing. '
. 'Ask the owner of this user group to login again with this user, in order to provide a new RefreshToken. '
. 'This user group will lose all potentially gained roles through this character.',
$user->name, $this->main_character->name, $this->group->users->map(function ($user) {return $user->name; })
->implode(', ')
);

SeatgroupLog::create([
'event' => 'error',
'message' => $message,
]);

if (! empty($this->recipients))
Notification::send($this->recipients, (new SeatGroupErrorNotification($user, $message)));
event(new MissingRefreshToken($user, $this->main_character));

}
}
Expand All @@ -206,63 +186,14 @@ private function onFail($exception)

report($exception);

$message = sprintf('An error occurred while syncing user group of %s (%s). Please check the logs.',
$this->main_character->name,
$this->group->users->map(function ($user) {return $user->name; })->implode(', ')
);

SeatgroupLog::create([
'event' => 'error',
'message' => $message,
]);

if (! empty($this->recipients))
Notification::send($this->recipients, (new SeatGroupErrorNotification(User::find($this->main_character->character_id), $message)));
event(new GroupSyncFailed($this->group, $this->main_character));

throw $exception;

}

private function onFinish($sync)
{
$should_send_notification = false;

if (! empty($sync['attached'])) {

SeatgroupLog::create([
'event' => 'attached',
'message' => sprintf('The user group of %s (%s) has successfully been attached to the following roles: %s.',
$this->main_character->name,
$this->group->users->map(function ($user) {

return $user->name;
})->implode(', '),
Role::whereIn('id', $sync['attached'])->pluck('title')->implode(', ')
),
]);

$should_send_notification = true;
}

if (! empty($sync['detached'])) {

SeatgroupLog::create([
'event' => 'detached',
'message' => sprintf('The user group of %s (%s) has been detached from the following roles: %s.',
$this->main_character->name,
$this->group->users->map(function ($user) {

return $user->name;
})->implode(', '),
Role::whereIn('id', $sync['detached'])->pluck('title')->implode(', ')
),
]);

$should_send_notification = true;
}

if (! empty($this->recipients) && $should_send_notification)
Notification::send($this->recipients, (new SeatGroupUpdateNotification($this->group, $sync)));

if (! (empty($sync['attached']) && empty($sync['detached'])))
event(new GroupSynced($this->group, $this->main_character, $sync));
}
}
36 changes: 36 additions & 0 deletions src/Listeners/CreateSyncFailedLogsEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 08.01.2019
* Time: 22:23.
*/

namespace Herpaderpaldent\Seat\SeatGroups\Listeners;

use Herpaderpaldent\Seat\SeatGroups\Events\GroupSyncFailed;
use Herpaderpaldent\Seat\SeatGroups\Models\SeatgroupLog;
use Illuminate\Contracts\Queue\ShouldQueue;

class CreateSyncFailedLogsEntry implements ShouldQueue
{
public function __construct()
{

}

public function handle(GroupSyncFailed $event)
{

$message = sprintf('An error occurred while syncing user group of %s (%s). Please check the logs.',
$event->main_character->name,
$event->group->users->map(function ($user) {return $user->name; })->implode(', ')
);

SeatgroupLog::create([
'event' => 'error',
'message' => $message,
]);

}
}
75 changes: 75 additions & 0 deletions src/Listeners/CreateSyncedSeatLogsEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 08.01.2019
* Time: 20:28.
*/

namespace Herpaderpaldent\Seat\SeatGroups\Listeners;

use Herpaderpaldent\Seat\SeatGroups\Events\GroupSynced;
use Herpaderpaldent\Seat\SeatGroups\Models\SeatgroupLog;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Seat\Web\Models\Acl\Role;

class CreateSyncedSeatLogsEntry implements ShouldQueue
{
use InteractsWithQueue;

public function __construct()
{

}

public function handle(GroupSynced $event)
{
if (empty($event->sync['attached']) && empty($event->sync['detached']))
$this->delete();

if (! empty($event->sync['attached'])) {

SeatgroupLog::create([
'event' => 'attached',
'message' => sprintf('The user group of %s (%s) has successfully been attached to the following roles: %s.',
$event->main_character->name,
$event->group->users->map(function ($user) {

return $user->name;
})->implode(', '),
Role::whereIn('id', $event->sync['attached'])->pluck('title')->implode(', ')
),
]);
}

if (! empty($event->sync['detached'])) {

SeatgroupLog::create([
'event' => 'detached',
'message' => sprintf('The user group of %s (%s) has been detached from the following roles: %s.',
$event->main_character->name,
$event->group->users->map(function ($user) {

return $user->name;
})->implode(', '),
Role::whereIn('id', $event->sync['detached'])->pluck('title')->implode(', ')
),
]);
}

}

/**
* Handle a job failure.
*
* @param \Herpaderpaldent\Seat\SeatGroups\Events\GroupSynced $event
* @param \Exception $exception
*
* @return void
*/
public function failed(GroupSynced $event, $exception)
{
report($exception);
}
}
Loading

0 comments on commit 57871fe

Please sign in to comment.