Laravel transient queues and messages - laravel

I'm working on Laravel 5.1.46 (LTS) with rabbitmq for message queues with package
.env
QUEUE_DRIVER=rabbitmq
config/queue.php
'rabbitmq' => [
'driver' => 'rabbitmq',
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
'port' => env('RABBITMQ_PORT', 5672),
'vhost' => env('RABBITMQ_VHOST', '/'),
'login' => env('RABBITMQ_LOGIN', 'guest'),
'password' => env('RABBITMQ_PASSWORD', 'guest'),
// name of the default queue,
'queue' => env('RABBITMQ_QUEUE'),
// create the exchange if not exists
'exchange_declare' => true,
// create the queue if not exists and bind to the exchange
'queue_declare_bind' => true,
'queue_params' => [
'passive' => false,
'durable' => true, // false
'exclusive' => false,
'auto_delete' => false,
],
'exchange_params' => [
// more info at http://www.rabbitmq.com/tutorials/amqp-concepts.html
'type' => env('RABBITMQ_EXCHANGE_TYPE', 'direct'),
'passive' => false,
// the exchange will survive server restarts
'durable' => true, // fakse
'auto_delete' => false,
]
I've 8 queues in total. Queue names are stored in .env file.
QUEUE_ONE=queue-one
QUEUE_TWO=queue-two
.
.
.
QUEUE_EIGHT=queue-eight
And while dispatching a job,
dispatch(new Job1())->onQueue(env('QUEUE_ONE'))
Queues and messages are persistent/durable.
Due to some performance issues, I need to change the durability of some queues and their messages. So,
5 queues and their messages will be transient (non-persistent)
3 queues and their messages will be persistent
How it is possible with-in Laravel and rabbitmq ?
Note :
I know, I can set
durable = false
but it will be for all queues,

// config/queue.php
'rabbitmq_durable' => [
'driver' => 'rabbitmq',
// ...
'queue_params' => [
'passive' => false,
'durable' => true,
'exclusive' => false,
'auto_delete' => false,
],
// ...
],
'rabbitmq_not_durable' => [
'driver' => 'rabbitmq',
// ...
'queue_params' => [
'passive' => false,
'durable' => false,
'exclusive' => false,
'auto_delete' => false,
],
// ...
]
Laravel 5.1
To use different configs and different connections on Laravel 5.1 you have to use the Queue fascade:
Queue::connection('rabbitmq_durable')->pushOn('queue_1', TestJob::class);
Queue::connection('rabbitmq_not_durable')->pushOn('queue_6', TestJob::class);
Laravel 5.2+
To use different configs and different connections on Laravel 5.2 and onwards you can use the onConnection() method like so:
TestJob::dispatch()->onQueue('queue_one')->onConnection('rabbitmq_durable');
TestJob::dispatch()->onQueue('queue_six')->onConnection('rabbitmq_not_durable');

Related

Laravel logging to to custom channel does not consider minimum level

So i made a custom logging handler with a custom logger to log to db, and it works correctly.
What i cannot manage to work is the minimum loggin level define in the configuration.
To test it i made a stack with single and my custom channel (let's call it database), hanving both a error level, and logged a message for every log level with artisan: the daily file contains 4 messages, but my custom channel has 8 messages while i expected only 4.
Testing with a stack of single and daily instead work correctly, so this makes me think that i have to handle the minimum loggin level in my custom classes, but i couldn't find anywhere something that tells it explicitly.
Is not a problem to update my custom classes to check the log level, but i want to be sure that i'm not mistaking something else about laravel logging.
config/logging.php
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'daily'],
//'channels' => ['single', 'database'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'error',
],
'database' => [
'driver' => 'custom',
'handler' => App\Logging\CustomLoggingHandler::class,
'via' => App\Logging\CustomLogger::class,
'level' => env('MIN_DB_LOGGING_LEVEL', 'error'),
],
],

Why Laravel uses the default queue connection even if I specified a queue with different connection

