<?php
/**
* Validates configuration for task queues used in batch processing.
*
* @param array $config An array containing the configuration settings.
* Expected keys: 'queue_name', 'worker_count', 'redis_host', 'redis_port', 'redis_password', 'batch_size', 'max_retries'.
* @return array|string Returns an array of validation errors if invalid, or an empty array if valid.
*/
function validateTaskQueueConfig(array $config): array
{
$errors = [];
// Validate required parameters
if (!isset($config['queue_name']) || empty($config['queue_name'])) {
$errors[] = 'Queue name is required.';
}
if (!isset($config['worker_count']) || !is_numeric($config['worker_count']) || $config['worker_count'] <= 0) {
$errors[] = 'Worker count must be a positive integer.';
}
if (!isset($config['redis_host']) || empty($config['redis_host'])) {
$errors[] = 'Redis host is required.';
}
if (!isset($config['redis_port']) || !is_numeric($config['redis_port']) || $config['redis_port'] < 1 || $config['redis_port'] > 65535) {
$errors[] = 'Redis port must be a number between 1 and 65535.';
}
if (isset($config['redis_password']) && !empty($config['redis_password'])) {
//Password validation can be expanded, but for now, just check if not empty
}
if (!isset($config['batch_size']) || !is_numeric($config['batch_size']) || $config['batch_size'] <= 0) {
$errors[] = 'Batch size must be a positive integer.';
}
if (!isset($config['max_retries']) || !is_numeric($config['max_retries']) || $config['max_retries'] <= 0) {
$errors[] = 'Max retries must be a positive integer.';
}
//Edge case: Validate batch_size against queue_name (e.g., prevent excessively large batches for short queue names)
if (strlen($config['queue_name']) < 5 && $config['batch_size'] > 1000) {
$errors[] = 'Batch size is too large for the queue name. Consider reducing batch size.';
}
//Edge case: Validate max_retries to be smaller than batch size (to avoid infinite loops)
if ($config['max_retries'] >= $config['batch_size']) {
$errors[] = 'Max retries should be less than batch size to prevent infinite retries.';
}
return $errors;
}
//Example usage
/*
$config = [
'queue_name' => 'my_queue',
'worker_count' => 2,
'redis_host' => 'localhost',
'redis_port' => 6379,
'redis_password' => '',
'batch_size' => 10,
'max_retries' => 3,
];
$validationErrors = validateTaskQueueConfig($config);
if (!empty($validationErrors)) {
echo 'Validation Errors: ' . implode(', ', $validationErrors) . "\n";
} else {
echo 'Configuration is valid.\n';
}
*/
?>
Add your comment