1. Go to this page and download the library: Download liniopay/idle 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/ */
liniopay / idle example snippets
$container = new PimpleContainer();
// Idle configuration
$container[IdleConfig::class] = function() {
$serviceConfig = 'worker_config.php');
return new IdleConfig($serviceConfig, $messageConfig, $jobConfig, $workerConfig);
};
// Logs
$container[LoggerInterface::class] = function() {
$log = new Logger('idle');
$log->pushHandler(new MonologStreamHandler('php://stdout'));
return $log;
};
// PSR11 Container Wrapper for Pimple
$container[PSRContainer::class] = function(PimpleContainer $container) {
return new PSRContainer($container);
};
// Idle
$container[MessageFactoryInterface::class] = function(PimpleContainer $container) {
return new MessageFactory($container[PSRContainer::class]);
};
$container[ServiceFactoryInterface::class] = function(PimpleContainer $container) {
return new ServiceFactory($container[PSRContainer::class]);
};
$container[JobFactoryInterface::class] = function(PimpleContainer $container) {
return new JobFactory($container[PSRContainer::class]);
};
$container[MessageJobInterface::class] = function(PimpleContainer $container) {
return new MessageJobFactory($container[PSRContainer::class]);
};
$container[SimpleJobInterface::class] = function(PimpleContainer $container) {
return new SimpleJobFactory($container[PSRContainer::class]);
};
$container[WorkerFactoryInterface::class] = function(PimpleContainer $container) {
return new WorkerFactory($container[PSRContainer::class]);
};
// Services
$container[SQSService::class] = function(PimpleContainer $container) {
return new SQSServiceFactory($container[PSRContainer::class]);
};
// Workers
$container[BazWorker::class] = function(PimpleContainer $container) {
return new BazWorkerFactory($container[PSRContainer::class]);
};
$messageFactory = $container->get(\LinioPay\Idle\Message\MessageFactory::class);
/** @var @var SendableMessage|QueueMessage $message */
$message = $messageFactory->createSendableMessage([
'queue_identifier' => 'my-queue',
'body'=> 'hello queue payload!',
'attributes' => [
'foo' => 'bar',
]
]);
// You could then queue this message:
$message->send(); // Send is an alias for `queue` which queues the message to its service (SQS)
use GuzzleHttp\Psr7\Request; // Any Request class may be used as long as it implements PSR7
// ...
$messageFactory = $container->get(\LinioPay\Idle\Message\MessageFactory::class);
$messageFactory->createSendableMessage([
'queue_identifier' => 'my-task-queue',
'attributes' => [
'request' => new Request(
'PUT',
'http://foobar.example.com',
[
'Content-Type' => 'application/json',
],
'payload'
),
]
])->send();
$messageFactory = $container->get(\LinioPay\Idle\Message\MessageFactory::class);
/** @var SendableMessage|TopicMessage $message */
$message = $messageFactory->createSendableMessage([
'topic_identifier' => 'my-topic', // Key 'topic_identifier' lets idle know to expect a TopicMessage. Its value must match the name of the configured topic in the config.
'body'=> 'hello pubsub payload!',
'attributes' => [
'foo' => 'bar',
]
]);
// You can now send this message up to the topic
$message->send(); // Send to PubSub with the configured 'publish' parameters
$messageFactory = $container->get(\LinioPay\Idle\Message\MessageFactory::class);
/** @var SubscriptionMessage $message */
$message = $messageFactory->createMessage([
'subscription_identifier' => 'my-subscription', // Key 'subscription_identifier' lets idle know to expect a SubscriptionMessage. Its value must match the name of the configured subscription in the config.
'body'=> 'hello pubsub payload!',
'attributes' => [
'foo' => 'bar',
]
]);
use LinioPay\Idle\Job\JobFactory;
use LinioPay\Idle\Job\Jobs\SimpleJob;
/** @var JobFactory $jobFactory */
$jobFactory = $container->get(\LinioPay\Idle\Job\JobFactory::class);
$job = $jobFactory->createJob(SimpleJob::IDENTIFIER, [ // Create a Job of the type SimpleJob::IDENTIFIER
'simple_identifier' => 'my-simple-job', // The name of our SimpleJob
'foo' => 'bar', // Set parameters to override the configured defaults for `my-simple-job`
]);
$job->process(); // Processes each the defined workers for `my-simple-job`. In this case only `FooWorker`.
$success = $job->isSuccessful();
$duration = $job->getDuration();
$errors = $job->getErrors();