Shopware 6: Finding Shopware 6.5 instances – Monitoring Queue on Stage, Part 1

On one of our staging instances we had the problem, that the SEO urls didn’t get generated. Long story short: The problem was, that the queue worker didn’t work. Why? Because from shopware 6.4 to 6.5 the default queue was renamed from “default” to “async” and from 6.5 we need to set the queue name async explicitly. So we changed our supervisor script and it worked again.

BUT, it was already the second time this happened to us. The first time is ok, we learn from it, the second time is stupid. So avoiding a third time is highly important to me and us.

Monitoring the queues

So we decided to monitor the queues, so we know a lot quicker that something is wrong. And I implemented it yesterday. How we monitor the queue will be part of the second part, this part is about finding all Shopware 6.5+ instances on our staging server.

Finding Shopware 6.5 on the staging server

Find composer.lock and check for shopware version

I tried hard to find a nice way to collect all the composer.lock files in any subdirectory. Our directory structure changed a few times, so I don’t know were the composer.lock is.

I tried with a RecursiveDirectoryIterator, but because I don’t have read access to one of the directories it threw an error and I didn’t find a way to avoid it.

So I tried with find, but it was hard to make it follow symlinks. Although I’m sure that was on me. I’m not very experienced with the in and outs of bash and other shells.

ls it is!

So I neded up with ls 🎉 ls -L follows symlinks -1 returns one result per line and **/ searches in subdirectories.

   private function collectComposerLock(): array
    {
        $paths = [];
        $stars = '**/';
        for ($i = 0; $i <= 1; $i++) {
            $paths = array_merge($this->ls(self::BASE_PATH . $stars . 'current/composer.lock'), $paths);
            $stars .= '**/';
        }
        return $paths;
    }

    private function ls(string $path): array
    {
        $command = "ls -L $path -1";
        $path = shell_exec($command);
        return array_filter(explode("\n", $path ?? ''));
    }

I’m not sure how many subdirectories there are, so I wrapped it into a loop and merged everything into an array.

Next is to loop through all these composer.lock files most of them are Shopware, some of them are 6.5+. But there are Magento 1 and Magento 2, contao and custom Symfony implementations in between.

I thought the common ground for Shopware is shopware/core, so let’s read all composer.lock files and check for the shopware/core version:

    public function getShopware65Paths(): array
    {
        $paths = $this->collectComposerLock();

        foreach (array_diff($paths, $this->skipList) as $path) {
            if ($this->isShopware65($path)) {
                $shopware65Paths[] = $path;
            }
        }

        return array_map(static function ($path) {
            return str_replace('/composer.lock', '', $path);
        }, $shopware65Paths ?? []);
    }

    private function isShopware65(string $path): bool
    {
        $composerLock = json_decode(file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
        foreach ($composerLock['packages'] as $package) {
            $version = ltrim($package['version'], 'v');
            if ($package['name'] === 'shopware/core' && version_compare($version, '6.5', '>=')) {
                return true;
            }
        }
        return false;
    }

We read the composer.lock, check wether shopware/core exists between the packages and compare the version.

Sometimes the versions starts with a “v”, so we trim the version – just to be sure.

Done. Now we return a list of paths with a composer.lock file from getShopware65Paths().

And tomorrow you can read how to check wether the queue is ok.

3 thoughts on “Shopware 6: Finding Shopware 6.5 instances – Monitoring Queue on Stage, Part 1

Leave a Reply

Discover more from Winkelwagen

Subscribe now to keep reading and get access to the full archive.

Continue reading