-
-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connecting different Yii2 apps to the same queue #339
Comments
@zhuravljov any idea on how we can improve to solve it? |
At first, Implement your own serializer class if you want to handle custom messages in the workers. You can simply replace |
@larryli so do you think better docs would be enough? |
@samdark Yes. |
@flaviovs I copy some code from \yii\web\UrlManager to MyJsonSerializer. So, handle multi-apps jobs: 'queue' => [
'class' => yii\queue\redis\Queue::class,
'redis' => 'redis',
'serializer' => [
'class' => app\MyJsonSerializer::class,
'jobAttribute' => 'event',
'rules' => [
'<app:\w+>-common-<job:\w+>' => 'app/jobs/common/<job>', // pass $job->app
'<app:\w+>-<job:\w+>' => 'app/jobs/<app>/<job>',
],
],
]; push: Yii::$app->queue->push([
'event' => 'app1-foobar',
'foo' => 'bar',
]); Some code: list($route, $params) = $this->parse(ArrayHelper::getValue($unserialized, $this->jobAttribute));
$route = trim($route, '/');
$pos = strrpos($route, '/');
if ($pos === false) {
$route = ucfirst($route);
} else {
$pos++;
$route = str_replace('/', '\\', substr($route, 0, $pos)) . ucfirst(substr($route, $pos));
}
$params['class'] = $route . 'Job';
if (strpos($params['class'], '-') !== false || !class_exists($params['class'])) {
throw new UnknownClassException($params['class']);
}
foreach ($unserialized as $key => $value) {
if (!isset($params[$key])) {
$params[$key] = $value;
}
}
return Yii::createObject($params); |
I think that a clean and simple solution would be to have a @larryli, BTW, passing FYI, I'm testing now the custom serializer workaround. Actually, this is akin to the |
@flaviovs
Now you can push a JSON message on the queue which has a "class" as a property.
|
Hi @luayessa - thanks for the tip. For completeness, my primary goal was to have two distinct Yii2 to communicate, and the solution I adopted was actually very simple: just make sure that the job class both in app A and B have the same properties. In other words, they do not need to come from the same codebase, just have the same public attributes. I even made |
@flaviovs good job |
What steps will reproduce the problem?
On the producer:
On the consumer:
Then, in the producer:
What's expected?
Unserialized job data easily accessible somehow to the consumer.
What do you get instead?
Impossible to get direct access to the unserialized job data.
Looking into
unserializeMessage()
(see below) we see that the unserialized job is returned only if it is aJobInterface
object, which obviously it is not (i.e. it is a PHP array just unserialized from a JSON string).yii2-queue/src/Queue.php
Lines 265 to 281 in ee28f57
The only way I could manage to access the data is to get the (serialized) JSON string using
$ev->error->getSerialized()
, and then decode it. That does not look right to me.Not sure of it is a bug, or design decision. However, this issue makes it very difficult to (ironically) two Yii2 apps that do not share the same codebase to communicate.
Additional info
The text was updated successfully, but these errors were encountered: