New code for updated import routine.

This commit is contained in:
James Cole
2018-05-03 17:23:16 +02:00
parent c5142aeba5
commit 6bddb63b45
20 changed files with 843 additions and 47 deletions

View File

@@ -85,7 +85,7 @@ class JobConfigurationController extends Controller
Log::debug('Job needs no config, is ready to run!');
$this->repository->updateStatus($importJob ,'ready_to_run');
return redirect(route('import.job.status.index', [$importProvider->key]));
return redirect(route('import.job.status.index', [$importJob->key]));
}
// create configuration class:

View File

@@ -22,14 +22,19 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Import;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Import\Routine\RoutineInterface;
use FireflyIII\Import\Storage\ImportArrayStorage;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Log;
use Symfony\Component\Debug\Exception\FatalThrowableError;
/**
* Class JobStatusController
@@ -82,14 +87,21 @@ class JobStatusController extends Controller
}
/**
* @param ImportJob $job
* @param ImportJob $importJob
*
* @return JsonResponse
*/
public function json(ImportJob $importJob): JsonResponse
{
$json = [
'status' => $importJob->status,
$extendedStatus = $importJob->extended_status;
$json = [
'status' => $importJob->status,
'errors' => $importJob->errors,
'count' => count($importJob->transactions),
'tag_id' => $importJob->tag_id,
'tag_name' => null === $importJob->tag_id ? null : $importJob->tag->tag,
'journals' => $extendedStatus['count'] ?? 0,
'journals_text' => trans_choice('import.status_with_count', $extendedStatus['count'] ?? 0),
];
return response()->json($json);
@@ -104,11 +116,11 @@ class JobStatusController extends Controller
public function start(ImportJob $importJob): JsonResponse
{
// catch impossible status:
$allowed = ['ready_to_run'];
$allowed = ['ready_to_run', 'need_job_config'];
if (null !== $importJob && !in_array($importJob->status, $allowed)) {
Log::error('Job is not ready.');
session()->flash('error', trans('import.bad_job_status'));
return redirect(route('import.index'));
return response()->json(['status' => 'NOK', 'message' => 'JobStatusController::start expects state "ready_to_run".']);
}
$importProvider = $importJob->provider;
@@ -122,6 +134,21 @@ class JobStatusController extends Controller
// if the job is set to "provider_finished", we should be able to store transactions
// generated by the provider.
// otherwise, just continue.
if ($importJob->status === 'provider_finished') {
try {
$this->importFromJob($importJob);
} catch (FireflyException $e) {
$message = 'The import storage routine crashed: ' . $e->getMessage();
Log::error($message);
Log::error($e->getTraceAsString());
// set job errored out:
$this->repository->setStatus($importJob, 'error');
return response()->json(['status' => 'NOK', 'message' => $message]);
}
}
// set job to be running:
$this->repository->setStatus($importJob, 'running');
@@ -144,6 +171,67 @@ class JobStatusController extends Controller
// expect nothing from routine, just return OK to user.
return response()->json(['status' => 'OK', 'message' => 'stage_finished']);
}
/**
* @param ImportJob $job
*
* @return JsonResponse
* @throws FireflyException
*/
public function store(ImportJob $importJob): JsonResponse
{
// catch impossible status:
$allowed = ['provider_finished', 'storing_data']; // todo remove storing data.
if (null !== $importJob && !in_array($importJob->status, $allowed)) {
Log::error('Job is not ready.');
return response()->json(['status' => 'NOK', 'message' => 'JobStatusController::start expects state "provider_finished".']);
}
// set job to be storing data:
$this->repository->setStatus($importJob, 'storing_data');
try {
$this->importFromJob($importJob);
} catch (FireflyException $e) {
$message = 'The import storage routine crashed: ' . $e->getMessage();
Log::error($message);
Log::error($e->getTraceAsString());
// set job errored out:
$this->repository->setStatus($importJob, 'error');
return response()->json(['status' => 'NOK', 'message' => $message]);
}
// set job to be finished.
$this->repository->setStatus($importJob, 'finished');
// expect nothing from routine, just return OK to user.
return response()->json(['status' => 'OK', 'message' => 'storage_finished']);
}
/**
* @param ImportJob $importJob
*
* @throws FireflyException
*/
private function importFromJob(ImportJob $importJob): void
{
try {
$storage = new ImportArrayStorage($importJob);
$journals = $storage->store();
$extendedStatus = $importJob->extended_status;
$extendedStatus['count'] = $journals->count();
$this->repository->setExtendedStatus($importJob, $extendedStatus);
} catch (FireflyException|Exception|FatalThrowableError $e) {
throw new FireflyException($e->getMessage());
}
}
// /**