Created
April 23, 2018 15:07
-
-
Save lorenzulrich/51d80adf5a75dc8c15f260bbaa00644d to your computer and use it in GitHub Desktop.
TraceableJob for Flow Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Visol\Foo\Controller; | |
/* | |
* This file is part of the Visol.Foo package. | |
*/ | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Mvc\Controller\ActionController; | |
use TYPO3\Flow\Mvc\View\JsonView; | |
use TYPO3\Flow\Reflection\ObjectAccess; | |
class AbstractApiController extends ActionController | |
{ | |
/** | |
* @var array | |
*/ | |
protected $supportedMediaTypes = array('application/json'); | |
/** | |
* @var array | |
*/ | |
protected $viewFormatToObjectNameMap = array('json' => JsonView::class); | |
/** | |
* @return \TYPO3\Flow\Mvc\View\JsonView | |
*/ | |
protected function getView() | |
{ | |
return $this->view; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Visol\Foo\Controller; | |
/* | |
* This file is part of the Visol.Foo package. | |
*/ | |
use TYPO3\Flow\Annotations as Flow; | |
use Visol\Foo\Domain\Model\TraceableJob; | |
use Flowpack\JobQueue\Common\Annotations as Job; | |
class ExampleApiController extends AbstractApiController | |
{ | |
/** | |
* @var \Visol\Foo\TraceableJobManager | |
* @Flow\Inject | |
*/ | |
protected $traceableJobManager; | |
/** | |
* Imports some data asynchronously | |
*/ | |
public function importData() | |
{ | |
$traceableJob = $this->traceableJobManager->create(); | |
$this->executeImportData($traceableJob); | |
$this->getView()->assign( | |
'value', | |
$traceableJob | |
); | |
} | |
/** | |
* Performs the data import | |
* | |
* This task is executed asynchronously | |
* | |
* @param TraceableJob $traceableJob | |
* @Job\Defer(queueName="my-queue") | |
*/ | |
public function executeImportData(TraceableJob $traceableJob) | |
{ | |
$success = $this->doSomething(); | |
if ($success) { | |
$this->traceableJobManager->success($traceableJob, 'Data imported.'); | |
} else { | |
$this->traceableJobManager->fail($traceableJob, 'Import failed.'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################ | |
# Traceable Job API | |
################################ | |
- | |
name: 'Visol.Foo :: TraceableJob :: Status' | |
uriPattern: 'api/job-status/{traceableJob}' | |
defaults: | |
'@package': 'Visol.Foo' | |
'@controller': 'TraceableJobApi' | |
'@action': 'show' | |
'@format': 'json' | |
httpMethods: ['GET'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Visol\Foo\Domain\Model; | |
/* | |
* This file is part of the Visol.Foo package. | |
*/ | |
use Doctrine\ORM\Mapping as ORM; | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Persistence\PersistenceManagerInterface; | |
/** | |
* @Flow\Entity | |
*/ | |
class TraceableJob | |
{ | |
/** | |
* @var string | |
* @Flow\identity | |
* @Flow\transient | |
*/ | |
protected $identifier; | |
/** | |
* @Flow\Inject | |
* @var PersistenceManagerInterface | |
*/ | |
protected $persistenceManager; | |
const STATUS_PENDING = 'pending'; | |
const STATUS_SUCCESS = 'success'; | |
const STATUS_FAILURE = 'failure'; | |
/** | |
* @var string | |
*/ | |
protected $status = self::STATUS_PENDING; | |
/** | |
* @var string | |
* @ORM\Column(nullable=true) | |
*/ | |
protected $message; | |
/** | |
* @var string | |
* @ORM\Column(nullable=true, type="text") | |
*/ | |
protected $result; | |
// TODO maybe add calling user and DateTime for logging purpose | |
/** | |
* @return string | |
*/ | |
public function getIdentifier() | |
{ | |
return $this->persistenceManager->getIdentifierByObject($this); | |
} | |
/** | |
* @return string | |
*/ | |
public function getStatus() | |
{ | |
return $this->status; | |
} | |
/** | |
* @param string $status | |
*/ | |
public function setStatus($status) | |
{ | |
$this->status = $status; | |
} | |
/** | |
* @return string | |
*/ | |
public function getMessage() | |
{ | |
return $this->message; | |
} | |
/** | |
* @param string $message | |
*/ | |
public function setMessage($message) | |
{ | |
$this->message = $message; | |
} | |
/** | |
* @return string | |
*/ | |
public function getResult() | |
{ | |
return $this->result; | |
} | |
/** | |
* @param string $result | |
*/ | |
public function setResult($result) | |
{ | |
$this->result = $result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Visol\Foo\Controller; | |
/* | |
* This file is part of the Visol.Foo package. | |
*/ | |
use TYPO3\Flow\Annotations as Flow; | |
use Visol\Foo\Domain\Model\TraceableJob; | |
class TraceableJobApiController extends AbstractApiController | |
{ | |
/** | |
* @param TraceableJob $traceableJob | |
*/ | |
public function showAction(TraceableJob $traceableJob) { | |
$this->getView()->assign('value', $traceableJob); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Visol\Foo; | |
/* | |
* This file is part of the Visol.Foo package. | |
*/ | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Persistence\PersistenceManagerInterface; | |
use Visol\Foo\Domain\Model\TraceableJob; | |
/** | |
* @Flow\Scope("singleton") | |
*/ | |
class TraceableJobFactory | |
{ | |
/** | |
* @var \Visol\Foo\Domain\Repository\TraceableJobRepository | |
* @Flow\Inject | |
*/ | |
protected $traceableJobRepository; | |
/** | |
* @var PersistenceManagerInterface | |
* @Flow\Inject | |
*/ | |
protected $persistenceManager; | |
/** | |
* Create and return a new TraceableJob | |
* | |
* @param string $status | |
* @return TraceableJob | |
* @throws \TYPO3\Flow\Persistence\Exception\IllegalObjectTypeException | |
*/ | |
public function create($status = TraceableJob::STATUS_PENDING) { | |
$traceableJob = new TraceableJob(); | |
$traceableJob->setStatus($status); | |
$this->traceableJobRepository->add($traceableJob); | |
$this->persistenceManager->whitelistObject($traceableJob); | |
$this->persistenceManager->persistAll($traceableJob); | |
return $traceableJob; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Visol\Foo; | |
/* | |
* This file is part of the Visol.Foo package. | |
*/ | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Persistence\PersistenceManagerInterface; | |
use Visol\Foo\Domain\Model\TraceableJob; | |
/** | |
* @Flow\Scope("singleton") | |
*/ | |
class TraceableJobManager | |
{ | |
/** | |
* @var \Visol\Foo\TraceableJobFactory | |
* @Flow\Inject | |
*/ | |
protected $traceableJobFactory; | |
/** | |
* @var \Visol\Foo\Domain\Repository\TraceableJobRepository | |
* @Flow\Inject | |
*/ | |
protected $traceableJobRepository; | |
/** | |
* @var PersistenceManagerInterface | |
* @Flow\Inject | |
*/ | |
protected $persistenceManager; | |
/** | |
* Create a traceable job | |
* | |
* @return TraceableJob | |
*/ | |
public function create() { | |
return $this->traceableJobFactory->create(); | |
} | |
/** | |
* Marks a job as successfully executed | |
* | |
* @param TraceableJob $traceableJob | |
* @param null $message | |
* @param null $result | |
* @throws \TYPO3\Flow\Persistence\Exception\IllegalObjectTypeException | |
*/ | |
public function success(TraceableJob $traceableJob, $message = null, $result = null) { | |
$traceableJob->setStatus(TraceableJob::STATUS_SUCCESS); | |
$traceableJob->setMessage($message); | |
$traceableJob->setResult($result); | |
$this->traceableJobRepository->update($traceableJob); | |
$this->persistenceManager->whitelistObject($traceableJob); | |
$this->persistenceManager->persistAll($traceableJob); | |
} | |
/** | |
* Marks a job as successfully executed | |
* | |
* @param TraceableJob $traceableJob | |
* @param null $message | |
* @throws \TYPO3\Flow\Persistence\Exception\IllegalObjectTypeException | |
*/ | |
public function fail(TraceableJob $traceableJob, $message = null) { | |
$traceableJob->setStatus(TraceableJob::STATUS_FAILURE); | |
$traceableJob->setMessage($message); | |
$this->traceableJobRepository->update($traceableJob); | |
$this->persistenceManager->whitelistObject($traceableJob); | |
$this->persistenceManager->persistAll($traceableJob); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Visol\Foo\Domain\Repository; | |
/* | |
* This file is part of the Visol.Foo package. | |
*/ | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Persistence\Repository; | |
/** | |
* @Flow\Scope("singleton") | |
*/ | |
class TraceableJobRepository extends Repository | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment