"bar", "beers" => array("Appenzeller", "Guinness", "Kölsch"), "alcohol_free" => false);'; /** @var array */ private $initialConfig = ['foo' => 'bar', 'beers' => ['Appenzeller', 'Guinness', 'Kölsch'], 'alcohol_free' => false]; /** @var string */ private $configFile; /** @var string */ private $randomTmpDir; protected function setUp(): void { parent::setUp(); $this->randomTmpDir = Server::get(ITempManager::class)->getTemporaryFolder(); $this->configFile = $this->randomTmpDir . 'testconfig.php'; file_put_contents($this->configFile, self::TESTCONTENT); } protected function tearDown(): void { unlink($this->configFile); parent::tearDown(); } private function getConfig(): Config { return new Config($this->randomTmpDir, 'testconfig.php'); } public function testGetKeys(): void { $expectedConfig = ['foo', 'beers', 'alcohol_free']; $this->assertSame($expectedConfig, $this->getConfig()->getKeys()); } public function testGetKeysReturnsEnvironmentKeysIfSet() { $expectedConfig = ['foo', 'beers', 'alcohol_free', 'taste']; putenv('NC_taste=great'); $this->assertSame($expectedConfig, $this->getConfig()->getKeys()); putenv('NC_taste'); } public function testGetValue(): void { $config = $this->getConfig(); $this->assertSame('bar', $config->getValue('foo')); $this->assertSame(null, $config->getValue('bar')); $this->assertSame('moo', $config->getValue('bar', 'moo')); $this->assertSame(false, $config->getValue('alcohol_free', 'someBogusValue')); $this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers', 'someBogusValue')); $this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers')); } public function testGetValueReturnsEnvironmentValueIfSet(): void { $config = $this->getConfig(); $this->assertEquals('bar', $config->getValue('foo')); putenv('NC_foo=baz'); $config = $this->getConfig(); $this->assertEquals('baz', $config->getValue('foo')); putenv('NC_foo'); // unset the env variable } public function testGetValueReturnsEnvironmentValueIfSetToZero(): void { $config = $this->getConfig(); $this->assertEquals('bar', $config->getValue('foo')); putenv('NC_foo=0'); $config = $this->getConfig(); $this->assertEquals('0', $config->getValue('foo')); putenv('NC_foo'); // unset the env variable } public function testGetValueReturnsEnvironmentValueIfSetToFalse(): void { $config = $this->getConfig(); $this->assertEquals('bar', $config->getValue('foo')); putenv('NC_foo=false'); $config = $this->getConfig(); $this->assertEquals('false', $config->getValue('foo')); putenv('NC_foo'); // unset the env variable } public function testSetValue(): void { $config = $this->getConfig(); $config->setValue('foo', 'moo'); $this->assertSame('moo', $config->getValue('foo')); $content = file_get_contents($this->configFile); $expected = " 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n"; $this->assertEquals($expected, $content); $config->setValue('bar', 'red'); $config->setValue('apps', ['files', 'gallery']); $this->assertSame('red', $config->getValue('bar')); $this->assertSame(['files', 'gallery'], $config->getValue('apps')); $content = file_get_contents($this->configFile); $expected = " 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'bar' => 'red',\n 'apps' => \n " . " array (\n 0 => 'files',\n 1 => 'gallery',\n ),\n);\n"; $this->assertEquals($expected, $content); } public function testSetValues(): void { $config = $this->getConfig(); $content = file_get_contents($this->configFile); $this->assertEquals(self::TESTCONTENT, $content); // Changing configs to existing values and deleting non-existing once // should not rewrite the config.php $config->setValues([ 'foo' => 'bar', 'not_exists' => null, ]); $this->assertSame('bar', $config->getValue('foo')); $this->assertSame(null, $config->getValue('not_exists')); $content = file_get_contents($this->configFile); $this->assertEquals(self::TESTCONTENT, $content); $config->setValues([ 'foo' => 'moo', 'alcohol_free' => null, ]); $this->assertSame('moo', $config->getValue('foo')); $this->assertSame(null, $config->getValue('not_exists')); $content = file_get_contents($this->configFile); $expected = " 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n"; $this->assertEquals($expected, $content); } public function testDeleteKey(): void { $config = $this->getConfig(); $config->deleteKey('foo'); $this->assertSame('this_was_clearly_not_set_before', $config->getValue('foo', 'this_was_clearly_not_set_before')); $content = file_get_contents($this->configFile); $expected = " \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n"; $this->assertEquals($expected, $content); } public function testConfigMerge(): void { // Create additional config $additionalConfig = '"totallyOutdated");'; $additionalConfigPath = $this->randomTmpDir . 'additionalConfig.testconfig.php'; file_put_contents($additionalConfigPath, $additionalConfig); // Reinstantiate the config to force a read-in of the additional configs $config = new Config($this->randomTmpDir, 'testconfig.php'); // Ensure that the config value can be read and the config has not been modified $this->assertSame('totallyOutdated', $config->getValue('php53', 'bogusValue')); $this->assertEquals(self::TESTCONTENT, file_get_contents($this->configFile)); // Write a new value to the config $config->setValue('CoolWebsites', ['demo.owncloud.org', 'owncloud.org', 'owncloud.com']); $expected = " 'bar',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'php53' => 'totallyOutdated',\n 'CoolWebsites' => \n array (\n " . " 0 => 'demo.owncloud.org',\n 1 => 'owncloud.org',\n 2 => 'owncloud.com',\n ),\n);\n"; $this->assertEquals($expected, file_get_contents($this->configFile)); // Cleanup unlink($additionalConfigPath); } } d='n110' href='#n110'>110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package queue

