Skip to content

Commit 652ead5

Browse files
committed
logs upload command
Signed-off-by: romanetar <[email protected]>
1 parent fb40fee commit 652ead5

File tree

4 files changed

+170
-50
lines changed

4 files changed

+170
-50
lines changed

app/Console/Commands/LogsUploader.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php namespace App\Console\Commands;
2+
/**
3+
* Copyright 2023 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
use Carbon\Carbon;
16+
use Carbon\CarbonPeriod;
17+
use Exception;
18+
use Illuminate\Console\Command;
19+
use Illuminate\Support\Facades\Log;
20+
use Illuminate\Support\Facades\Storage;
21+
22+
/**
23+
* Class LogsUploader
24+
* @package App\Console\Commands
25+
*/
26+
final class LogsUploader extends Command
27+
{
28+
/**
29+
* The console command name.
30+
*
31+
* @var string
32+
*/
33+
protected $name = 'management:logs-uploader';
34+
35+
/**
36+
* The name and signature of the console command.
37+
*
38+
* @var string
39+
*/
40+
protected $signature = 'management:logs-uploader';
41+
42+
43+
/**
44+
* The console command description.
45+
*
46+
* @var string
47+
*/
48+
protected $description = 'Upload local logs to external storage';
49+
50+
private function formatFileName(string $date_str) {
51+
return "laravel-{$date_str}.log";
52+
}
53+
54+
/**
55+
* Execute the console command.
56+
*
57+
* @return mixed
58+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
59+
*/
60+
public function handle()
61+
{
62+
try {
63+
$date_format = 'Y-m-d';
64+
$ui_file_name = 'uploads_info';
65+
$pending_uploads = [];
66+
$yesterday = Carbon::yesterday('UTC');
67+
68+
$logs_fs = Storage::disk('logs');
69+
if ($logs_fs->exists($ui_file_name)) {
70+
$uploads_info = explode(PHP_EOL, $logs_fs->get($ui_file_name));
71+
sort($uploads_info);
72+
$first_date_str = $uploads_info[0];
73+
$date_from = Carbon::createFromFormat($date_format, $first_date_str);
74+
//get upload gaps
75+
$period = CarbonPeriod::create($date_from, $yesterday);
76+
77+
foreach ($period as $date) {
78+
$date_str = $date->format($date_format);
79+
if (!in_array($date_str, $uploads_info)) {
80+
$pending_uploads[] = $this->formatFileName($date_str);
81+
}
82+
}
83+
} else {
84+
$pending_uploads[] = $this->formatFileName($yesterday->format($date_format));
85+
}
86+
87+
foreach ($pending_uploads as $pending_upload) {
88+
$logs_fs->append($ui_file_name, $pending_upload);
89+
}
90+
91+
//Storage::disk('logs_s3')->put($log_name, $content);
92+
} catch (Exception $ex) {
93+
Log::error($ex);
94+
}
95+
}
96+
}

app/Console/Kernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Kernel extends ConsoleKernel
4949
\App\Console\Commands\PresentationMediaUploadsRegenerateTemporalLinks::class,
5050
\App\Console\Commands\PurgeAuditLogCommand::class,
5151
\App\Console\Commands\SummitBadgesQREncryptor::class,
52+
\App\Console\Commands\LogsUploader::class,
5253
];
5354

5455
/**
@@ -98,5 +99,8 @@ protected function schedule(Schedule $schedule)
9899
}
99100

100101
$schedule->command('summit:presentations-regenerate-media-uploads-temporal-public-urls')->everyMinute()->withoutOverlapping()->onOneServer();
102+
103+
// logs upload
104+
$schedule->command('management:logs-uploader')->dailyAt("07:00")->timezone('UTC')->withoutOverlapping()->onOneServer();
101105
}
102106
}

config/filesystems.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
'root' => storage_path('app'),
4949
],
5050

51+
'logs' => [
52+
'driver' => 'local',
53+
'root' => storage_path('logs'),
54+
],
55+
5156
'public' => [
5257
'driver' => 'local',
5358
'root' => storage_path('app/public'),
@@ -92,6 +97,15 @@
9297
'endpoint' => env('AWS_ENDPOINT_ASSETS'),
9398
],
9499

100+
'logs_s3' => [
101+
'driver' => 's3',
102+
'key' => env('AWS_ACCESS_KEY_ID_LOGS'),
103+
'secret' => env('AWS_SECRET_ACCESS_KEY_LOGS'),
104+
'region' => env('AWS_DEFAULT_REGION_LOGS'),
105+
'bucket' => env('AWS_BUCKET_LOGS'),
106+
'endpoint' => env('AWS_ENDPOINT_LOGS'),
107+
],
108+
95109
'static_images_s3' => [
96110
'driver' => 's3',
97111
'key' => env('AWS_ACCESS_KEY_ID_STATIC_IMAGES'),

tests/OAuth2SummitOrdersApiTest.php

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
**/
1414

1515
use Illuminate\Support\Facades\App;
16+
use LaravelDoctrine\ORM\Facades\EntityManager;
1617
use models\summit\PaymentGatewayProfileFactory;
1718
use models\summit\IPaymentConstants;
1819
use App\Models\Foundation\Summit\Factories\SummitTicketTypeFactory;
1920
use App\Models\Foundation\Summit\Factories\SummitBadgeTypeFactory;
21+
use models\summit\Summit;
2022
use services\model\ISummitService;
2123

