After_party

repository·master·Indexed 42 days ago

https://github.com/thestevemitchell/after_party

A tool for managing automated data-related deployment tasks in Rails applications (compatible with Rails 3.1+). It allows developers to run Ruby-based data updates after database schema migrations have been completed, supporting both ActiveRecord and Mongoid. It includes generators for task creation, Rake commands for execution and status tracking, and integration options for Capistrano.

Tokens
2.3K
Snippets
12
Records
17
Agent score
54%

What's inside After_party

  1. Best practices and caveats for After_party tasks

    master

    Idempotency

    Crucial: Tasks may fail halfway through and will not record progress. Because these are often data updates, rollbacks are not supported. Always design your tasks to be idempotent, meaning they can be run multiple times without causing side effects or errors.

    Data vs. Schema Migrations

    • Schema Migrations: Used for structural changes to the database. Use these if you need to manipulate data during a structural change (e.g., populating a new column) and can handle model-DB mismatches.
    • After_party Tasks: Intended for data migrations. Use these for complex logic, external data imports, or cleaning up invalid data using Ruby code and Rails models. They run after migrations are complete, so your models will always match the current schema.
  2. Create a new deploy task

    master

    Use the generator to create a new deployment task. This creates a timestamped rake task in lib/tasks/deployment/.

    rails generate after_party:task task_name --description=optional_description_of_the_task

    Example output path: lib/tasks/deployment/20130130215258_task_name.rake

    rails generate after_party:task task_name --description=optional_description_of_the_task
  3. Automate After_party with Capistrano

    master

    To ensure tasks run automatically after migrations during deployment, add a task to your config/deploy.rb.

    Synchronous execution (Blocks deployment until finished):

    # 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'

    Asynchronous execution (Does not block deployment):

    Use nohup to start the task in the background. This is useful for long-running tasks where you don't want to halt the deployment process.

    # config/deploy.rb
    namespace :deploy do
      task :after_party, :roles => :app, :only => { :primary => true }  do
        run "cd #{release_path} ; nohup #{rake} after_party:run RAILS_ENV=#{rails_env} > #{current_path}/log/after_party.log  2>&1 &", :pty => false
      end
    end
    
    after 'deploy:migrate', 'deploy:after_party'
  4. Install After_party in a Rails application

    master

    After_party is compatible with Rails 3.1 or above.

    For ActiveRecord users:

    1. Add gem 'after_party' to your Gemfile and run bundle install.
    2. Run the install generator to create the initializer and database migration:
      rails generate after_party:install
      rake db:migrate

    For Mongoid users:

    1. Add gem 'after_party' to your Gemfile and run bundle install.
    2. Run the install generator with the mongoid argument:
      rails generate after_party:install mongoid
    # Gemfile
    gem 'after_party'
  5. Run and manage After_party tasks

    master

    After_party tasks are managed via rake commands:

    • Run all pending tasks: Executes all tasks that have not yet been recorded in the environment, in order of their timestamp. Each task is recorded in the database upon completion.
      rake after_party:run
    • Check task status: Provides a migration-like overview of which tasks have been run.
      rake after_party:status
    • Run a specific task manually: Since tasks are enhanced rake tasks, you can call them directly by their name.
      rake after_party:task_name
    rake after_party:run
    rake after_party:status
    rake after_party:task_name
  6. Configure a task to run on every deployment

    master

    By default, After_party tasks run only once per environment. To force a task to run with every deployment (or every time after_party:run is called), locate the generated rake file in lib/tasks/deployment/ and comment out the line that records the task as completed:

    # update task as completed.  If you remove the line below, the task will run with every deploy (or every time you call after_party:run)
    # AfterParty::TaskRecord.create :version => '<%= timestamp %>'
    # update task as completed.  If you remove the line below, the task will run with every deploy (or every time you call after_party:run)
    AfterParty::TaskRecord.create :version => '<%= timestamp %>'
  7. How After_party identifies pending deployment tasks

    master

    After_party identifies tasks by scanning the lib/tasks/deployment/ directory for .rake files that follow a specific naming convention. A task is considered 'pending' if it has a valid timestamp in its filename and that timestamp has not been recorded as completed in the TaskRecord system.

    Task Filename Format: Tasks must be named using the pattern {timestamp}_{task_name}.rake (e.g., 1715678400_cleanup_logs.rake).

    • Timestamp: A numeric value used to determine the order of execution and track completion.
    • Task Name: The identifier for the task.

    When AfterParty::TaskRecorder.pending_files is called, it returns a list of TaskRecorder instances for all matching files that are not yet marked as completed, sorted by their timestamp.

    # Example of a valid task filename pattern:
    # 1715678400_my_deployment_task.rake
  8. Install AfterParty via Rails Generator

    master

    To set up AfterParty in a Rails application, use the provided Rails generator. This will create the necessary configuration initializer and, if you are using ActiveRecord, the required database migration file.

    By default, the generator assumes you are using active_record as your ORM. If you are using a different ORM, you can specify it as an argument.

    # Run the generator in your Rails root
    rails generate after_party:install
    
    # Or specify a different ORM if necessary
    rails generate after_party:install my_orm_name
  9. Generate a new deployment task using TaskGenerator

    master

    You can use the AfterParty::Generators::TaskGenerator to scaffold new automated deployment tasks. This generator creates a new .rake file in lib/tasks/deployment/ using a timestamped filename to prevent collisions.

    To ensure the task is unique, the generator checks existing files in lib/tasks/deployment/*.rake via AfterParty::TaskRecorder. If a task name collision is detected, the generation will fail with an error message: Error: Please Create a unique task name.

    # Usage via Rails generator CLI (conceptual)
    rails generate after_party:task my_new_task --description="My custom task description"