cybercog / laravel-love

Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
https://komarev.com/sources/laravel-love
MIT License
1.16k stars 72 forks source link

Command line Recount throws error and ReactionCounter always showing 0 #80

Closed NicklasT closed 5 years ago

NicklasT commented 5 years ago

Hi, this is the first time I open an issue on GitHub, so forgive me if I am missing some conventions. First of all, thank you for this package.

When running "php artisan:love recount" in the command prompt, I get this error:

Symfony\Component\Debug\Exception\FatalThrowableError : Call to undefined method Cog\Laravel\Love\Reactant\ReactionTotal\Models\NullReactionTotal::update()

at /var/www/theproject/vendor/cybercog/laravel-love/src/Console/Commands/Recount.php:166 162| } 163| 164| /* @var \Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal $total / 165| $total = $reactant->getReactionTotal(); 166| $total->update([ 167| 'count' => $totalCount, 168| 'weight' => $totalWeight, 169| ]); 170| }

I also have an issue with reaction count in general, it seems the database tables for counters are empty, even when there are reactions.

I have some reactions as can been seen below. The reactions are added through for example $reacterFacade->reactTo($likable, $likestatus); and enter the love_reactions table as expected as seen below.

mysql> select * from love_reactions;
+-----+-------------+------------+------------------+---------------------+---------------------+
| id  | reactant_id | reacter_id | reaction_type_id | created_at          | updated_at          |
+-----+-------------+------------+------------------+---------------------+---------------------+
| 146 |           2 |          2 |                2 | 2019-07-16 03:24:20 | 2019-07-16 03:24:20 |
| 147 |           4 |          2 |                1 | 2019-07-16 03:24:22 | 2019-07-16 03:24:22 |
| 152 |           3 |          2 |                1 | 2019-07-16 03:25:04 | 2019-07-16 03:25:04 |
+-----+-------------+------------+------------------+---------------------+---------------------+

But the counters/totals tables are still empty.

mysql> select * from love_reactant_reaction_counters;
Empty set (0.00 sec)
mysql> select * from love_reactant_reaction_totals;
Empty set (0.00 sec)

Something like $reactantFacade->getReactionCounterOfType('Like')->getCount() also returns 0.

As far as I can tell I have set up my models as per the installation instructions and have an App\User as the Reacterable and an App\Review as Reactable.

Any advise is much appreciated.

NicklasT commented 5 years ago

Okay, so I have now realized that queues are being used for the Event Listeners.

Cog\Laravel\Love\Reactant\Listeners\IncrementAggregates Cog\Laravel\Love\Reactant\Listeners\DecrementAggregates

I did not have my queue driver properly set up so these jobs were never executed.

I believe the issue can be closed.

antonkomarev commented 5 years ago

Hi, @NicklasT! Thank you for the feedback. We should highlight this in documentation.

You are right, those 2 listeners are waiting for ReactionHasBeenRemoved or ReactionHasBeenAdded events being fired and updates aggregates values in database.

If you have QUEUE_CONNECTION=sync defined in .env file - aggregates update will be done synchronously. To prevent long wait time and browser hanging connection just setup queue connection in your application and updates will be executed on background. This happens because both listeners implements Illuminate\Contracts\Queue\ShouldQueue interface.

antonkomarev commented 5 years ago

I've added Aggregation Queue to documentation.

vesper8 commented 4 years ago

@antonkomarev I also just got this error. In my opinion the error should be caught and a more descriptive error should be shown.. the Call to undefined method Cog\Laravel\Love\Reactant\ReactionTotal\Models\NullReactionTotal::update() being caused by queues not being active is quite confusing

Also, it would be nice if the love:recount command had an optional parameter so it can be executed on the sync queue without having to change the queue settings in the .env file

My queue driver is set to redis but I have many jobs which I can run on sync when executed manually from the command line by doing something like this:

    public function __construct(QueueManager $queueManager)
    {
        $this->queueManager = $queueManager;

        parent::__construct();
    }

    public function handle()
    {
        ini_set('max_execution_time', 9999);
        ini_set('memory_limit', '2048M');

        $this->queueManager->setDefaultDriver('sync');

        ...
vesper8 commented 4 years ago

@antonkomarev I just made sure that my queues are working now, I can see that Laravel Horizon is up and running, yet I am still getting the same error when I try to run love:recount

Is there something more I need to do in order to make this work with Horizon?

vesper8 commented 4 years ago

Actually now I'm trying with the QUEUE_CONNECTION=sync and still getting the same error.. hrmm..