yii2-queue
yii2-queue copied to clipboard
Is type hint of push method inappropriate? It seems to conflict with handleMessage
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 |
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
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.
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.