Skip to content
Permalink
Browse files
✨ Start, stop and refresh webhooks
  • Loading branch information
lorisleiva committed Oct 2, 2018
1 parent 3e75ff9 commit 0f853f7d3e705acf02fe3ee7cb4e963215c4cd94
Showing with 74 additions and 0 deletions.
  1. +6 βˆ’0 app/Calendar.php
  2. +1 βˆ’0 app/Concerns/Synchronizable.php
  3. +2 βˆ’0 app/Console/Kernel.php
  4. +6 βˆ’0 app/GoogleAccount.php
  5. +24 βˆ’0 app/Jobs/RefreshWebhookSynchronizations.php
  6. +35 βˆ’0 app/Synchronization.php
@@ -6,6 +6,7 @@
use App\Event;
use App\GoogleAccount;
use App\Jobs\SynchronizeGoogleEvents;
use App\Jobs\WatchGoogleEvents;
use Illuminate\Database\Eloquent\Model;

class Calendar extends Model
@@ -30,4 +31,9 @@ public function synchronize()
{
SynchronizeGoogleEvents::dispatch($this);
}

public function watch()
{
WatchGoogleEvents::dispatch($this);
}
}
@@ -33,4 +33,5 @@ public function getGoogleService($service)
}

abstract public function synchronize();
abstract public function watch();
}
@@ -3,6 +3,7 @@
namespace App\Console;

use App\Jobs\PeriodicSynchronizations;
use App\Jobs\RefreshWebhookSynchronizations;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

@@ -26,6 +27,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
$schedule->job(new PeriodicSynchronizations())->everyFifteenMinutes();
$schedule->job(new RefreshWebhookSynchronizations())->daily();
}

/**
@@ -5,6 +5,7 @@
use App\Calendar;
use App\Concerns\Synchronizable;
use App\Jobs\SynchronizeGoogleCalendars;
use App\Jobs\WatchGoogleCalendars;
use App\Services\Google;
use App\User;
use Illuminate\Database\Eloquent\Model;
@@ -35,4 +36,9 @@ public function synchronize()
{
SynchronizeGoogleCalendars::dispatch($this);
}

public function watch()
{
WatchGoogleCalendars::dispatch($this);
}
}
@@ -0,0 +1,24 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class RefreshWebhookSynchronizations implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function handle()
{
Synchronization::query()
->whereNotNull('resource_id')
->whereNull('expired_at')
->orWhere('expired_at', '<', now()->addDays(2))
->get()
->each->refreshWebhook();
}
}
@@ -23,6 +23,36 @@ public function ping()
return $this->synchronizable->synchronize();
}

public function startListeningForChanges()
{
return $this->synchronizable->watch();
}

public function stopListeningForChanges()
{
if (! $this->resource_id) {
return;
}

$this->synchronizable
->getGoogleService('Calendar')
->channels->stop($this->asGoogleChannel());
}

public function refreshWebhook()
{
$this->stopListeningForChanges();

// Update the UUID since the previous one has
// already been associated to a Google Channel.
$this->id = Uuid::uuid4();
$this->save();

$this->startListeningForChanges();

return $this;
}

public function asGoogleChannel()
{
return tap(new \Google_Service_Calendar_Channel(), function ($channel) {
@@ -48,7 +78,12 @@ public static function boot()
});

static::created(function ($synchronization) {
$synchronization->startListeningForChanges();
$synchronization->ping();
});

static::deleting(function ($synchronization) {
$synchronization->stopListeningForChanges();
});
}
}

0 comments on commit 0f853f7

Please sign in to comment.