1. Go to this page and download the library: Download lekoala/kaly-tasks 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/ */
final readonly class GeneratePdf
{
public function __construct(
public string $documentId,
) {}
public function __invoke(PdfGenerator $generator): void
{
$generator->generate($this->documentId);
}
}
final readonly class SendAppointmentReminder
{
public function __construct(
public string $appointmentId,
public int $version,
public string $deduplicationKey,
) {}
public function __invoke(AppointmentRepository $appointments, ReminderSender $sender): void
{
$appointment = $appointments->find($this->appointmentId);
if ($appointment === null || $appointment->cancelled) {
return;
}
if ($appointment->version !== $this->version) {
return; // superseded by a later change
}
$sender->send($appointment);
}
}
$registry->register(
'appointments.remind',
SendAppointmentReminder::class,
deduplicateBy: 'deduplicationKey',
);
// On booking or rescheduling, ideally in the same application transaction:
$tasks->dispatch(
new SendAppointmentReminder(
$appointment->id,
$appointment->version,
"{$appointment->id}:{$appointment->version}",
),
notBefore: $appointment->startsAt->modify('-1 day'),
);
$registry->register('reports.daily', GenerateDailyReport::class, deduplicateBy: 'period');
// On every host, from cron
$tasks->dispatch(new GenerateDailyReport($clock->now()->format('Y-m-d')));
$tasks->sequence([
new PrepareConversation($id),
new GenerateAnswer($id),
new PublishAnswer($id),
], notBefore: $at);
$json = TaskChain::encode($encodedTask->chain); // string, or null for no continuation
$remaining = TaskChain::decode($json); // list<EncodedTask>
$pdo = new PDO($writerDsn, $user, $password);
$store = new MysqlTaskStore(new PdoConnection($pdo));
foreach ($store->schema() as $statement) {
// execute $statement
}
use Kaly\Task\ScheduledTaskDispatcher;
use Kaly\Task\TaskDispatcher;
use Kaly\Tasks\Connection\DoctrineConnection;
use Kaly\Tasks\DatabaseTaskDispatcher;
use Kaly\Tasks\RetryPolicy;
use Kaly\Tasks\Store\PostgresTaskStore;
use Kaly\Tasks\TaskRegistry;
use Kaly\Tasks\TaskSequenceDispatcher;
$app->configure(fn(Definitions $di) => $di
->set(TaskRegistry::class, static fn() => (new TaskRegistry())
->register('documents.generate-pdf', GeneratePdf::class)
->register('support.process-webhook', ProcessWebhook::class,
queue: 'support',
retry: new RetryPolicy(maxAttempts: 8, delay: 30),
deduplicateBy: 'eventId',
))
->set(ScheduledTaskDispatcher::class, static fn(ContainerInterface $c) => new DatabaseTaskDispatcher(
new PostgresTaskStore(new DoctrineConnection($c->get(EntityManagerInterface::class)->getConnection())),
$c->get(TaskRegistry::class),
))
->alias(TaskDispatcher::class, ScheduledTaskDispatcher::class)
->alias(TaskSequenceDispatcher::class, ScheduledTaskDispatcher::class));
$store->failed(limit: 50); // inspect failed tasks
$store->deleteFailed($id); // delete a failed task and free its deduplication key
$store->retryFailed($id); // run a failed task again, with fresh attempts
$store->purge(7 * 24 * 3600, 30 * 24 * 3600); // delete done tasks after a week, failed ones after a month
$stats = $store->stats(); // whole table
$mailStats = $store->stats('mail'); // one queue
$stats->ready; // pending, due now
$stats->scheduled; // pending, due later, retries
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.