Skip to content

Commit

Permalink
feat: improve reliability of pingversion (#5723)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Dec 27, 2021
1 parent 8ce3550 commit 0c791f6
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 100 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ jobs:
- name: Prepare environment
run: |
cp scripts/ci/.env.${{ matrix.connection }} .env
touch config/.version config/.release config/.commit
echo 'v2.17.0' > config/.version
touch config/.release config/.commit
mkdir -p public/js public/css results/coverage
{\
echo "{"; \
Expand Down Expand Up @@ -212,7 +213,8 @@ jobs:
- name: Prepare environment
run: |
cp scripts/ci/.env.${{ matrix.connection }} .env
touch config/.version config/.release config/.commit
echo 'v2.17.0' > config/.version
touch config/.release config/.commit
mkdir -p results/coverage results/cov results/console
chmod -R 777 storage bootstrap/cache
Expand Down
69 changes: 25 additions & 44 deletions app/Console/Commands/PingVersionServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace App\Console\Commands;

use GuzzleHttp\Client;
use function Safe\json_decode;
use PharIo\Version\Version;
use App\Models\Contact\Contact;
use Illuminate\Console\Command;
use App\Models\Instance\Instance;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Http\Client\RequestException;
use Symfony\Component\Console\Output\OutputInterface;

class PingVersionServer extends Command
Expand Down Expand Up @@ -43,65 +45,44 @@ public function handle()
}

$instance = Instance::first();
$instance->current_version = config('monica.app_version');

// Prepare the json to query version.monicahq.com
$json = [
'uuid' => $instance->uuid,
'version' => $instance->current_version,
'contacts' => Contact::count(),
];

$data = [
'uuid' => $instance->uuid,
'version' => $instance->current_version,
'contacts' => Contact::count(),
];

// Send the JSON
// Query version.monicahq.com
try {
$this->log('Call url:'.config('monica.weekly_ping_server_url'));
$client = new Client();
$response = $client->post(config('monica.weekly_ping_server_url'), [
'json' => $data,
]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
$this->log('ConnectException...');

return;
} catch (\GuzzleHttp\Exception\TransferException $e) {
$this->log('TransferException...');
$this->log('Call url: '.config('monica.weekly_ping_server_url'));
$response = Http::acceptJson()
->post(config('monica.weekly_ping_server_url'), [
'uuid' => $instance->uuid,
'version' => $instance->current_version,
'contacts' => Contact::count(),
])
->throw();
} catch (RequestException $e) {
$this->error('Error calling "'.config('monica.weekly_ping_server_url').'": '.$e->getMessage());
Log::error(__CLASS__.' Error calling "'.config('monica.weekly_ping_server_url').'": '.$e->getMessage(), [$e]);

return;
}

// Receive the JSON
$json = json_decode($response->getBody(), true);

if (json_last_error() !== JSON_ERROR_NONE) {
// JSON is invalid
// The function json_last_error returns the last error occurred during the JSON encoding and decoding
$this->log('json error...');
$json = $response->json();

return;
}
$this->log('instance version: '.$instance->current_version);
$this->log('current version: '.$json['latest_version']);

// make sure the JSON has all the fields we need
if (! isset($json['latest_version']) || ! isset($json['new_version']) || ! isset($json['number_of_versions_since_user_version'])) {
return;
}
$latestVersion = new Version($json['latest_version']);
$currentVersion = new Version($instance->current_version);

$this->log('instance version:'.$instance->current_version);
$this->log('current version:'.$json['latest_version']);
if ($json['latest_version'] != $instance->current_version) {
if ($latestVersion > $currentVersion) {
$instance->latest_version = $json['latest_version'];
$instance->latest_release_notes = $json['notes'];
$instance->number_of_versions_since_current_version = $json['number_of_versions_since_user_version'];
$instance->save();
} else {
$instance->latest_release_notes = null;
$instance->number_of_versions_since_current_version = null;
$instance->save();
}

$instance->save();
}

public function log($string)
Expand Down
7 changes: 6 additions & 1 deletion app/Http/Middleware/CheckVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Middleware;

use Closure;
use PharIo\Version\Version;
use App\Models\Instance\Instance;

class CheckVersion
Expand All @@ -18,7 +19,11 @@ public function handle($request, Closure $next)
{
$instance = Instance::first();

if ($instance->latest_version == config('monica.app_version')) {
$appVersion = new Version(config('monica.app_version'));
$latestVersion = new Version($instance->latest_version ?? '0.0.0');
$currentVersion = new Version($instance->current_version ?? '0.0.0');

if ($latestVersion == $appVersion && $currentVersion != $latestVersion) {

// The instance has been updated to the latest version. We reset
// the ping data.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"monicahq/laravel-cloudflare": "^1.0",
"monicahq/laravel-sabre": "^1.2",
"ok/ipstack-client": "^1.2",
"phar-io/version": "^3.1",
"pragmarx/countries-laravel": "^0",
"pragmarx/google2fa": "^8.0",
"pragmarx/google2fa-laravel": "^1.3",
Expand Down
104 changes: 52 additions & 52 deletions composer.lock

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

6 changes: 5 additions & 1 deletion database/factories/InstanceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
});

$factory->define(App\Models\Instance\Instance::class, function (Faker\Generator $faker) {
return [];
return [
'uuid' => $faker->uuid,
'latest_version' => '1.0.0',
'current_version' => '1.0.0',
];
});

$factory->define(App\Models\Instance\Emotion\Emotion::class, function (Faker\Generator $faker) {
Expand Down
77 changes: 77 additions & 0 deletions tests/Commands/PingVersionServerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Tests\Commands;

use Tests\TestCase;
use App\Models\Instance\Instance;
use Illuminate\Support\Facades\Http;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class PingVersionServerTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_send_ping()
{
config(['monica.weekly_ping_server_url' => 'https://version.test/ping']);
config(['monica.app_version' => '2.9.0']);

Instance::all()->each(function ($instance) {
$instance->delete();
});
$instance = factory(Instance::class)->create();

$ret = [
'new_version' => true,
'latest_version' => '3.1.0',
'number_of_versions_since_user_version' => 2,
'notes' => 'notes',
];

Http::fake([
'https://version.test/*' => Http::response($ret, 200),
]);

$this->artisan('monica:ping');

$instance->refresh();

$this->assertEquals('3.1.0', $instance->latest_version);
$this->assertEquals('notes', $instance->latest_release_notes);
$this->assertEquals(2, $instance->number_of_versions_since_current_version);
}

/** @test */
public function it_clear_instance()
{
config(['monica.weekly_ping_server_url' => 'https://version.test/ping']);
config(['monica.app_version' => '3.1.0']);

Instance::all()->each(function ($instance) {
$instance->delete();
});
$instance = factory(Instance::class)->create([
'latest_version' => '3.1.0',
]);

$ret = [
'new_version' => false,
'latest_version' => '2.9.0',
'number_of_versions_since_user_version' => 0,
'notes' => '',
];

Http::fake([
'https://version.test/*' => Http::response($ret, 200),
]);

$this->artisan('monica:ping');

$instance->refresh();

$this->assertEquals('3.1.0', $instance->latest_version);
$this->assertNull($instance->latest_release_notes);
$this->assertNull($instance->number_of_versions_since_current_version);
}
}

0 comments on commit 0c791f6

Please sign in to comment.