From 721a228c13aef455d692b8e80a24e3d996775dd5 Mon Sep 17 00:00:00 2001 From: mychidarko Date: Tue, 12 Sep 2023 08:39:08 +0000 Subject: [PATCH] feat: add config generation command --- src/Queue/Commands/QueueConfigCommand.php | 40 ++++ src/Queue/Commands/stubs/config.stub | 235 ++++++++++++++++++++++ 2 files changed, 275 insertions(+) create mode 100644 src/Queue/Commands/QueueConfigCommand.php create mode 100644 src/Queue/Commands/stubs/config.stub diff --git a/src/Queue/Commands/QueueConfigCommand.php b/src/Queue/Commands/QueueConfigCommand.php new file mode 100644 index 0000000..0ab0b43 --- /dev/null +++ b/src/Queue/Commands/QueueConfigCommand.php @@ -0,0 +1,40 @@ +comment('Generating queue config...'); + + $appConfigDir = getcwd() . '/config'; + $appConfigFile = $appConfigDir . '/queue.php'; + + if (!Core::isMVCProject()) { + $appConfigDir = getcwd(); + $appConfigFile = $appConfigDir . '/queue.config.php'; + } + + if (file_exists($appConfigFile)) { + $this->error('Queue config already exists'); + return 1; + } + + if (!copy(__DIR__ . '/stubs/config.stub', $appConfigFile)) { + $this->error('Failed to generate queue config'); + return 1; + } + + $this->info('Queue config generated successfully'); + + return 0; + } +} diff --git a/src/Queue/Commands/stubs/config.stub b/src/Queue/Commands/stubs/config.stub new file mode 100644 index 0000000..43a540a --- /dev/null +++ b/src/Queue/Commands/stubs/config.stub @@ -0,0 +1,235 @@ + 'redis', + + /* + |-------------------------------------------------------------------------- + | Default Queue Connection + |-------------------------------------------------------------------------- + | + | Here you may specify which of the queue connections below you wish + | to use as your default connection for all queue work. + | + */ + 'default' => 'redis', + + /* + |-------------------------------------------------------------------------- + | Connection Config + |-------------------------------------------------------------------------- + | + | This section sets up the connection for your queue. You can set up + | the credentials you need to connect to your queue here. Leaf + | queue supports redis and database connections. + | + | See https://leafphp.dev/modules/queue#connections for more info + | + */ + 'connections' => [ + 'redis' => [ + 'host' => _env('REDIS_HOST', '127.0.0.1'), + 'port' => _env('REDIS_PORT', '6379'), + 'password' => _env('REDIS_PASSWORD', ''), + 'dbname' => _env('REDIS_DB', 0), + ], + + 'sqlite' => [ + 'driver' => 'sqlite', + 'url' => _env('DATABASE_URL'), + 'database' => _env('DB_DATABASE', DatabasePath('database.sqlite')), + 'prefix' => '', + 'foreign_key_constraints' => _env('DB_FOREIGN_KEYS', true), + ], + + 'mysql' => [ + 'driver' => 'mysql', + 'url' => _env('DATABASE_URL'), + 'host' => _env('DB_HOST', '127.0.0.1'), + 'port' => _env('DB_PORT', '3306'), + 'database' => _env('DB_DATABASE', 'test'), + 'username' => _env('DB_USERNAME', 'test'), + 'password' => _env('DB_PASSWORD', ''), + 'unix_socket' => _env('DB_SOCKET', ''), + 'charset' => _env('DB_CHARSET', 'utf8mb4'), + 'collation' => _env('DB_COLLATION', 'utf8mb4_unicode_ci'), + 'prefix' => '', + 'prefix_indexes' => true, + 'strict' => true, + 'engine' => null, + 'options' => extension_loaded('pdo_mysql') ? array_filter([ + PDO::MYSQL_ATTR_SSL_CA => _env('MYSQL_ATTR_SSL_CA'), + ]) : [], + ], + + 'pgsql' => [ + 'driver' => 'pgsql', + 'url' => _env('DATABASE_URL'), + 'host' => _env('DB_HOST', '127.0.0.1'), + 'port' => _env('DB_PORT', '5432'), + 'database' => _env('DB_DATABASE', 'forge'), + 'username' => _env('DB_USERNAME', 'forge'), + 'password' => _env('DB_PASSWORD', ''), + 'charset' => _env('DB_CHARSET', 'utf8'), + 'prefix' => '', + 'prefix_indexes' => true, + 'schema' => 'public', + 'sslmode' => 'prefer', + ], + + 'sqlsrv' => [ + 'driver' => 'sqlsrv', + 'url' => _env('DATABASE_URL'), + 'host' => _env('DB_HOST', 'localhost'), + 'port' => _env('DB_PORT', '1433'), + 'database' => _env('DB_DATABASE', 'forge'), + 'username' => _env('DB_USERNAME', 'forge'), + 'password' => _env('DB_PASSWORD', ''), + 'charset' => _env('DB_CHARSET', 'utf8'), + 'prefix' => '', + 'prefix_indexes' => true, + ], + ], + + /* + |-------------------------------------------------------------------------- + | Default Queue Table + |-------------------------------------------------------------------------- + | + | This is the name of the table that will be used to store queue jobs. + | You may change the table name to anything you like. If you change + | the table name, don't forget to change it in the migration file. If you + | use redis, this will be the name of the list that will be used to store + | queue jobs. + | + */ + 'table' => 'leafphp_main_jobs', + + /* + |-------------------------------------------------------------------------- + | Worker config + |-------------------------------------------------------------------------- + | + | This section sets up the configuration for your worker. This config + | is used by default when you dispatch a job. You can override this + | config by passing a config array to the dispatch method. + | + */ + 'workerConfig' => [ + /* + |-------------------------------------------------------------------------- + | Job execution delay + |-------------------------------------------------------------------------- + | + | This option allows you to set a delay for when a job should be executed. + | This is useful for scheduling jobs to run at a later time. + | + */ + 'delay' => 0, + + /* + |-------------------------------------------------------------------------- + | Delay before retry + |-------------------------------------------------------------------------- + | + | This option allows you to set a delay for when a job should be retried. + | This is useful when you need to setup something before retrying a job. + | + */ + 'delayBeforeRetry' => 0, + + /* + |-------------------------------------------------------------------------- + | Expire time + |-------------------------------------------------------------------------- + | + | This option allows you to set a time limit for how long a job should + | be kept in the queue. This is useful for archiving old jobs that you + | may need to reference later for data or other purposes. + | + */ + 'expire' => 60, + + /* + |-------------------------------------------------------------------------- + | Force job execution + |-------------------------------------------------------------------------- + | + | This option allows you to force a job to run even if it has expired or + | reached its retry limit. This is useful for jobs that you need to run + | at all costs. + | + */ + 'force' => false, + + /* + |-------------------------------------------------------------------------- + | Memory limit + |-------------------------------------------------------------------------- + | + | This option allows you to set a memory limit for the worker. This is + | useful for preventing memory leaks and other memory related issues. + | + */ + 'memory' => 128, + + /* + |-------------------------------------------------------------------------- + | Quit when queue is empty + |-------------------------------------------------------------------------- + | + | This option allows you to set whether the worker should quit when the + | queue is empty. By default, the worker will keep running even when + | the queue is empty. You can set this to true to make the worker quit + | when the queue is empty. + | + */ + 'quitOnEmpty' => false, + + /* + |-------------------------------------------------------------------------- + | Worker sleep time + |-------------------------------------------------------------------------- + | + | This option allows you to set how long the worker should sleep when + | the queue is empty. This is useful for preventing the worker from + | consuming too much CPU when the queue is empty. + | + */ + 'sleep' => 3, + + /* + |-------------------------------------------------------------------------- + | Queue timeout + |-------------------------------------------------------------------------- + | + | This option allows you to set a timeout for the queue. This is useful + | for preventing the queue from running for too long. + | + */ + 'timeout' => 60, + + /* + |-------------------------------------------------------------------------- + | Job retry limit + |-------------------------------------------------------------------------- + | + | This option allows you to set a retry limit for a job. This is useful + | for preventing a job from running too many times. + | + */ + 'tries' => 3, + ], +];