Skip to content

Commit

Permalink
feat: add config generation command
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Sep 12, 2023
1 parent 0ea5ec7 commit 721a228
Show file tree
Hide file tree
Showing 2 changed files with 275 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Queue/Commands/QueueConfigCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Leaf\Queue\Commands;

use Aloe\Command;
use Aloe\Core;

class QueueConfigCommand extends Command
{
protected static $defaultName = 'queue:config';
public $description = 'Generate queue config';
public $help = 'Generate queue config';

protected function handle()
{
$this->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;
}
}
235 changes: 235 additions & 0 deletions src/Queue/Commands/stubs/config.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Adapter Config
|--------------------------------------------------------------------------
|
| Leaf allows you to use different queue adapters through the same API,
| giving you convenient access to various queue backends using the same
| syntax for each one. Here you may set the default queue adapter.
|
| Supported: "redis", "db". Others will be added in the future.
|
*/
'adapter' => '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,
],
];

0 comments on commit 721a228

Please sign in to comment.