1. Go to this page and download the library: Download friendsofhyperf/support 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/ */
friendsofhyperf / support example snippets
use function FriendsOfHyperf\Support\dispatch;
// Simple closure dispatch to async queue
dispatch(function () {
// Your job logic here
logger()->info('Job executed!');
});
// With configuration
dispatch(function () {
// Your job logic here
})
->onConnection('high-priority')
->delay(60) // Execute after 60 seconds
->setMaxAttempts(5);
// With dependency injection
dispatch(function (UserService $userService, LoggerInterface $logger) {
$users = $userService->getActiveUsers();
$logger->info('Processing ' . count($users) . ' users');
});
use Hyperf\Amqp\Message\ProducerMessageInterface;
use function FriendsOfHyperf\Support\dispatch;
// Dispatch AMQP message
dispatch($amqpMessage)
->setPool('default')
->setExchange('my.exchange')
->setRoutingKey('my.routing.key')
->setTimeout(10)
->setConfirm(true);
use Hyperf\Kafka\Producer\ProduceMessage;
use function FriendsOfHyperf\Support\dispatch;
// Dispatch Kafka message
dispatch($kafkaMessage)
->setPool('default');
use FriendsOfHyperf\Support\CallQueuedClosure;
// Create a closure job
$job = CallQueuedClosure::create(function () {
// Your job logic
return 'Job completed!';
});
// Configure max attempts
$job->setMaxAttempts(3);
// The job can be pushed to queue manually or via dispatch()
use FriendsOfHyperf\Support\Bus\PendingAsyncQueueDispatch;
$pending = new PendingAsyncQueueDispatch($job);
$pending
->onConnection('default')
->delay(30)
->when($condition, function ($dispatch) {
$dispatch->onConnection('special');
})
->unless($otherCondition, function ($dispatch) {
$dispatch->delay(60);
});
// Job is dispatched when object is destroyed
use FriendsOfHyperf\Support\Bus\PendingAmqpProducerMessageDispatch;
$pending = new PendingAmqpProducerMessageDispatch($message);
$pending
->setPool('default')
->setExchange('my.exchange')
->setRoutingKey('my.routing.key')
->setTimeout(5)
->setConfirm(true);
// Message is sent when object is destroyed
use FriendsOfHyperf\Support\Bus\PendingKafkaProducerMessageDispatch;
$pending = new PendingKafkaProducerMessageDispatch($message);
$pending->setPool('default');
// Message is sent when object is destroyed
use function FriendsOfHyperf\Support\dispatch;
dispatch($job)
->when($shouldUseHighPriority, function ($dispatch) {
$dispatch->onConnection('high-priority');
})
->unless($isTestMode, function ($dispatch) {
$dispatch->delay(10);
});
use FriendsOfHyperf\Support\Backoff\ArrayBackoff;
// Custom delays
$backoff = new ArrayBackoff([100, 500, 1000, 2000, 5000]);
// Stop after array is exhausted (returns 0)
$backoff = new ArrayBackoff([100, 500, 1000], false);
// From comma-separated string
$backoff = ArrayBackoff::fromString('100, 500, 1000, 2000');
// From predefined patterns
$backoff = ArrayBackoff::fromPattern('short'); // [100, 200, 300, 500, 1000]
$backoff = ArrayBackoff::fromPattern('medium'); // [200, 500, 1000, 2000, 5000]
$backoff = ArrayBackoff::fromPattern('long'); // [500, 1000, 2000, 5000, 10000, 30000]
$backoff = ArrayBackoff::fromPattern('exponential'); // [100, 200, 400, 800, 1600, 3200, 6400]
// Usage in retry logic
$attempt = 0;
while (true) {
try {
return performOperation();
} catch (Exception $e) {
$delay = $backoff->next();
if ($delay === 0) {
throw $e; // No more retries
}
usleep($delay * 1000); // Convert to microseconds
}
}
interface BackoffInterface
{
public function next(): int; // Get next delay in milliseconds
public function reset(): void; // Reset attempt counter
public function getAttempt(): int; // Get current attempt count
public function sleep(): int; // Sleep for calculated delay
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.