5

I noticed that the "updated" column for indexers only changes, when I run the indexers manually from the command line. It seems like everything is up to date (i.e. I see changes reflected in the frontend)

They are set to "update on save". When I change the mode to "update by schedule", the date changes as expected.

What could be the reason? Is this the normal behavior in Magento 2.1?

  • 1
    Good question, personally I just add an extra cron job. – William Oakley Sep 2 '16 at 11:01
  • Maybe this date is updated only when the full reindex is trigger. – Franck Garnier Feb 20 '17 at 14:07
1

The class Magento\Indexer\Model\Indexer\State serves for keeping index state, and column updated is modified with this piece of code.

public function beforeSave()
{
    $this->setUpdated(time());
    return parent::beforeSave();
}

It will be triggered on (almost) every save() call. It happens in Magento\Indexer\Model\Indexer within reindexAll(), reindexRow(), reindexList() methods.

When $this->getState()->save() is called from one of those three methods, then save() method from \Magento\Framework\Model\ResourceModel\Db\AbstractDb will be called:

public function save(\Magento\Framework\Model\AbstractModel $object)
{    
    ...
    try {
    if (!$this->isModified($object)) {
        $this->processNotModifiedSave($object);
        $this->commit();
        $object->setHasDataChanges(false);
        return $this;
    }
    $object->validateBeforeSave();
    $object->beforeSave();
    ...

What is "the issue" here?

Well, if you call $this->getState()->save() (as it's done in reindexRow() and reindexList()), then because of if (!$this->isModified($object)) - beforeSave() will never be triggerred. It further means that new updated time will not be set.

In case you call full reindex from command line, it will execute reindexAll(). The difference here is that there is a piece of code which sets status STATUS_WORKING:

public function reindexAll()
{
    if ($this->getState()->getStatus() != StateInterface::STATUS_WORKING) {
        $state = $this->getState();
        $state->setStatus(StateInterface::STATUS_WORKING);
        $state->save();
        ...

Since here data is changed, then beforeSave() will be triggered, and new updated time will be set.

The real question at the end is: Is it a bug or a feature?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.