Create a fake routine, check for its progress.

This commit is contained in:
James Cole
2018-04-30 06:37:29 +02:00
parent 1c2089b8a3
commit cd75224cdd
9 changed files with 207 additions and 6 deletions

View File

@@ -27,6 +27,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Middleware\IsDemoUser; use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Import\Routine\RoutineInterface; use FireflyIII\Import\Routine\RoutineInterface;
use FireflyIII\Models\ImportJob; use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Log; use Log;
@@ -35,6 +36,9 @@ use Log;
*/ */
class JobStatusController extends Controller class JobStatusController extends Controller
{ {
/** @var ImportJobRepositoryInterface */
private $repository;
/** /**
* *
*/ */
@@ -46,6 +50,7 @@ class JobStatusController extends Controller
function ($request, $next) { function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-archive'); app('view')->share('mainTitleIcon', 'fa-archive');
app('view')->share('title', trans('firefly.import_index_title')); app('view')->share('title', trans('firefly.import_index_title'));
$this->repository = app(ImportJobRepositoryInterface::class);
return $next($request); return $next($request);
} }
@@ -104,6 +109,8 @@ class JobStatusController extends Controller
if (null === $className || !class_exists($className)) { if (null === $className || !class_exists($className)) {
return response()->json(['status' => 'NOK', 'message' => sprintf('Cannot find import routine class for job of type "%s".', $importProvider)]); return response()->json(['status' => 'NOK', 'message' => sprintf('Cannot find import routine class for job of type "%s".', $importProvider)]);
} }
// set job to be running:
$this->repository->setStatus($job, 'running');
/** @var RoutineInterface $routine */ /** @var RoutineInterface $routine */
$routine = app($className); $routine = app($className);
@@ -115,9 +122,15 @@ class JobStatusController extends Controller
Log::error($message); Log::error($message);
Log::error($e->getTraceAsString()); Log::error($e->getTraceAsString());
// set job errored out:
$this->repository->setStatus($job, 'errored');
return response()->json(['status' => 'NOK', 'message' => $message]); return response()->json(['status' => 'NOK', 'message' => $message]);
} }
// set job finished this step:
$this->repository->setStatus($job, 'stage_finished');
// expect nothing from routine, just return OK to user. // expect nothing from routine, just return OK to user.
return response()->json(['status' => 'OK', 'message' => 'finished']); return response()->json(['status' => 'OK', 'message' => 'finished']);
} }

View File

@@ -0,0 +1,83 @@
<?php
/**
* FakeRoutine.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Routine;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Support\Import\Routine\Fake\StageNewHandler;
/**
* Class FakeRoutine
*/
class FakeRoutine implements RoutineInterface
{
/** @var ImportJob */
private $job;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* FakeRoutine constructor.
*/
public function __construct()
{
$this->repository = app(ImportJobRepositoryInterface::class);
}
/**
* Fake import routine has three stages:
*
* "new": will quietly log gibberish for 15 seconds, then switch to stage "ahoy"
* unless "ahoy" has been done already. If so, jump to stage "final".
* "ahoy": will log some nonsense and then drop job into "need_extra_config" to force it back to the job config routine.
* "final": will do some logging, sleep for 10 seconds and then finish. Generates 5 random transactions.
*
* @return bool
* @throws FireflyException
*/
public function run(): void
{
switch ($this->job->stage) {
default:
throw new FireflyException(sprintf('Fake routine cannot handle stage "%s".', $this->job->stage));
case 'new':
$handler = new StageNewHandler;
$handler->run();
}
}
/**
* @param ImportJob $job
*
* @return mixed
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
$this->repository->setUser($job->user);
}
}

View File

@@ -334,6 +334,20 @@ class ImportJobRepository implements ImportJobRepositoryInterface
return $job; return $job;
} }
/**
* @param ImportJob $job
* @param string $stage
*
* @return ImportJob
*/
public function setStage(ImportJob $job, string $stage): ImportJob
{
$job->stage = $stage;
$job->save();
return $job;
}
/** /**
* @param ImportJob $job * @param ImportJob $job
* @param string $status * @param string $status

View File

@@ -145,6 +145,14 @@ interface ImportJobRepositoryInterface
*/ */
public function setStatus(ImportJob $job, string $status): ImportJob; public function setStatus(ImportJob $job, string $status): ImportJob;
/**
* @param ImportJob $job
* @param string $stage
*
* @return ImportJob
*/
public function setStage(ImportJob $job, string $stage): ImportJob;
/** /**
* @param ImportJob $job * @param ImportJob $job
* @param int $steps * @param int $steps

View File

@@ -0,0 +1,44 @@
<?php
/**
* StageNewHandler.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Import\Routine\Fake;
use Log;
/**
* Class StageNewHandler
*/
class StageNewHandler
{
/**
*
*/
public function run(): void
{
for ($i = 0; $i < 15; $i++) {
Log::debug(sprintf('Am now in stage new hander, sleeping... (%d)', $i));
sleep(1);
}
}
}

View File

@@ -11,6 +11,7 @@ use FireflyIII\Import\Prerequisites\FakePrerequisites;
use FireflyIII\Import\Prerequisites\FilePrerequisites; use FireflyIII\Import\Prerequisites\FilePrerequisites;
use FireflyIII\Import\Prerequisites\SpectrePrerequisites; use FireflyIII\Import\Prerequisites\SpectrePrerequisites;
use FireflyIII\Import\Routine\BunqRoutine; use FireflyIII\Import\Routine\BunqRoutine;
use FireflyIII\Import\Routine\FakeRoutine;
use FireflyIII\Import\Routine\FileRoutine; use FireflyIII\Import\Routine\FileRoutine;
use FireflyIII\Import\Routine\SpectreRoutine; use FireflyIII\Import\Routine\SpectreRoutine;
@@ -59,7 +60,7 @@ return [
'file' => FilePrerequisites::class, 'file' => FilePrerequisites::class,
'bunq' => BunqPrerequisites::class, 'bunq' => BunqPrerequisites::class,
'spectre' => SpectrePrerequisites::class, 'spectre' => SpectrePrerequisites::class,
'plaid' => 'FireflyIII\Import\Prerequisites\PlaidPrerequisites', 'plaid' => false,
'quovo' => false, 'quovo' => false,
'yodlee' => false, 'yodlee' => false,
], ],
@@ -77,13 +78,18 @@ return [
'file' => FileConfigurator::class, 'file' => FileConfigurator::class,
'bunq' => BunqConfigurator::class, 'bunq' => BunqConfigurator::class,
'spectre' => SpectreConfigurator::class, 'spectre' => SpectreConfigurator::class,
'plaid' => 'FireflyIII\Import\Configuration\PlaidConfigurator', 'plaid' => false,
'quovo' => false,
'yodlee' => false,
], ],
'routine' => [ 'routine' => [
'fake' => FakeRoutine::class,
'file' => FileRoutine::class, 'file' => FileRoutine::class,
'bunq' => BunqRoutine::class, 'bunq' => BunqRoutine::class,
'spectre' => SpectreRoutine::class, 'spectre' => SpectreRoutine::class,
'plaid' => 'FireflyIII\Import\Routine\PlaidRoutine', 'plaid' => false,
'quovo' => false,
'yodlee' => false,
], ],
'options' => [ 'options' => [

View File

@@ -52,8 +52,10 @@ function reportOnJobStatus(data) {
case "ready_to_run": case "ready_to_run":
startJob(); startJob();
checkOnJob(); checkOnJob();
break;
case "running":
showProgressBox();
checkOnJob();
break; break;
default: default:
console.error('Cannot handle status ' + data.status); console.error('Cannot handle status ' + data.status);
@@ -108,6 +110,17 @@ function reportFailure(xhr, status, error) {
// show error box. // show error box.
} }
function showProgressBox() {
// hide fatal error box:
$('.fatal_error').hide();
// hide initial status box:
$('.status_initial').hide();
// show running box:
$('.status_running').show();
}
/** /**
* Function is called when the job could not be started. * Function is called when the job could not be started.
* *

View File

@@ -42,6 +42,26 @@
</div> </div>
</div> </div>
</div> </div>
{# box to show when job is running ... #}
<div class="row status_running statusbox" style="display: none;">
<div class="col-lg-8 col-lg-offset-2 col-md-12 col-sm-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title" id="import-status-title">{{ trans('import.status_running_title') }}</h3>
</div>
<div class="box-body">
<div id="import-status-holder">
<div class="progress">
<div class="progress-bar progress-bar-info active progress-bar-striped" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: 100%">
<span class="sr-only">Running...</span>
</div>
</div>
<p id="import-status-txt">Some text here</p>
</div>
</div>
</div>
</div>
</div>
{# Box for when the job is ready to start {# Box for when the job is ready to start
<div class="row status_configured statusbox" style="display:none;"> <div class="row status_configured statusbox" style="display:none;">

View File

@@ -461,7 +461,7 @@ Route::group(
Route::get('job/json/{importJob}', ['uses' => 'Import\JobStatusController@json', 'as' => 'job.status.json']); Route::get('job/json/{importJob}', ['uses' => 'Import\JobStatusController@json', 'as' => 'job.status.json']);
// start the job! // start the job!
Route::post('job/start/{importJob}', ['uses' => 'Import\JobStatusController@start', 'as' => 'job.start']); Route::any('job/start/{importJob}', ['uses' => 'Import\JobStatusController@start', 'as' => 'job.start']);
// import method prerequisites: // import method prerequisites:
# #