I have problem with laravel queues. In my project, default connection is sync, I want to add sqs connection for one type of jobs.
When I dispatch job in this way:
TestAction::dispatch()->onQueue('test');
Job is performed immediately (by sync connection).
If I dispatch job in this way:
TestAction::dispatch()->onQueue('test')->onConnection('sqsTestAction');
everything is ok.
Queue "test" is in sqsTestAction connection, why in first example job is being sent by sync connection?
My config/queue.php:
'default' => env('QUEUE_CONNECTION', 'sync'),
'connections' => [
'sync' => [
'driver' => 'sync',
],
'sqsTestAction' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('AWS_SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => "test",
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
],
Laravel ver 5.8
why in first example job is being sent by sync connection?
Because it is set by default as you can see. Change .env file value for QUEUE_CONNECTION to sqsTestAction and that will become default.
In default key of config/queue file, second parameter is fallback parameter for case when .env value doesn't exist.

Yii2 session expires after user is idle for a fixed seconds despite session timeouts being set to at least 1 day

I have already added these codes in my config/web file
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
'authTimeout' =>86400,
'loginUrl' => ['account/login'],
],
'session' => [
'timeout' => 86400,
],
After session expires I want to automatically logout and redirect to login action.
Make sure in your php.ini file
config
session.gc_maxlifetime
by default set as :
session.gc_maxlifetime=1440
change it, to :
session.gc_maxlifetime=86400
Very first you have to set 'enableAutoLogin' => false, .
now add these lines there in your config/web.
I have added it in frontend/config/main.php because I am using frontend only.
'components' => [
...
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
'enableSession' => true,
'authTimeout' => 1800, //30 minutes
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'class' => 'yii\web\Session',
'name' => 'advanced-frontend',
'timeout' => 1800,
],
...
Now go to yii2/web/User.php and write code for destroy session in logout method before return as guest()-
public function logout($destroySession = true)
{
$identity = $this->getIdentity();
if ($identity !== null && $this->beforeLogout($identity)) {
......
if ($destroySession && $this->enableSession) {
Yii::$app->getSession()->destroy();
}
$this->afterLogout($identity);
}
$session = Yii::$app->session;
$session->remove('other.id');
$session->remove('other.name');
// (or) if is optional if above won't works
unset($_SESSION['class.id']);
unset($_SESSION['class.name']);
// (or) if is optional if above won't works
unset($session['other.id']);
unset($session['other.name']);
return $this->getIsGuest();
}
For me it worked great.

How to configure cache for nodge eauth in Yii2

Now, I use default cache for all:
'cache' => [
'class' => 'yii\caching\FileCache',
],
And I use nodge eauth for registration users.
Default config nodge:
'components' => [
'eauth' => array(
'class' => 'nodge\eauth\EAuth',
'popup' => true, // Use the popup window instead of redirecting.
'cache' => false, // Cache component name or false to disable cache. Defaults to 'cache' on production environments.
'cacheExpire' => 0, // Cache lifetime. Defaults to 0 - means unlimited.
)
]
What should I specify to cache?
If you want to cache requests, your config should be
'eauth' => [
'class' => 'nodge\eauth\EAuth',
'popup' => false, // Use the popup window instead of redirecting.
'cache' => 'cache', // Cache component name or false to disable cache. Defaults to 'cache' on production environments.
'cacheExpire' => 0, // Cache lifetime. Defaults to 0 - means unlimited.]
But I can be wrong.

Installing user-management for Yii2.0

I've been trying to install user-management for Yii2.0, but getting ReflectionException while loading the page. I have attached the error page and directory structure below.
and the file path is as shown below.
I've searched a lot to find out the reason for this, but nothing worked out. can someone tell me what am I missing here to get it work. looks like the user-management installation documentation has some flaws. It is not clear enough to understand. Hope to get the steps to install. Thanks
Here is my console/web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gAry7SfUr0oOjNQDqItsobmGBcJajQoW',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
//'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'class' => 'app\webvimark\modules\user-management\components\UserConfig',
// Comment this if you don't want to record user logins
'on afterLogin' => function($event) {
\webvimark\modules\user-management\models\UserVisitLog::newVisitor($event->identity->id);
}
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'modules'=>[
'user-management' => [
'class' => 'webvimark\modules\user-management\UserManagementModule',
// 'enableRegistration' => true,
// Here you can set your handler to change layout for any controller or action
// Tip: you can use this event in any module
'on beforeAction'=>function(yii\base\ActionEvent $event) {
if ( $event->action->uniqueId == 'user-management/auth/login' )
{
$event->action->controller->layout = 'loginLayout.php';
};
},
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
It seems to have a slight difference with the expected configuration for this extension.
Use this
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
ie UserManagement instead of user-management is a configuration path and not a route

Resources