Skip to content
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
52 changes: 38 additions & 14 deletions app/Jobs/ProcessPolydockAppInstanceJobs/BaseJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,52 @@ abstract class BaseJob implements ShouldQueue
/**
* Create a new job instance.
*/
public function __construct(int $appInstanceId) {
public function __construct(int $appInstanceId)
{
$this->appInstanceId = $appInstanceId;
}

public function getPolydockJobId()
{
$appInstance = PolydockAppInstance::find($this->appInstanceId)->refresh();
if(!$appInstance) {

if (!$appInstance) {
Log::error('Failed to process PolydockAppInstance - not found', [
'app_instance_id' => $this->appInstanceId,
'job_type' => class_basename(static::class)
]);

throw new \Exception('Failed to process PolydockAppInstance ' . $this->appInstanceId . ' - not found');
}
$uniqueId = "app-instance-{$appInstance->id}-job-".class_basename(static::class);

$uniqueId = "app-instance-{$appInstance->id}-job-" . class_basename(static::class);

return $uniqueId;
}

public function failed(\Throwable $exception): void
{
Log::error('Job failed: ' . class_basename(static::class), [
'app_instance_id' => $this->appInstanceId ?? null,
'error' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
]);

try {
$appInstance = $this->appInstance ?? PolydockAppInstance::find($this->appInstanceId);
if ($appInstance) {
$message = "Failed processing with the following - " . $exception->getMessage();
$appInstance->logLine("error", $message);
}
} catch (\Throwable $e) {
// Ensure the failed handler never throws
Log::error('Error while running job failed() handler', [
'original_error' => $exception->getMessage(),
'handler_error' => $e->getMessage(),
]);
}
}

/**
* Get the middleware the job should pass through.
*
Expand All @@ -66,15 +90,15 @@ public function middleware()
public function polydockJobStart()
{
$this->appInstance = PolydockAppInstance::find($this->appInstanceId)->refresh();
if(!$this->appInstance) {
if (!$this->appInstance) {
Log::error('Failed to process PolydockAppInstance - not found', [
'app_instance_id' => $this->appInstanceId,
'job_type' => class_basename(static::class)
]);

throw new \Exception('Failed to process PolydockAppInstance ' . $this->appInstanceId . ' - not found');
}

$uniqueId = $this->getPolydockJobId();

Log::info('Starting to process PolydockAppInstance', [
Expand All @@ -83,20 +107,20 @@ public function polydockJobStart()
'store_app_id' => $this->appInstance->polydock_store_app_id,
'store_app_name' => $this->appInstance->storeApp->name,
'status' => $this->appInstance->status->value
]);
]);
}

public function polydockJobDone()
{
if(!$this->appInstance) {
if (!$this->appInstance) {
Log::error('Failed to process PolydockAppInstance - not found', [
'app_instance_id' => $this->appInstanceId,
'job_type' => class_basename(static::class)
]);

throw new \Exception('Failed to process PolydockAppInstance ' . $this->appInstanceId . ' - not found');
}

$uniqueId = $this->getPolydockJobId();

Log::info('Finished processing PolydockAppInstance', [
Expand All @@ -105,6 +129,6 @@ public function polydockJobDone()
'store_app_id' => $this->appInstance->polydock_store_app_id,
'store_app_name' => $this->appInstance->storeApp->name,
'status' => $this->appInstance->status->value
]);
]);
}
}
24 changes: 18 additions & 6 deletions app/PolydockEngine/Traits/PolydockEngineFunctionCallerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,34 @@ protected function processPolydockAppUsingFunction(PolydockAppInstance $appInsta
return true;
}
catch(PolydockAppInstanceStatusFlowException $e) {
$polydockApp->error($appFunctionName . ' failed - status flow exception', $outputContext + ['exception' => $e]);
$message = $appFunctionName . ' failed - status flow exception';
$context = $outputContext + ['exception' => $e];
$polydockApp->error($message, $context);
if($appInstance->getStatus() !== $failedStatus) {
$polydockApp->info('Forcing status to ' . $failedStatus->value, $outputContext);
$appInstance->setStatus($failedStatus)->save();
$appInstance->
logLine("error", $message, $context)->
setStatus($failedStatus)->save();
}
return false;
}
catch(PolydockEngineProcessPolydockAppInstanceException $e) {
$polydockApp->error($appFunctionName . ' failed - process exception', $outputContext + ['exception' => $e]);
$message = $appFunctionName . ' failed - process exception';
$context = $outputContext + ['exception' => $e];
$polydockApp->error( $message, $context);
if($appInstance->getStatus() !== $failedStatus) {
$polydockApp->info('Forcing status to ' . $failedStatus->value, $outputContext);
$appInstance->setStatus($failedStatus)->save();
$appInstance->
logLine("error", $message, $context)->
setStatus($failedStatus)->save();
}
} catch(Exception $e) {
$polydockApp->error($appFunctionName . ' failed - unknown exception', $outputContext + ['exception' => $e]);
$appInstance->setStatus($failedStatus)->save();
$message = $appFunctionName . ' failed - unknown exception';
$context = $outputContext + ['exception' => $e];
$polydockApp->error($message, $context);
$appInstance->
logLine("error", $message, $context)->
setStatus($failedStatus)->save();
}

return false;
Expand Down