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

Stoppable behavior #102

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Stoppable behavior #102

wants to merge 11 commits into from

Conversation

zhuravljov
Copy link
Member

Related #87.

The code is simple and anyone can create it inside own project.
But maybe need to create abstract class which will include stopping logics, and cache component should be moved into child?

@zhuravljov zhuravljov requested a review from cebe July 29, 2017 16:38
@TerraSkye
Copy link
Contributor

This wont work when your dealing with EA filecache and 2 different services,
It requires to that the runner and pusher use the same cache instance.

@zhuravljov
Copy link
Member Author

zhuravljov commented Jul 29, 2017

@TerraSkye, you are right, cache must use common storage only.

zhuravljov and others added 2 commits July 30, 2017 00:08
* @param string $id of a job
* @return bool
*/
protected function setStopping($id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably markAsStopped would be a better name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it make sense.

*/
public $cache = 'cache';
/**
* @var bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation is missing, its not really clear to me what this switch is for.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The option allows to turn off status usage that is not supported for AMQP driver.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be in the phpdoc.

public function events()
{
return [
Queue::EVENT_BEFORE_EXEC => function (ExecEvent $event) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making this a beforeExec() method instead of a closure could make it easier to extend this class.

@cebe
Copy link
Member

cebe commented Jul 30, 2017

This wont work when your dealing with EA filecache and 2 different services,
It requires to that the runner and pusher use the same cache instance.

could be added as a note to the phpdoc.

@samdark samdark added the type:enhancement Enhancement label Aug 15, 2017
@samdark samdark added this to the 2.0.1 milestone Aug 15, 2017
@samdark
Copy link
Member

samdark commented Aug 27, 2017

How about unit tests?

@zhuravljov
Copy link
Member Author

@samdark, tests have been added.

Copy link
Member

@samdark samdark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me.

Copy link
Member

@cebe cebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except the naming it looks good to me.

* @author Roman Zhuravlev <zhuravljov@gmail.com>
* @since 2.0.1
*/
class Behavior extends \yii\base\Behavior
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo it should be yii\queue\behaviors\Stoppable instead of yii\queue\stoppable\Behavior, Behavior as a class name does not make sense to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be used in a configuration by long name only. As well as closure behavior. Readability shouldn't be impaired.

'queue' => [
    //...
    'as stoppable' => \yii\queue\stoppable\Behavior::class,
    'as closure' => \yii\queue\closure\Behavior::class,
],

I think to make separate behaviors namespace isn't good idea.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be used in a configuration by long name only

Why do you think that people uses long names in configuration? I almost always use short names with use statements at the top of the file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rob006, I'm not sure. But this make sense for config files, which cover many class definitions from different namespaces.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, you will resolve name conflicts using namespace aliases. DbConnection, RedisConnection etc. So you can add alias:

use yii\queue\stoppable\Behavior as QueueStoppable;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I talk no about name suffix. Every package class is grouped to namespace by its role, no by class type.

This is extremely inconsistent to be honest. We have yii\queue\serializers for serializers, but doing the same for behaviors is wrong? So now we have at least three conventions for naming behaviors:

  • yii\queue\cli\Verbose,
  • yii\queue\LogBehavior,
  • yii\queue\stoppable\Behavior.

We have Interface suffix for interfaces, Event suffix for events, but name for behavior is simply random.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have yii\queue\serializers for serializers, but doing the same for behaviors is wrong?

Yes, because serializer implements only one role. It is queue\serializers.

Behavior is queue middleware. And behaviors can solve many different functions. queue\stoppable\Behavior allows to stop a job, queue\closure\Behavior serializes closures, queue\LogBehavior writes logs, etc.

So now we have at least three conventions for naming behaviors

I renamed Verbose to VerboseBehavior, and now we have single naming rule:

yii\queue\<Role><Type>

yii\queue\LogBehavior: Role = queue\Log, Type = Behavior
yii\queue\closure\Behavior: Role = queue\closure\, Type = Behavior
yii\queue\stoppable\Behavior: Role = stoppable\, Type = Behavior
yii\queue\PushEvent: Role = queue\Push, Type = Event
etc.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed Verbose to VerboseBehavior, and now we have single naming rule:

You clearly have two naming conventions:

  1. yii\queue\<Role><Type>
  2. yii\queue\<role>\<Type>

Anyway, I just hate using general names for specific implementations. It kills readability and productivity. I often use "Search by class name" feature in IDE - it is useless if you name all behaviors as "Behavior". You can't just import such class because you probably will get conflicts and Behavior will be just meaningless if you find it in code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of common ways of naming:

  • yii\<mode>\Application
  • yii\<package>\Module
  • yii\gii\generators\<entity>\Generator
  • Illuminate\Redis\Connections\Connection
  • Illuminate\Database\Connection
  • <vendor>\<package>\Client

I often use "Search by class name" feature in IDE - it is useless if you name all behaviors as "Behavior".

I'm not sure about all IDEs, but PHPStorm is able to search class usage considering namespace.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a pointless discussion.

In certain discussions there is no right or wrong, just different opinions.
In this particular case, the Yii2 staff could enrich the discussion with their opinions.

In my opinion, the best solution would be to maintain the naming pattern used in Yii2.
After all, it is an extension to a larger project and here is no place to innovate.

cc: @cebe @samdark

@zhuravljov zhuravljov modified the milestones: 2.0.1, 2.0.2 Nov 3, 2017
@zhuravljov zhuravljov modified the milestones: 2.0.2, 2.0.x Dec 15, 2017
@SamMousa
Copy link
Contributor

I don't like this approach at all.

  • There is no guarantee they will actually be stopped (a cache is not persistent reliable storage).
  • It uses the cache as some kind of quick storage, that is not what it is for.
  • If the cache is not reachable for whatever reason, all workers will stop working.
  • Several drivers already implement remove() (file, db, beanstalk, redis).

The exceptions here are amqp and gearman.

In the cases where you use amqp / gearman and you have multiple workers, you will need a shared cache.
If you are on the same host then you could use files storage, but in that case you could just as well use the file queue driver since you're depending on the file system anyway.
If you're on different hosts you'll probably end up using a cache driver like redis or memcache; in which case you could just as well use redis for your queue in the first place.

As OP mentioned, implementing it in your own project is simple. Since this (in my opinion) is hardly ever the best, or even a good, solution I think adding this behavior will result in worse decisions by developers.

@samdark samdark removed this from the 2.0.x milestone May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants