1. <?php
  2. /**
  3. * Validates configuration for task queues used in batch processing.
  4. *
  5. * @param array $config An array containing the configuration settings.
  6. * Expected keys: 'queue_name', 'worker_count', 'redis_host', 'redis_port', 'redis_password', 'batch_size', 'max_retries'.
  7. * @return array|string Returns an array of validation errors if invalid, or an empty array if valid.
  8. */
  9. function validateTaskQueueConfig(array $config): array
  10. {
  11. $errors = [];
  12. // Validate required parameters
  13. if (!isset($config['queue_name']) || empty($config['queue_name'])) {
  14. $errors[] = 'Queue name is required.';
  15. }
  16. if (!isset($config['worker_count']) || !is_numeric($config['worker_count']) || $config['worker_count'] <= 0) {
  17. $errors[] = 'Worker count must be a positive integer.';
  18. }
  19. if (!isset($config['redis_host']) || empty($config['redis_host'])) {
  20. $errors[] = 'Redis host is required.';
  21. }
  22. if (!isset($config['redis_port']) || !is_numeric($config['redis_port']) || $config['redis_port'] < 1 || $config['redis_port'] > 65535) {
  23. $errors[] = 'Redis port must be a number between 1 and 65535.';
  24. }
  25. if (isset($config['redis_password']) && !empty($config['redis_password'])) {
  26. //Password validation can be expanded, but for now, just check if not empty
  27. }
  28. if (!isset($config['batch_size']) || !is_numeric($config['batch_size']) || $config['batch_size'] <= 0) {
  29. $errors[] = 'Batch size must be a positive integer.';
  30. }
  31. if (!isset($config['max_retries']) || !is_numeric($config['max_retries']) || $config['max_retries'] <= 0) {
  32. $errors[] = 'Max retries must be a positive integer.';
  33. }
  34. //Edge case: Validate batch_size against queue_name (e.g., prevent excessively large batches for short queue names)
  35. if (strlen($config['queue_name']) < 5 && $config['batch_size'] > 1000) {
  36. $errors[] = 'Batch size is too large for the queue name. Consider reducing batch size.';
  37. }
  38. //Edge case: Validate max_retries to be smaller than batch size (to avoid infinite loops)
  39. if ($config['max_retries'] >= $config['batch_size']) {
  40. $errors[] = 'Max retries should be less than batch size to prevent infinite retries.';
  41. }
  42. return $errors;
  43. }
  44. //Example usage
  45. /*
  46. $config = [
  47. 'queue_name' => 'my_queue',
  48. 'worker_count' => 2,
  49. 'redis_host' => 'localhost',
  50. 'redis_port' => 6379,
  51. 'redis_password' => '',
  52. 'batch_size' => 10,
  53. 'max_retries' => 3,
  54. ];
  55. $validationErrors = validateTaskQueueConfig($config);
  56. if (!empty($validationErrors)) {
  57. echo 'Validation Errors: ' . implode(', ', $validationErrors) . "\n";
  58. } else {
  59. echo 'Configuration is valid.\n';
  60. }
  61. */
  62. ?>

Add your comment