### Full setup workflow Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Example of the full setup workflow for the After Party gem. ```bash # Full setup workflow rails generate after_party:install rake db:migrate rails generate after_party:task initial_setup --description="Initial setup" # Edit lib/tasks/deployment/*_initial_setup.rake rake after_party:run ``` -------------------------------- ### Installation Steps (ActiveRecord) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Step-by-step guide to install after_party with ActiveRecord. ```bash # 1. Generate the installer rails generate after_party:install # 2. Review the generated initializer and migration cat config/initializers/after_party.rb cat db/migrate/*_create_task_records.rb # 3. Run the migration rake db:migrate # 4. Create your first task rails generate after_party:task example_task --description="Example task" # 5. During deployment, run: rake after_party:run ``` -------------------------------- ### Initial Setup Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Steps to add and install the After Party gem. ```bash # 1. Add to Gemfile gem 'after_party' # 2. Install bundle install # 3. Generate setup files rails generate after_party:install rake db:migrate ``` -------------------------------- ### Installation Steps (Mongoid) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Step-by-step guide to install after_party with Mongoid. ```bash # 1. Generate the installer with Mongoid rails generate after_party:install mongoid # 2. Review the generated initializer cat config/initializers/after_party.rb # 3. No migration needed for Mongoid # 4. Create your first task rails generate after_party:task example_task --description="Example task" # 5. During deployment, run: rake after_party:run ``` -------------------------------- ### Initial Setup Task Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Example of a task to create default roles, users, or settings on first deploy. ```ruby # Create default roles, users, or settings on first deploy ``` -------------------------------- ### Install the Gem Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Install the gem using bundler. ```bash bundle install ``` -------------------------------- ### ORM Setup in Initializer Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Example of setting up the ORM in config/initializers/after_party.rb before TaskRecorder is used. ```ruby # config/initializers/after_party.rb AfterParty.setup do |config| require 'after_party/active_record.rb' # MUST come before TaskRecorder is used end ``` -------------------------------- ### Deployment workflow example: Create and run a new task Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/rake-tasks.md Step-by-step guide for creating, testing, and deploying a new After Party task. ```bash # 1. Create a new task rails generate after_party:task email_migration --description="Migrate email provider" # 2. Implement the task # Edit lib/tasks/deployment/*_email_migration.rake # 3. Test locally rake after_party:email_migration # 4. Re-test (verify idempotency) rake after_party:email_migration # 5. Check status before deployment rake after_party:status # 6. Commit and push git add . git commit -m "Add email migration task" git push origin main # 7. During deployment (automatic via Capistrano) cap production deploy # Capistrano runs: rake after_party:run RAILS_ENV=production # 8. Verify task completed cap production invoke "RAILS_ENV=production rake after_party:status" ``` -------------------------------- ### Feature Flag Setup Task Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Example of a task to enable or disable features per environment. ```ruby # Enable/disable features per environment ``` -------------------------------- ### Installation Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Steps to add the after_party gem to your Gemfile, install it, and run migrations. ```bash # Add to Gemfile gem 'after_party' # Install and initialize bundle install rails generate after_party:install rake db:migrate ``` -------------------------------- ### Custom Primary Key Setup Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Example of setting up the task_records table without an auto-increment primary key. ```ruby create_table :task_records, id: false do |t| t.string :version, null: false end ``` -------------------------------- ### Installation Commands Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/DOCUMENTATION_INDEX.md Commands to install the After Party gem and generate initial configuration files. ```bash gem 'after_party' bundle install rails generate after_party:install rake db:migrate ``` -------------------------------- ### Migration Installation (Mongoid) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-record.md Command to install the TaskRecord migration for Mongoid. ```bash rails generate after_party:install mongoid ``` -------------------------------- ### Mongoid Configuration Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Example `config/mongoid.yml` showing database and host settings for different environments. ```yaml development: clients: default: database: myapp_development hosts: - localhost:27017 test: clients: default: database: myapp_test hosts: - localhost:27017 production: clients: default: database: myapp_production hosts: - prod-mongo-1.example.com:27017 - prod-mongo-2.example.com:27017 ``` -------------------------------- ### Migration Installation (Rails) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-record.md Command to install the TaskRecord migration for Rails. ```bash rails generate after_party:install rake db:migrate ``` -------------------------------- ### Usage Example: ActiveRecord Configuration Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/afterparty-module.md Example of configuring AfterParty with ActiveRecord. ```ruby # Configure with ActiveRecord (default) AfterParty.setup do |config| require 'after_party/active_record.rb' end ``` -------------------------------- ### Example Recovery Steps Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Command-line examples demonstrating the steps to recover from a failed task. ```bash # Examine the error tail -100 log/after_party.log # Fix the task implementation vim lib/tasks/deployment/*_broken_task.rake # Test the fix rake after_party:broken_task # Commit the fix git add -A git commit -m "Fix task error" git push # On next deployment, task runs again via: rake after_party:run ``` -------------------------------- ### Array of TaskRecorder Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/types.md Example showing the output of `pending_files` and how to iterate over it. ```ruby pending = AfterParty::TaskRecorder.pending_files # => [ # # # # ] pending.each do |recorder| puts "#{recorder.timestamp}: #{recorder.task_name}" end # => 20120205141454: m_three # => 20130205176456: z_first # => 20130207948264: a_second2 ``` -------------------------------- ### Usage Example: Mongoid Configuration Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/afterparty-module.md Example of configuring AfterParty with Mongoid. ```ruby # Configure with Mongoid AfterParty.setup do |config| require 'after_party/mongoid.rb' end ``` -------------------------------- ### ORM-Agnostic Usage Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/orm-integration.md Example of how TaskRecorder can be used with any ORM, demonstrating the ORM-agnostic approach. ```ruby # This works identically with either backend def pending? timestamp && !TaskRecord.completed_task?(timestamp) end ``` -------------------------------- ### Test Database Setup Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Commands to set up the test database and check After Party status. ```bash RAILS_ENV=test rake db:create db:migrate RAILS_ENV=test rake after_party:status ``` -------------------------------- ### Capistrano Integration - Typical Setup Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Typical Capistrano setup for running After Party tasks after deployment migrations. ```ruby # config/deploy.rb set :rake, "cd #{current_path} && bundle exec rake" namespace :deploy do task :after_party, roles: :web, only: { primary: true } do run "cd #{release_path} && RAILS_ENV=#{stage} bundle exec rake after_party:run" end end after 'deploy:migrate', 'deploy:after_party' ``` -------------------------------- ### Filename Anatomy Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-generator.md An example illustrating the structure of a generated filename. ```text [TIMESTAMP]_[TASK_NAME].rake Example: 20130130215258_create_admin_user.rake | | Timestamp Task Name ``` -------------------------------- ### ORM Selection (ActiveRecord) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Example of selecting ActiveRecord as the ORM in the initializer. ```ruby # ActiveRecord (default) AfterParty.setup do |config| require 'after_party/active_record.rb' end ``` -------------------------------- ### Installation - Gemfile Source: https://github.com/thestevemitchell/after_party/blob/master/README.md Add After_party to your Gemfile. ```ruby #Gemfile gem 'after_party' ``` -------------------------------- ### ActiveRecord Database Access Examples Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/orm-integration.md Examples of creating, querying, and deleting records using ActiveRecord. ```ruby # Creating a record task = AfterParty::TaskRecord.create(version: '20130130215258') # Querying tasks = AfterParty::TaskRecord.all tasks = AfterParty::TaskRecord.where(version: '20130130215258') # Deleting (for testing) AfterParty::TaskRecord.delete_all ``` -------------------------------- ### Idempotent Task Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Demonstrates an idempotent task that safely finds or creates a user, and a non-idempotent task that creates duplicates. ```ruby # ✓ Idempotent: finds or creates namespace :after_party do task create_default_user: :environment do User.find_or_create_by!(email: 'admin@example.com') do |user| user.role = 'admin' end AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end # ✗ Not idempotent: creates duplicates namespace :after_party do task bad_create_user: :environment do User.create!(email: 'admin@example.com', role: 'admin') AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Rails Logging Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Example of using Rails logger within an After Party task. ```ruby # In task implementation Rails.logger.info "Starting data migration" puts "Output to stdout" ``` -------------------------------- ### Reverse Tasks Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Illustrates how to implement rollback alternatives using separate 'apply' and 'revert' tasks. ```ruby # 20130130000000_apply_change.rake namespace :after_party do task apply_change: :environment do # Forward change User.update_all(verified: true) AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end # 20130131000000_revert_change.rake (if needed) namespace :after_party do task revert_change: :environment do # Reverse change User.update_all(verified: false) # Don't record this task if reverting previous one end end ``` -------------------------------- ### Mongoid Configuration Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/orm-integration.md Example Mongoid configuration in config/mongoid.yml for different environments. ```yaml development: clients: default: database: myapp_development hosts: - localhost:27017 test: clients: default: database: myapp_test hosts: - localhost:27017 production: clients: default: database: myapp_production hosts: - prod-mongo.example.com:27017 ``` -------------------------------- ### Implement Task Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Example implementation of a task to create initial users and roles, including recording task completion. ```ruby # lib/tasks/deployment/20130130215258_create_initial_data.rake namespace :after_party do desc 'Deployment task: Create initial users and roles' task create_initial_data: :environment do puts "Running deploy task 'create_initial_data'" # Implementation Role.find_or_create_by!(name: 'Admin') { |r| r.permissions = [:all] } Role.find_or_create_by!(name: 'User') { |r| r.permissions = [:read] } User.find_or_create_by!(email: 'admin@example.com') do |u| u.password = SecureRandom.hex(16) u.role = Role.find_by(name: 'Admin') end puts "Created initial data" # Record completion AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Manual Task Creation Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-generator.md Instructions and code to manually create a task file in the deployment directory. ```bash mkdir -p lib/tasks/deployment cat > lib/tasks/deployment/20130130215258_my_task.rake << 'EOF' namespace :after_party do desc 'Deployment task: my_task' task my_task: :environment do puts "Running deploy task 'my_task'" # Your code here AfterParty::TaskRecord .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end EOF ``` -------------------------------- ### Non-Idempotent Task Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-record.md An example of a non-idempotent task that should be avoided. ```ruby # This will create duplicate users if run twice User.create!(email: 'admin@example.com', role: 'admin') ``` -------------------------------- ### TaskRecorder Instance Attributes Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/types.md Example demonstrating the usage of TaskRecorder instance attributes and methods. ```ruby recorder = AfterParty::TaskRecorder.new('/app/lib/tasks/deployment/20130130215258_my_task.rake') puts recorder.filename # => "/app/lib/tasks/deployment/20130130215258_my_task.rake" puts recorder.timestamp # => "20130130215258" puts recorder.task_name # => "my_task" puts recorder.pending? # => true (if not yet completed) ``` -------------------------------- ### Task Name Argument Examples Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-generator.md Examples of providing task names as arguments to the generator. ```bash rails generate after_party:task create_admin_user rails generate after_party:task import_legacy_data ``` -------------------------------- ### ORM Selection (Mongoid) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Example of selecting Mongoid as the ORM in the initializer. ```ruby # OR Mongoid AfterParty.setup do |config| require 'after_party/mongoid.rb' end ``` -------------------------------- ### TaskRecorder.pending_files Usage Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-recorder.md Example of how to retrieve and iterate over pending deployment tasks. ```ruby # Get all pending tasks pending = AfterParty::TaskRecorder.pending_files pending.each do |recorder| puts "Task: #{recorder.task_name}" puts "Timestamp: #{recorder.timestamp}" puts "File: #{recorder.filename}" end # Output: # Task: create_admin_user # Timestamp: 20130130215258 # File: /app/lib/tasks/deployment/20130130215258_create_admin_user.rake ``` -------------------------------- ### Environment-Specific Rails Configuration Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Example of environment-specific Rails configuration. ```ruby # config/environments/production.rb Rails.application.configure do # Production-specific setup (if needed) end ``` -------------------------------- ### Capistrano Integration - Basic Setup Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Basic Capistrano configuration to integrate after_party tasks into the deployment process. ```ruby # config/deploy.rb set :rake, "cd #{current_path} && bundle exec rake" namespace :deploy do task :after_party, roles: :web, only: { primary: true } do run "cd #{release_path} && RAILS_ENV=#{stage} #{rake} after_party:run" end end after 'deploy:migrate', 'deploy:after_party' ``` -------------------------------- ### Integration with Deployment Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Example of how After Party integrates with deployment tools like Capistrano. ```bash # Ensure tasks run automatically cap production deploy # Capistrano runs all pending tasks ``` -------------------------------- ### Individual Task Execution example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/rake-tasks.md Example of executing a specific task file by its name. ```bash # Example task file: lib/tasks/deployment/20130130215258_create_admin_user.rake rake after_party:create_admin_user ``` -------------------------------- ### Usage - Check task status Source: https://github.com/thestevemitchell/after_party/blob/master/README.md Get a migration-like overview of your tasks. ```bash rake after_party:status ``` -------------------------------- ### Usage example for completed_task? Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-record.md Demonstrates how to use the completed_task? method after creating a TaskRecord and checking for an existing task. ```ruby # After a task completes AfterParty::TaskRecord.create(version: '20130130215258') # Check if task is completed AfterParty::TaskRecord.completed_task?('20130130215258') # => true AfterParty::TaskRecord.completed_task?('20130131000000') # => false ``` -------------------------------- ### Database Query Optimization - Efficient Batch Operation Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Example of an efficient batch operation for updating records. ```ruby # Efficient: batch operation namespace :after_party do task bulk_update: :environment do User.where(status: 'pending').update_all(status: 'active') AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Invalid Task Filename Examples Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Examples of filenames that do not match the expected format for AfterParty tasks. ```text # Invalid: missing timestamp create_admin_user.rake # Invalid: malformed timestamp 2013_01_30_task.rake 13_01_30_task.rake # Invalid: no underscore separator 20130130215258task.rake # Invalid: wrong extension 20130130215258_task.txt # Valid examples 20130130215258_create_admin.rake 20200101000000_import_data.rake ``` -------------------------------- ### Optional Task Execution - Commenting out TaskRecord.create Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-record.md Example showing how to make a task run on every deployment by commenting out the TaskRecord.create line. ```ruby namespace :after_party do desc 'Check system status' task check_system: :environment do puts "Checking system health..." # Task implementation puts "System is healthy" # Commented out to run every deployment: # AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Creating a TaskRecord instance Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-record.md Example of creating a new TaskRecord instance with a specific version. ```ruby AfterParty::TaskRecord.create(version: '20130130215258') ``` -------------------------------- ### Task Metrics Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Example of how to record metrics for a task, including processed records, duration, and timestamp. ```ruby namespace :after_party do task record_metrics: :environment do start_time = Time.current # Task implementation records_processed = process_records duration = Time.current - start_time # Record metrics metrics = { task_name: 'record_metrics', processed: records_processed, duration_seconds: duration.to_i, timestamp: Time.current } Rails.logger.info "Task metrics: #{metrics.inspect}" # Could send to monitoring service, statsd, etc. AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Interface Similarity Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/orm-integration.md Demonstrates the similar public interface for creating, checking completion, and retrieving task records in both implementations. ```ruby # Both support: AfterParty::TaskRecord.create(version: '20130130215258') AfterParty::TaskRecord.completed_task?('20130130215258') AfterParty::TaskRecord.all ``` -------------------------------- ### Idempotent Task Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-record.md An example of an idempotent task that is safe to run multiple times. ```ruby # This is safe to run multiple times User.where(email: 'admin@example.com').first_or_create!(role: 'admin') ``` -------------------------------- ### Example Task File Implementation Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-generator.md A generated task file demonstrating user migration and idempotency. ```ruby namespace :after_party do desc 'Deployment task: Migrate users from legacy system' task migrate_users: :environment do puts "Running deploy task 'migrate_users'" # Find all legacy users LegacyUser.find_each do |legacy| # Create new user only if doesn't exist (idempotent) User.find_or_create_by!(email: legacy.email) do |user| user.name = legacy.name user.imported_at = Time.current end end puts "Migrated #{LegacyUser.count} users" # Update task as completed AfterParty::TaskRecord .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Idempotency Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-generator.md Demonstrates idempotent vs. non-idempotent code for task implementation. ```ruby # ✓ Idempotent - safe to run multiple times User.find_or_create_by!(email: 'admin@example.com') # ✗ Not idempotent - creates duplicates User.create!(email: 'admin@example.com') ``` -------------------------------- ### Good - Idempotent Task Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Provides an example of an idempotent task that can be safely run multiple times without causing unintended side effects. ```ruby # GOOD - Idempotent namespace :after_party do task idempotent: :environment do User.find_or_create_by!(email: 'admin@example.com') do |user| user.role = 'admin' end AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end # Safe to run multiple times ``` -------------------------------- ### Idempotent Task Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Demonstrates an idempotent task that is safe to retry. ```ruby # ✓ Safe to retry User.find_or_create_by!(email: 'admin@example.com') ``` ```ruby # ✗ Not safe (creates duplicates) User.create!(email: 'admin@example.com') ``` -------------------------------- ### Example Usage with Mass Assignment (Rails 3.x) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/orm-integration.md Demonstrates creating a record in Rails 3.x with mass assignment. ```ruby # This works in Rails 3.x due to attr_accessible AfterParty::TaskRecord.create(version: '20130130215258') ``` -------------------------------- ### initializer Hook Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Initializes the task record model and any dependencies. ```ruby initializer 'load_task_record_models' do load 'after_party/models/task_recorder.rb' end ``` -------------------------------- ### Capistrano integration Source: https://github.com/thestevemitchell/after_party/blob/master/README.md Example of how to integrate After_party tasks into Capistrano's deploy.rb. ```ruby #config/deploy.rb namespace :deploy do task :after_party, :roles => :web, :only => { :primary => true } do run "cd #{release_path} && RAILS_ENV=#{stage} #{rake} after_party:run" end end after 'deploy:migrate', 'deploy:after_party' ``` -------------------------------- ### Usage in Code (Version) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/types.md Examples of deriving and using the version string in code. ```ruby # During task creation (in Rails generator) timestamp = Time.now.utc.strftime('%Y%m%d%H%M%S') # => "20130130215258" # In generated task file AfterParty::TaskRecord.create version: '20130130215258' # In TaskRecorder recorder = AfterParty::TaskRecorder.new(filename) recorder.timestamp # => "20130130215258" ``` -------------------------------- ### Incomplete Task Recording Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Demonstrates a scenario where a task might be marked as incomplete due to an error during TaskRecord.create. ```ruby namespace :after_party do task risky_recording: :environment do # Data migration succeeds User.update_all(verified: true) # But this fails: AfterParty::TaskRecord.create version: nil # Raises error! # Result: Task completes but isn't recorded as complete end end ``` -------------------------------- ### Railtie Configuration Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Example configuration for the AfterParty Railtie in `config/initializers/after_party.rb`. ```ruby # config/initializers/after_party.rb AfterParty.setup do |config| require 'after_party/active_record.rb' # or mongoid.rb end ``` -------------------------------- ### Generators Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Commands to generate After Party setup files and new tasks. ```bash rails generate after_party:install ``` ```bash rails generate after_party:install mongoid ``` ```bash rails generate after_party:task task_name --description="Description" ``` -------------------------------- ### Connection Pooling Configuration Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Example database.yml configuration for setting connection pool sizes for development and production environments. ```yaml # config/database.yml development: pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> production: pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 20 } %> ``` -------------------------------- ### Third-Party Integration Task Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Example of a task to fetch data from external APIs or set up webhooks. ```ruby # Fetch data from external APIs, set up webhooks ``` -------------------------------- ### Usage: Querying all completed tasks Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/types.md Example of retrieving all completed task versions. ```ruby # Get all completed task versions completed_versions = AfterParty::TaskRecord.all.map(&:version) # => ["20130130215258", "20130131000000", "20130201120000"] ``` -------------------------------- ### Usage: Creating a task record Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/types.md Example of creating a new task record when a task completes. ```ruby # When a task completes AfterParty::TaskRecord.create(version: '20130130215258') ``` -------------------------------- ### Example of ORM Configuration Mismatch Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Illustrates a scenario where the initializer configures Mongoid, but subsequent code attempts to use ActiveRecord, leading to errors. ```ruby # config/initializers/after_party.rb loads Mongoid AfterParty.setup do |config| require 'after_party/mongoid.rb' end # But migration tries to use ActiveRecord rake db:migrate # Fails because Mongoid doesn't create the migration # Or TaskRecord tries to use ActiveRecord features AfterParty::TaskRecord.create version: '20130130215258' # May fail ``` -------------------------------- ### Rake Task Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/rake-tasks.md Example of how to test the 'after_party:my_task' Rake task. ```ruby describe 'rake after_party:my_task' do it 'successfully completes' do expect { Rake::Task['after_party:my_task'].invoke }.to change { TaskRecord.count }.by(1) end end ``` -------------------------------- ### Making Tasks Run Every Deployment Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-generator.md Example of how to configure a task to run on every deployment by commenting out the TaskRecord.create line. ```ruby namespace :after_party do desc 'Deployment task: Send deployment notification' task notify_deployment: :environment do puts "Running deploy task 'notify_deployment'" # Send notification to team NotificationService.deploy_completed # Uncomment to run on every deploy: # AfterParty::TaskRecord # .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Pre-Deployment Checklist: Test Locally Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Command to test After Party tasks locally in a test environment. ```bash RAILS_ENV=test rake db:create db:migrate RAILS_ENV=test rake after_party:test_task_name ``` -------------------------------- ### Conditional Loading Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Example of conditionally loading AfterParty based on the Rails environment. ```ruby # config/initializers/after_party.rb if Rails.env.production? || Rails.env.staging? AfterParty.setup do |config| require 'after_party/active_record.rb' end end ``` -------------------------------- ### Overriding Loaded Files Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Example of overriding the `FILE_MASK` in `TaskRecorder` for custom behavior. ```ruby # config/initializers/after_party_customization.rb # (Must be alphabetically after after_party.rb) # Override behavior if needed module AfterParty class TaskRecorder FILE_MASK = '/custom/path/*.rake' end end ``` -------------------------------- ### Example Code Path for Filename Parsing Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Ruby code snippet illustrating how AfterParty's TaskRecorder parses filenames and handles mismatches. ```ruby def parse_filename /(\d+)_(.+)\.rake/.match(Pathname(@filename).basename.to_s) do |m| @timestamp = m[1] @task_name = m[2] end # If no match, @timestamp and @task_name remain nil end def pending? timestamp && !TaskRecord.completed_task?(timestamp) # Returns false if timestamp is nil end ``` -------------------------------- ### AfterParty.setup Configuration Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/afterparty-module.md Configures the AfterParty gem with the desired ORM (Object-Relational Mapping) backend. ```ruby AfterParty.setup do |config| require 'after_party/active_record.rb' end ``` -------------------------------- ### after_party:status usage example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/rake-tasks.md Example of checking task status and its expected output. ```bash # Check task status rake after_party:status # Output example: # Status Task ID Task Name # -------------------------------------------------- # down 20130130215258 Create admin user # down 20130131000000 Import data # up 20130131120000 Fix emails ``` -------------------------------- ### Basic Usage (ActiveRecord) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Invokes the InstallGenerator for ActiveRecord. ```bash rails generate after_party:install ``` -------------------------------- ### task_name Attribute Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-recorder.md Example of accessing the task_name attribute. ```ruby recorder.task_name # => "task_name" ``` -------------------------------- ### timestamp Attribute Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-recorder.md Example of accessing the timestamp attribute. ```ruby recorder.timestamp # => "20130130215258" ``` -------------------------------- ### Initializer Configuration (Default ActiveRecord) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Default configuration for the after_party gem using ActiveRecord. ```ruby AfterParty.setup do |config| # ==> ORM configuration # Load and configure the ORM. Supports :active_record (default) and # :mongoid (bson_ext recommended) by default. Other ORMs may be # available as additional gems. require 'after_party/active_record.rb' # or: require 'after_party/mongoid.rb' end ``` -------------------------------- ### filename Attribute Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-recorder.md Example of accessing the filename attribute. ```ruby recorder.filename # => "/workspace/home/after_party/lib/tasks/deployment/20130130215258_task_name.rake" ``` -------------------------------- ### Database Connection Error Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Example of an error message when the database is not accessible during task loading. ```text ActiveRecord::NoDatabaseError: could not translate host name "db.example.com" to address ``` -------------------------------- ### Typical Scenario for 'no pending tasks to run' Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Shows the expected output when all deployment tasks have been completed. ```bash $ rake after_party:run no pending tasks to run ``` -------------------------------- ### Usage - Run deploy tasks Source: https://github.com/thestevemitchell/after_party/blob/master/README.md Run all deploy tasks that have not been recorded yet. ```bash rake after_party:run ``` -------------------------------- ### Private Method: timestamp Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Generates a timestamp for migration filenames. ```ruby def timestamp @timestamp ||= Time.now.utc.strftime('%Y%m%d%H%M%S') end ``` -------------------------------- ### Task Process Fails Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Example of a rake task that raises an unhandled exception during execution. ```ruby namespace :after_party do task broken_task: :environment do # This will fail raise "Critical error occurred!" # This line never executes AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Generated Timestamps Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/types.md Example of generating a timestamp using Rails Time utilities. ```ruby Time.now.utc.strftime('%Y%m%d%H%M%S') # => "20130130215258" ``` -------------------------------- ### TaskRecord Hash Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/types.md An example of the data structure returned by `rake after_party:status`. ```ruby { version: '20130130215258', task_name: 'Create Admin User', status: 'down' } ``` -------------------------------- ### pending? Method Usage Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-recorder.md Example of checking if a task is pending execution. ```ruby recorder = AfterParty::TaskRecorder.new('/app/lib/tasks/deployment/20130130215258_create_admin_user.rake') if recorder.pending? puts "Task has not run yet in this environment" else puts "Task already executed" end ``` -------------------------------- ### Example of Deployment Tool Integration Issue (Incorrect Configuration) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Illustrates common misconfigurations in deployment tools like Capistrano that can prevent After Party tasks from running. ```ruby # Missing after_party task definition # config/deploy.rb doesn't have: # task :after_party do ... # after 'deploy:migrate', 'deploy:after_party' # Wrong environment variable run "rake after_party:run" # Missing RAILS_ENV # Wrong directory run "rake after_party:run" # Not in release_path ``` -------------------------------- ### Deployment Workflow Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md A step-by-step workflow for using After Party during development and deployment. ```text 1. Developer creates task └─> rails generate after_party:task name 2. Develop and test locally └─> rake after_party:TASK_NAME 3. Commit and push └─> git push origin feature-branch 4. Deploy (automatic) └─> cap production deploy └─> Runs: rake db:migrate └─> Runs: rake after_party:run ├─> Discovers pending tasks ├─> Executes each task in order └─> Records completion 5. Verify └─> rake after_party:status ``` -------------------------------- ### parse_filename Method Usage Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-recorder.md Examples of valid and invalid filenames for the parse_filename method. ```ruby # Valid filenames: # 20130130215258_create_admin_user.rake # 20200101000000_import_data.rake # # Invalid filenames (result in nil values): # create_admin_user.rake (missing timestamp) # 2013_01_30_task.rake (wrong timestamp format) ``` -------------------------------- ### TaskRecorder.new Usage Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-recorder.md Example of creating a new TaskRecorder instance and accessing its attributes. ```ruby recorder = AfterParty::TaskRecorder.new('/app/lib/tasks/deployment/20130130215258_create_admin_user.rake') puts recorder.timestamp # => "20130130215258" puts recorder.task_name # => "create_admin_user" ``` -------------------------------- ### Private Method: migration_exists? Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Checks if a TaskRecord migration has already been created. ```ruby def migration_exists? absolute = File.expand_path('db/migrate/', destination_root) Dir.glob("#{absolute}/[0-9]*_create_#{table_name}.rb").first end ``` -------------------------------- ### Example Output of Task Failure Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Example output shown in the console when a rake task fails due to an unhandled exception. ```shell $ rake after_party:run Running deploy task 'task_one' ...success... Running deploy task 'task_two' rake aborted! StandardError: Critical error occurred! ``` -------------------------------- ### Memory Management with find_in_batches Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Demonstrates memory-efficient migration using `find_in_batches` and advises on `delete_all` vs. `destroy_all`. ```ruby namespace :after_party do task memory_efficient_migration: :environment do # Use find_in_batches to avoid loading all records Record.find_in_batches(batch_size: 1000) do |batch| batch.each { |record| record.process! } end # For bulk operations, use delete_all instead of destroy_all # delete_all is faster but doesn't trigger callbacks # Use destroy_all if callbacks are important # Clear query cache between batches if needed ActiveRecord::Base.connection_pool.with_connection do |conn| conn.execute 'RESET QUERY CACHE' if defined?(MySQL) end AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Initializer Configuration (Mongoid) Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Configuration for the after_party gem when using Mongoid. ```ruby # For Mongoid: AfterParty.setup do |config| require 'after_party/mongoid.rb' end ``` -------------------------------- ### Mongoid: Connection Error Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/orm-integration.md Example of a Mongoid connection error. ```text Mongoid::Errors::NoSessionsConfig ``` -------------------------------- ### With Description Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/task-generator.md Command to generate a new deployment task with a name and an optional description. ```bash rails generate after_party:task create_admin --description="Create default admin user" ``` -------------------------------- ### Batch Operations Best Practice Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Illustrates the efficiency of using batch operations for updates. ```ruby # Efficient User.where(status: 'pending').update_all(status: 'active') ``` ```ruby # Inefficient User.where(status: 'pending').each { |u| u.update!(status: 'active') } ``` -------------------------------- ### generators Hook Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Registers Rails generators with the framework. ```ruby generators do load 'generators/task/task_generator.rb' load 'generators/install/install_generator.rb' end ``` -------------------------------- ### ActiveRecord Migration Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/orm-integration.md Example migration for creating the task_records table. ```ruby # db/migrate/*_create_task_records.rb class CreateTaskRecords < ActiveRecord::Migration[4.2] # or [5.2], [6.0], [7.0] def change create_table :task_records, id: false do |t| t.string :version, null: false end end end ``` -------------------------------- ### Create Task Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Command to generate a new after_party task with a description. ```bash rails generate after_party:task create_initial_data --description="Create initial users and roles" ``` -------------------------------- ### Example of a task that fails Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/rake-tasks.md Illustrates how a task raising an exception behaves. ```ruby # This task will fail namespace :after_party do task :example_failure do raise "Something went wrong!" # TaskRecord.create never executes end end ``` -------------------------------- ### Example Rails Generate Commands Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Commands available after registering generators. ```bash rails generate after_party:install rails generate after_party:install mongoid rails generate after_party:task task_name ``` -------------------------------- ### Testing with ActiveRecord Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/orm-integration.md Commands to set up and run tests with the ActiveRecord backend. ```bash RAILS_ENV=test rake db:create RAILS_ENV=test rake db:migrate RAILS_ENV=test rake after_party:run ``` -------------------------------- ### Migration for Rails 5+ Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Database migration to create the task_records table for Rails 5 and later. ```ruby # Rails 5+ class CreateTaskRecords < ActiveRecord::Migration[4.2] def change create_table :task_records, id: false do |t| t.string :version, null: false end end end ``` -------------------------------- ### Environment-Specific Tasks Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Demonstrates how to create tasks that are specific to a particular Rails environment, like development. ```ruby # You can control execution based on environment namespace :after_party do desc 'Development-specific setup' task setup_development_data: :environment do if Rails.env.development? # Create test data, seed databases, etc. end AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### ORM Name Argument Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/install-generator.md Specifies the ORM backend for the generator. ```bash rails generate after_party:install active_record ``` ```bash rails generate after_party:install mongoid ``` -------------------------------- ### Creating a Task Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/README.md Generating and implementing a new After Party task. ```bash rails generate after_party:task create_admin_user --description="Create initial admin" ``` ```ruby namespace :after_party do desc 'Deployment task: Create initial admin' task create_admin_user: :environment do puts "Running deploy task 'create_admin_user'" # Implementation User.find_or_create_by!(email: 'admin@example.com') do |u| u.role = 'admin' end # Record completion (prevents re-run on next deploy) AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ``` -------------------------------- ### Log output from running tasks Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/rake-tasks.md Example of status messages generated during task execution. ```bash $ rake after_party:run Running deploy task 'create_admin_user' Created admin user: admin@example.com Running deploy task 'import_legacy_data' Imported 1250 records ``` -------------------------------- ### Pre-Deployment Checklist: Verify Idempotency Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/errors.md Commands to verify task idempotency by running a task twice. ```bash rake after_party:test_task_name rake after_party:test_task_name # Run again ``` -------------------------------- ### rake_tasks Hook Example Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/api-reference/railtie.md Loads the main rake task definitions for the after_party gem. ```ruby rake_tasks do load 'tasks/deploy_task_runner.rake' end ``` -------------------------------- ### Capistrano Integration - Asynchronous Task Execution Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/configuration.md Capistrano setup for running After Party tasks asynchronously in the background. ```ruby # config/deploy.rb namespace :deploy do task :after_party, roles: :app, only: { primary: true } do run "cd #{release_path} ; nohup bundle exec rake after_party:run RAILS_ENV=#{rails_env} > #{current_path}/log/after_party.log 2>&1 &", pty: false end end ``` -------------------------------- ### Status Reporting Source: https://github.com/thestevemitchell/after_party/blob/master/_autodocs/usage-patterns.md Example of a task that notifies a team via email upon completion. ```ruby namespace :after_party do task notify_completion: :environment do # Task implementation # Notify team of completion NotificationService.send_email( to: 'team@example.com', subject: 'Deployment tasks completed', body: "All after_party tasks completed successfully" ) AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp end end ```