PHP code example of tochka-developers / queue-promises
1. Go to this page and download the library: Download tochka-developers/queue-promises library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tochka-developers / queue-promises example snippets
namespace App\Promises;
use Tochka\Queue\Promises\Jobs\Promise;
class TestPromise extends Promise
{
/**
* Instance initialization
*
* @return void
*/
public function __construct()
{
// Jobs chaining may be done here
}
/**
* This will be called after all jobs of the promise have finished execution, but before success or errors.
* If this method returns false, success and errors won't be called.
*
* @return bool
*/
public function before(): bool
{
// ...
return true;
}
/**
* This will be called if all the jobs have completed successfully.
*
* @return bool
*/
public function success(): bool
{
// ...
return true;
}
/**
* This will be called if one or more jobs have failed.
*
* @return bool
*/
public function errors(): bool
{
// ...
return true;
}
/**
* This will be called if the promise timed out.
*
* @return bool
*/
public function timeout(): bool
{
// ...
return true;
}
/**
* This will be called after the execution of success or errors
*
* @return bool
*/
public function after()
{
// ...
return true;
}
}
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Tochka\Queue\Promises\Contracts\MayPromised;
use Tochka\Queue\Promises\Jobs\Promised;
class SomeJob implements ShouldQueue, MayPromised
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Promised;
public $text;
public function __construct($text)
{
$this->text = $text;
}
public function handle()
{
// some actions
}
}
$promise->runSync(); // Run the promise synchronously
$promise->runAsync(); // Run the promise asynchronously
$promise->setPromiseFinishConditions(
true, // The promise must execute when a job completes successfully for the first time.
true // The promise must execute when a job fails for the first time.
);
class SomeEvent implements PromisedEvent
{
public $id;
public $data;
public function __construct($id, string $data)
{
$this->id = $id;
$this->data = $data;
}
/**
* Get the event id
* @return string
*/
public function getUniqueId()
{
return $this->id;
}
}
public function before(): bool
{
$results = $this->getResults();
foreach ($results as $result) {
$status = $result->getJobStatus(); // This returns the job execution status
}
}
public function errors(SomeJob1 $job1, SomeJob2 $job2): bool
{
echo $job1->getJobStatus();
echo $job2->getJobStatus();
}
/**
* Get the job execution status
* Returns either MayPromised::JOB_STATUS_SUCCESS or MayPromised::JOB_STATUS_ERROR
* @return string
*/
public function getJobStatus(): string;
/**
* Get the job execution errors
* Returns an array ['code' => ..., 'message' => ...]
* @return array
*/
public function getJobErrors(): array;