Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: commands exit without crashing the thread #763

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions app/Console/Commands/AddCentre.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,38 @@ public function handle()

switch (true) {
case (!$this->centreUser):
exit("Can't find that User.\n");
break;
$this->error("Can't find that User.");
return 1;
case (!$this->sponsor):
exit("Sponsor " .
$this->error("Sponsor " .
$this->argument('shortcode') .
" does not exist, exit without change.\n");
return 2;
case ($this->centre):
exit("Centre " .
$this->error("Centre " .
$this->centre->name .
", " .
$this->centre->prefix .
" exists, exit without change.\n");
return 3;
case (!in_array($this->argument('preference'), $this->preferences)):
exit("The preference " .
$this->error("The preference " .
$this->argument('preference') .
" does not exist, exit without change.\n");
return 4;
default:
// Check the centreuser is happy to proceed
if (!$this->warnUser()) {
exit("Exit without change.\n");
$this->error("Exit without change.\n");
return 5;
};
// Log the centreuser in.
Auth::login($this->centreUser);

// Did that work?
if (!Auth::check()) {
exit("Failed to login.\n");
$this->error("Failed to login.\n");
return 6;
};

$this->centre = new Centre([
Expand All @@ -116,7 +121,7 @@ public function handle()
$this->centre->sponsor()->associate($this->sponsor);
$this->centre->save();

exit("Done.\n");
$this->info("Done.\n");
}
}

Expand Down
15 changes: 9 additions & 6 deletions app/Console/Commands/AddSponsor.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,29 @@ public function handle()

switch (true) {
case (!$this->centreUser):
exit("Can't find that User.\n");
break;
$this->error("Can't find that User.\n");
return 1;
case ($this->sponsor):
exit("Sponsor " .
$this->error("Sponsor " .
$this->sponsor->name .
", " .
$this->sponsor->shortcode .
" exists, exit without change.\n");
return 2;

default:
// Check the centreuser is happy to proceed
if (!$this->warnUser()) {
exit("Exit without change.\n");
$this->error("Exit without change.\n");
return 3;
};
// Log the centreuser in.
Auth::login($this->centreUser);

// Did that work?
if (!Auth::check()) {
exit("Failed to login.\n");
$this->error("Failed to login.\n");
return 4;
};

$this->sponsor = new Sponsor([
Expand All @@ -89,7 +92,7 @@ public function handle()

$this->sponsor->save();

exit("Done.\n");
$this->info("Done.\n");
}
}

Expand Down
15 changes: 8 additions & 7 deletions app/Console/Commands/LegacyImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ public function handle()

switch (true) {
case (!$this->authUser):
exit("Can't find the AdminUser");
break;
$this->error("Can't find the AdminUser");
return 1;
case (!$this->sponsor):
exit("Can't find the Sponsor");
break;
$this->error("Can't find the Sponsor");
return 2;
case (empty($this->codes)):
exit("There are no codes");
break;
$this->error("There are no codes");
return 3;
default:
if (!$this->warnUser()) {
exit("Failed to log in\n");
$this->error("Failed to log in\n");
return 4;
};
Auth::login($this->authUser);
$bar = $this->output->createProgressBar(count($this->codes));
Expand Down
85 changes: 38 additions & 47 deletions app/Console/Commands/MvlExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,45 @@ class MvlExport extends Command
/**
* Execute the console command.
*/
public function handle(): void
public function handle(): int
{
$this->initSettings();
$from = $this->option('from');
if ($from) {
try {
$this->startDate = Carbon::createFromFormat('d/m/Y', $from, 'UTC');
} catch (InvalidFormatException $exception) {
// Not a date
$this->error("From date was not a valid date: " . $from);
return 1;
}
} else {
// We were going to start this financial year
// $this->startDate = $this->getStartOfThisFinancialYear()->format('d/m/Y');
$this->startDate = Carbon::createFromFormat('d/m/Y', "01/01/1970", 'UTC');
}

$to = $this->option('to');
if ($to) {
try {
$this->endDate = Carbon::createFromFormat('d/m/Y', $to, 'UTC');
} catch (InvalidFormatException $exception) {
// Not a date
$this->error("To date was not a valid date: " . $to);
return 2;
}
} else {
// We were going to use till now
// $this->endDate = Carbon::now()->format('d/m/Y');
$this->endDate = Carbon::createFromFormat('d/m/Y', "31/08/2023", 'UTC');
}

$chunkSize = $this->option("chunk-size");
if ($chunkSize) {
$this->chunkSize = intval($chunkSize);
if ($this->chunkSize === 0) {
$this->error("Chunk size does not seem to be a valid int: " . $chunkSize);
}
}

$this->info(
sprintf(
Expand Down Expand Up @@ -124,49 +160,4 @@ public function handle(): void
$offset += $this->chunkSize;
}
}

/**
* @return void
*/
public function initSettings(): void
{
$from = $this->option('from');
if ($from) {
try {
$this->startDate = Carbon::createFromFormat('d/m/Y', $from, 'UTC');
} catch (InvalidFormatException $exception) {
// Not a date
$this->error("From date was not a valid date: " . $from);
exit(1);
}
} else {
// We were going to start this financial year
// $this->startDate = $this->getStartOfThisFinancialYear()->format('d/m/Y');
$this->startDate = Carbon::createFromFormat('d/m/Y', "01/01/1970", 'UTC');
}

$to = $this->option('to');
if ($to) {
try {
$this->endDate = Carbon::createFromFormat('d/m/Y', $to, 'UTC');
} catch (InvalidFormatException $exception) {
// Not a date
$this->error("To date was not a valid date: " . $to);
exit(1);
}
} else {
// We were going to use till now
// $this->endDate = Carbon::now()->format('d/m/Y');
$this->endDate = Carbon::createFromFormat('d/m/Y', "31/08/2023", 'UTC');
}

$chunkSize = $this->option("chunk-size");
if ($chunkSize) {
$this->chunkSize = intval($chunkSize);
if ($this->chunkSize === 0) {
$this->error("Chunk size does not seem to be a valid int: " . $chunkSize);
}
}

}
}
Loading