Skip to content
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

Is type hint of push method inappropriate? It seems to conflict with handleMessage #252

Open
Veitor opened this issue Jul 13, 2018 · 3 comments
Labels
type:docs Documentation

Comments

@Veitor
Copy link

Veitor commented Jul 13, 2018

I learned that type of $job in yii\queue\Queue::push($job) can be mixed except JobInterface instance. That meaings I can pass any type of parameter like string,array or object to this method.

In order to coverting $job to $message, I have to set yii\queue\Queue::$strictJobType to false for avoiding throwing exceptions.

The problem is coming, Why can't I handle receiving message in yii\queue\Queue::handleMessage, this method require message deserialized to be instance of JobInterface. Is this not against push method that allow passing type of non-JobInterface?

Do I need to implement class extend yii\queue\cli\Queue and override handleMessage? No need to do this, I‘m not going to develop new driver. I just use yii-queue according to the type hint of push mehod.

Q A
yii2-queue version 2.1.0
@cebe
Copy link
Member

cebe commented Aug 8, 2018

As far as I see there is no point in passing anything else than a JobInterface instance, so mixed should be removed from the typehint

@cebe cebe added the type:docs Documentation label Aug 8, 2018
@cebe cebe added this to the 2.1.1 milestone Aug 8, 2018
@rob006
Copy link

rob006 commented Aug 8, 2018

As far as I see there is no point in passing anything else than a JobInterface instance, so mixed should be removed from the typehint

This is for interoperability - I can push job in Yii and consume it with second script written in Python. Obviously I cannot rely on PHP types then, so I can push job as JSON - both Yii and Python script will be able to understand this job, in contrast to the serialized PHP object.

And you can handle such Job in handleMessage() you just need to use serializer smart enough to generate JobInterface instance from $message.

@Veitor
Copy link
Author

Veitor commented Aug 9, 2018

So, One of the correct ways to use it may be these steps:

  • produce message

step 1: Writing a Job implement JobInterface, and ths job class include data and processing logic.

step 2: Writing a serializer function which extracting data from Job class and coverting data to a specified format, and this format can be understanded by other Cross-language consumer.

  • consume message

step 1: Usually we use php yii queue/listen command to listen queue.

step 2: When received message, use defined unserializer function to converting data to Job class.

step 3: Job class will be passed to HandleMessage() function and execute processing logic in execute() method of class.


This looks like that serializer() and unserialzer() is key point. JobInterface is used in Yii application, the data in Job class must be extracted and converted to common format before push it to queue. Is it right?

So removing mixed typehint is correct, just use JobInterface in Yii Application.
That will be more complex if passing data of mixed type to push() method.

Of course, we can use anthoer design. typehint of push() method still be mixed, abandon definition of JobInterface, handleMessage method need to be forced to defined, it is used to handle message instead of JobInterface::execute() method. But there are others questions, such as custom handleMessage will not be used when using child process(I am confused here, I may need to open a new Issue).

All of above just is my opinion.

@samdark samdark removed this from the 2.2.0 milestone Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs Documentation
Projects
None yet
Development

No branches or pull requests

4 participants