2224
/**
@@ -58,54 +60,54 @@ final class OAuth2SummitOrdersApiTest extends ProtectedApiTest
5860

5961
use InsertOrdersTestData;
6062

61-
protected function setUp():void
62-
{
63-
parent::setUp();
64-
self::$test_secret_key = env('TEST_STRIPE_SECRET_KEY');
65-
self::$test_public_key = env('TEST_STRIPE_PUBLISHABLE_KEY');
66-
self::$live_secret_key = env('LIVE_STRIPE_SECRET_KEY');
67-
self::$live_public_key = env('LIVE_STRIPE_PUBLISHABLE_KEY');
68-
69-
self::insertSummitTestData();
70-
self::InsertOrdersTestData();
71-
// build payment profile and attach to summit
72-
self::$profile = PaymentGatewayProfileFactory::build(IPaymentConstants::ProviderStripe, [
73-
'application_type' => IPaymentConstants::ApplicationTypeRegistration,
74-
'is_test_mode' => true,
75-
'test_publishable_key' => self::$test_public_key,
76-
'test_secret_key' => self::$test_secret_key,
77-
'is_active' => false,
78-
]);
79-
80-
// build default badge type
81-
82-
$defaultBadge = SummitBadgeTypeFactory::build([
83-
'name' => 'DEFAULT',
84-
'is_default' => true,
85-
]);
86-
87-
// build ticket type
88-
89-
self::$ticketType = SummitTicketTypeFactory::build(self::$summit, [
90-
'name' => 'TICKET_1',
91-
'cost' => 100,
92-
'quantity_2_sell' => 1000,
93-
]);
94-
95-
self::$summit->addPaymentProfile(self::$profile);
96-
self::$summit->addBadgeType($defaultBadge);
97-
self::$summit->addTicketType(self::$ticketType);
98-
99-
self::$em->persist(self::$summit);
100-
self::$em->flush();
101-
102-
}
103-
104-
protected function tearDown():void
105-
{
106-
self::clearSummitTestData();
107-
parent::tearDown();
108-
}
63+
// protected function setUp():void
64+
// {
65+
// parent::setUp();
66+
// self::$test_secret_key = env('TEST_STRIPE_SECRET_KEY');
67+
// self::$test_public_key = env('TEST_STRIPE_PUBLISHABLE_KEY');
68+
// self::$live_secret_key = env('LIVE_STRIPE_SECRET_KEY');
69+
// self::$live_public_key = env('LIVE_STRIPE_PUBLISHABLE_KEY');
70+
//
71+
// self::insertSummitTestData();
72+
// self::InsertOrdersTestData();
73+
// // build payment profile and attach to summit
74+
// self::$profile = PaymentGatewayProfileFactory::build(IPaymentConstants::ProviderStripe, [
75+
// 'application_type' => IPaymentConstants::ApplicationTypeRegistration,
76+
// 'is_test_mode' => true,
77+
// 'test_publishable_key' => self::$test_public_key,
78+
// 'test_secret_key' => self::$test_secret_key,
79+
// 'is_active' => false,
80+
// ]);
81+
//
82+
// // build default badge type
83+
//
84+
// $defaultBadge = SummitBadgeTypeFactory::build([
85+
// 'name' => 'DEFAULT',
86+
// 'is_default' => true,
87+
// ]);
88+
//
89+
// // build ticket type
90+
//
91+
// self::$ticketType = SummitTicketTypeFactory::build(self::$summit, [
92+
// 'name' => 'TICKET_1',
93+
// 'cost' => 100,
94+
// 'quantity_2_sell' => 1000,
95+
// ]);
96+
//
97+
// self::$summit->addPaymentProfile(self::$profile);
98+
// self::$summit->addBadgeType($defaultBadge);
99+
// self::$summit->addTicketType(self::$ticketType);
100+
//
101+
// self::$em->persist(self::$summit);
102+
// self::$em->flush();
103+
//
104+
// }
105+
//
106+
// protected function tearDown():void
107+
// {
108+
// self::clearSummitTestData();
109+
// parent::tearDown();
110+
// }
109111

110112
/**
111113
* @return mixed
@@ -380,11 +382,14 @@ public function testReserveWithSummit(){
380382

381383
$res = memory_get_peak_usage(true);
382384

385+
$summit_repository = EntityManager::getRepository(Summit::class);
386+
self::$summit = $summit_repository->find(3685);
387+
383388
$summitId = self::$summit->getId();
384389
$companyId = 5;
385390

386391
$service = App::make(ISummitService::class);
387-
$service->addCompany($summitId, $companyId);
392+
//$service->addCompany($summitId, $companyId);
388393
$company = self::$summit->getRegistrationCompanyById($companyId);
389394

390395
$params = [
@@ -398,7 +403,8 @@ public function testReserveWithSummit(){
398403
"owner_company" => $company->getName(),
399404
"owner_company_id" => $company->getId(),
400405
"tickets" => [
401-
["type_id" => self::$ticketType->getId()],
406+
//["type_id" => self::$ticketType->getId()],
407+
["type_id" => 3080],
402408
]
403409
];
404410

0 commit comments

Comments
 (0)