### Example Process Method for Laravel One-Time Operation Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md An example demonstrating how to implement custom logic within the `process()` method of a Laravel one-time operation. This specific example updates the status of active users to 'awesome'. ```php // operations/XXXX_XX_XX_XXXXXX_awesome_operation.php public function process(): void { User::where('active', 1)->update(['status' => 'awesome']) // make active users awesome } ``` -------------------------------- ### Example Deployment Script with Tagged Operations (Bash) Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt An example deployment script demonstrating the use of tagged operations. It first processes operations tagged 'before-migrations', then runs database migrations, and finally processes any remaining operations. ```bash # Deployment pipeline php artisan operations:process --tag=before-migrations php artisan migrate php artisan operations:process # Processes all remaining operations ``` -------------------------------- ### Install Laravel One-Time Operations Package Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Installs the 'laravel-one-time-operations' package using Composer and creates the necessary database table via Artisan migration. ```shell composer require timokoerber/laravel-one-time-operations php artisan migrate ``` -------------------------------- ### Integrate Operations into Deployment Pipeline Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt This bash script demonstrates how to integrate one-time operations into a CI/CD deployment process. It includes steps for pulling code, installing dependencies, clearing caches, running tagged operations before migrations, executing database migrations, processing any remaining operations, and restarting queue workers. ```bash #!/bin/bash # deploy.sh - Example deployment script # Pull latest code git pull origin main # Install dependencies composer install --no-dev --optimize-autoloader # Clear caches php artisan config:cache php artisan route:cache php artisan view:cache # Run pre-migration operations php artisan operations:process --tag=before-migrations --sync # Run database migrations php artisan migrate --force # Run all remaining operations (async by default) php artisan operations:process # Restart queue workers to pick up new jobs php artisan queue:restart ``` -------------------------------- ### Synchronous Operation Example (PHP) Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt Example of a PHP class extending OneTimeOperation to perform a critical schema update synchronously. This operation blocks deployment until completion. It uses Laravel's DB facade within a transaction to ensure atomicity. ```php where('key', 'api_version')->update([ 'value' => '2.0', 'updated_at' => now(), ]); DB::table('feature_flags')->insert([ 'name' => 'new_api_enabled', 'enabled' => true, 'created_at' => now(), ]); }); } }; ``` -------------------------------- ### Programmatic Access to Operations with OneTimeOperationManager Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt The `OneTimeOperationManager` class offers static methods for programmatic control over operations, ideal for custom scripts or testing. It can retrieve unprocessed or all operation files, get operation class instances or their corresponding models by name, check for the existence of operation files, and retrieve configuration details like the operations directory path and the database table name. ```php getOperationName(); // '2024_01_15_150000_migrate_user_data' } // Get all operation files (regardless of status) $allFiles = OneTimeOperationManager::getAllOperationFiles(); // Get operation class instance by name $operation = OneTimeOperationManager::getClassObjectByName('2024_01_15_150000_migrate_user_data'); echo $operation->isAsync(); // true echo $operation->getQueue(); // 'default' echo $operation->getTag(); // 'user-migration' // Get operation model by name $model = OneTimeOperationManager::getModelByName('2024_01_15_143052_update_user_statuses'); // Check if operation file exists $exists = OneTimeOperationManager::fileExistsByName('2024_01_15_143052_update_user_statuses'); // Get operations directory path $directory = OneTimeOperationManager::getDirectoryPath(); // '/var/www/app/operations/' // Get configured table name $table = OneTimeOperationManager::getTableName(); // 'operations' ``` -------------------------------- ### Show Operations with Filters using Shell Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md The `operations:show` command lists all operations, with options to filter by their status: `pending`, `processed`, or `disposed`. This helps in managing and tracking the state of operations without direct database checks. ```shell php artisan operations:show pending // show only pending operations ``` ```shell php artisan operations:show pending disposed // show only pending and disposed operations ``` -------------------------------- ### Integrate Operations into Deployment Script Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Demonstrates how to include the one-time operations processing command in a deployment script, ensuring operations run after code deployment and database migrations. ```shell ... - php artisan migrate - php artisan operations:process ... ``` -------------------------------- ### Show All Operations Status (Bash) Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt Command to list all defined one-time operations and their current status: pending, processed, or disposed. This helps in monitoring the execution state of all operations. ```bash # Show all operations php artisan operations:show # Output: # 2024_01_15_143052_update_user_statuses ................ PROCESSED # 2024_01_15_150000_migrate_user_data ................... PENDING # 2024_01_10_120000_old_cleanup ......................... DISPOSED ``` -------------------------------- ### Force Sync/Async Execution (Bash) Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt Commands to override the default asynchronous behavior of operations. `--sync` forces all operations to run synchronously, while `--async` forces them to run asynchronously, regardless of their individual `$async` property settings. ```bash # Force all operations to run synchronously (ignore $async setting) php artisan operations:process --sync # Force all operations to run asynchronously (ignore $async setting) php artisan operations:process --async ``` -------------------------------- ### Publish and Edit Configuration File Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Publishes the configuration file for the one-time operations package, allowing customization of the operations directory and database table name. ```shell php artisan vendor:publish --provider="TimoKoerber\LaravelOneTimeOperations\Providers\OneTimeOperationsServiceProvider" ``` -------------------------------- ### Create Custom Operation Templates Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt You can define custom operation templates by creating a stub file named `one-time-operation.stub` in your project's root `/stubs` directory. This allows you to pre-configure properties like `$async`, `$queue`, and `$tag`, and to implement custom logic within the `process` method, including error handling and logging. ```php execute(); OperationLogger::success($this); } catch (\Throwable $e) { OperationLogger::failure($this, $e); throw $e; } } private function execute(): void { // Operation logic here } }; ``` -------------------------------- ### Run All Tests using Shell Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md The `composer test` command is used to execute all the test suites for the package. This is a standard practice for ensuring the code base is functioning as expected. ```shell composer test ``` -------------------------------- ### Default Configuration for One-Time Operations Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md The default configuration settings for the one-time operations package, specifying the directory for operation files and the name of the database table used to track processed operations. ```php // config/one-time-operation.php return [ 'directory' => 'operations', 'table' => 'operations', ]; ``` -------------------------------- ### Configure Operation Class Properties Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt Demonstrates how to configure properties within a `OneTimeOperation` class. This includes setting `$async` for asynchronous processing, `$queue` for dispatching jobs to a specific queue, and `$tag` for filtering operations. ```php update([ 'status' => 'active', 'migrated_at' => now(), ]); // Log completion for monitoring logger()->info('User migration completed', [ 'count' => User::where('status', 'active')->count() ]); } }; ``` -------------------------------- ### Generated Essential One-Time Operation File Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt A minimal template for a one-time operation file. It only includes the essential `process()` method, suitable for operations that do not require asynchronous processing, specific queues, or tags. ```php php artisan operations:make -e|--essential php artisan make:operation ``` -------------------------------- ### Test Operations Repeatedly using Shell Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md The `--test` flag allows for repeated execution of operations. This is beneficial during development to test an operation multiple times before marking it as processed, ensuring its reliability. ```shell php artisan operations:process --test ``` -------------------------------- ### Create Essential One-Time Operation File in Laravel Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Generates a simplified one-time operation file using the `--essential` or `-e` flag with the artisan command. This creates a cleaner file when not all default attributes are needed. ```shell php artisan operations:make AwesomeOperation --essential php artisan operations:make AwesomeOperation -e ``` -------------------------------- ### Process One-Time Operations Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Executes all pending one-time operations. Supports various options for synchronous/asynchronous processing, testing, isolation, queue specification, tagging, and re-running specific operations. ```shell php artisan operations:process php artisan operations:process --sync php artisan operations:process --async php artisan operations:process --test php artisan operations:process --isolated php artisan operations:process --queue= php artisan operations:process --tag= php artisan operations:process ``` -------------------------------- ### Generated Standard One-Time Operation File Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt A template for a standard one-time operation file. It includes properties for asynchronous processing, queue assignment, and tag filtering, along with the required `process()` method for defining the operation's logic. ```php 'operations', 'table' => 'operations', 'connection' => null, ]; ``` -------------------------------- ### Tag Filtering for Operations (Bash) Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt Commands to process only operations that have specific tags assigned. This is useful for orchestrating operations, such as running certain tasks before or after database migrations. Multiple tags can be specified. ```bash # Process only operations tagged 'before-migrations' php artisan operations:process --tag=before-migrations # Process operations with multiple tags php artisan operations:process --tag=critical --tag=user-data ``` -------------------------------- ### Force Asynchronous or Synchronous Processing in Laravel Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Allows forcing the execution mode of one-time operations during processing. Use `--async` to force asynchronous dispatching (using `dispatch()`) or `--sync` to force synchronous execution (using `dispatchSync()`), overriding the `$async` attribute in the operation file. ```shell php artisan operations:process --async // force dispatch() php artisan operations:process --sync // force dispatchSync() ``` -------------------------------- ### Process All One-Time Operations in Laravel Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Executes all pending one-time operation files. Processed operations are recorded in the `operations` table and will not be processed again. This command is typically part of the deployment process. ```shell php artisan operations:process ``` -------------------------------- ### Isolated Execution of Laravel Operations on Multi-Server Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Ensures that the `operations:process` command runs as a single instance across multiple servers when using a Multi-Server Architecture. This is achieved by utilizing the `--isolated` option, leveraging Laravel's Isolatable Commands feature. ```shell php artisan operations:process --isolated ``` -------------------------------- ### Re-run Specific Operation (Bash) Source: https://context7.com/timokoerber/laravel-one-time-operations/llms.txt Command to re-process a specific one-time operation using its filename. This is useful for fixing failed operations or re-running them after code changes. The user is prompted to confirm re-execution. ```bash # Re-run a specific operation php artisan operations:process 2024_01_15_143052_update_user_statuses # Output: # Operation was processed before. Process it again? (yes/no) [no]: yes # Processing operation 2024_01_15_143052_update_user_statuses. # 2024_01_15_143052_update_user_statuses ................. DONE # Processing finished. ``` -------------------------------- ### Show One-Time Operations Status Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Displays the status of one-time operations. Allows filtering to show all, pending, processed, or disposed operations, and supports combining multiple filters. ```shell php artisan operations:show php artisan operations:show pending php artisan operations:show processed php artisan operations:show disposed php artisan operations:show pending processed disposed ``` -------------------------------- ### Filter Operations by Tag using PHP and Shell Source: https://github.com/timokoerber/laravel-one-time-operations/blob/main/README.md Operations can be tagged in their PHP definition and then filtered during processing using the `--tag` option in the Artisan command. This allows for selective execution of operations, useful for staged deployments or specific tasks. ```php first(); // Check operation details echo $operation->name; // '2024_01_15_143052_update_user_statuses' echo $operation->dispatched; // 'async' or 'sync' echo $operation->processed_at; // Carbon datetime instance echo $operation->file_path; // Full path to operation file // Get all processed operations $processedOperations = Operation::all(); // Check if operation exists (was processed) $wasProcessed = Operation::whereName('2024_01_15_143052_update_user_statuses')->exists(); // Manually store an operation (used internally) Operation::storeOperation('custom_operation_name', async: true); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.