import (
	"context"

	"code.gitea.io/gitea/modules/graceful"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/nosql"

	"github.com/go-redis/redis/v8"
)

// RedisQueueType is the type for redis queue
const RedisQueueType Type = "redis"

// RedisQueueConfiguration is the configuration for the redis queue
type RedisQueueConfiguration struct {
	ByteFIFOQueueConfiguration
	RedisByteFIFOConfiguration
}

// RedisQueue redis queue
type RedisQueue struct {
	*ByteFIFOQueue
}

// NewRedisQueue creates single redis or cluster redis queue
func NewRedisQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
	configInterface, err := toConfig(RedisQueueConfiguration{}, cfg)
	if err != nil {
		return nil, err
	}
	config := configInterface.(RedisQueueConfiguration)

	byteFIFO, err := NewRedisByteFIFO(config.RedisByteFIFOConfiguration)
	if err != nil {
		return nil, err
	}

	byteFIFOQueue, err := NewByteFIFOQueue(RedisQueueType, byteFIFO, handle, config.ByteFIFOQueueConfiguration, exemplar)
	if err != nil {
		return nil, err
	}

	queue := &RedisQueue{
		ByteFIFOQueue: byteFIFOQueue,
	}

	queue.qid = GetManager().Add(queue, RedisQueueType, config, exemplar)

	return queue, nil
}

type redisClient interface {
	RPush(ctx context.Context, key string, args ...interface{}) *redis.IntCmd
	LPush(ctx context.Context, key string, args ...interface{}) *redis.IntCmd
	LPop(ctx context.Context, key string) *redis.StringCmd
	LLen(ctx context.Context, key string) *redis.IntCmd
	SAdd(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
	SRem(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
	SIsMember(ctx context.Context, key string, member interface{}) *redis.BoolCmd
	Ping(ctx context.Context) *redis.StatusCmd
	Close() error
}

var _ ByteFIFO = &RedisByteFIFO{}

// RedisByteFIFO represents a ByteFIFO formed from a redisClient
type RedisByteFIFO struct {
	client redisClient

	queueName string
}

// RedisByteFIFOConfiguration is the configuration for the RedisByteFIFO
type RedisByteFIFOConfiguration struct {
	ConnectionString string
	QueueName        string
}

// NewRedisByteFIFO creates a ByteFIFO formed from a redisClient
func NewRedisByteFIFO(config RedisByteFIFOConfiguration) (*RedisByteFIFO, error) {
	fifo := &RedisByteFIFO{
		queueName: config.QueueName,
	}
	fifo.client = nosql.GetManager().GetRedisClient(config.ConnectionString)
	if err := fifo.client.Ping(graceful.GetManager().ShutdownContext()).Err(); err != nil {
		return nil, err
	}
	return fifo, nil
}

// PushFunc pushes data to the end of the fifo and calls the callback if it is added
func (fifo *RedisByteFIFO) PushFunc(ctx context.Context, data []byte, fn func() error) error {
	if fn != nil {
		if err := fn(); err != nil {
			return err
		}
	}
	return fifo.client.RPush(ctx, fifo.queueName, data).Err()
}

// PushBack pushes data to the top of the fifo
func (fifo *RedisByteFIFO) PushBack(ctx context.Context, data []byte) error {
	return fifo.client.LPush(ctx, fifo.queueName, data).Err()
}

// Pop pops data from the start of the fifo
func (fifo *RedisByteFIFO) Pop(ctx context.Context) ([]byte, error) {
	data, err := fifo.client.LPop(ctx, fifo.queueName).Bytes()
	if err == nil || err == redis.Nil {
		return data, nil
	}
	return data, err
}

// Close this fifo
func (fifo *RedisByteFIFO) Close() error {
	return fifo.client.Close()
}

// Len returns the length of the fifo
func (fifo *RedisByteFIFO) Len(ctx context.Context) int64 {
	val, err := fifo.client.LLen(ctx, fifo.queueName).Result()
	if err != nil {
		log.Error("Error whilst getting length of redis queue %s: Error: %v", fifo.queueName, err)
		return -1
	}
	return val
}

func init() {
	queuesMap[RedisQueueType] = NewRedisQueue
}