๐Ÿ’Ž ๐Ÿ’Ž Async Operations in TuskLang - Ruby Edition

Ruby Documentation

๐Ÿ’Ž Async Operations in TuskLang - Ruby Edition

"We don't bow to any king" - Asynchronous Processing with Ruby Power

Async operations in TuskLang provide powerful asynchronous processing capabilities, allowing you to handle concurrent operations, background jobs, and real-time processing. In Ruby, this integrates seamlessly with Rails, Sidekiq, and provides advanced async patterns that go beyond traditional synchronous approaches.

๐Ÿš€ Basic Async Operations

Simple Async Tasks

require 'tusklang'

TuskLang configuration for basic async operations

tsk_content = <<~TSK [async_operations] # Simple async task execution async_task: @async.execute(() => { # Background task logic result = @expensive_operation() @log.info("Async task completed: " + result) return result }) # Async task with parameters async_task_with_params: @async.execute((params) => { user_id = params.user_id data = params.data # Process data asynchronously processed_data = @process_user_data(user_id, data) @notify_user(user_id, "Data processed successfully") return processed_data }, {user_id: @request.user_id, data: @request.data}) # Async task with callback async_task_with_callback: @async.execute(() => { return @expensive_operation() }).then((result) => { @log.info("Task completed with result: " + result) @send_notification(result) }).catch((error) => { @log.error("Task failed: " + error.message) @handle_error(error) }) # Parallel async execution parallel_tasks: @async.parallel([ () => @fetch_user_data(@request.user_id), () => @fetch_user_preferences(@request.user_id), () => @fetch_user_activity(@request.user_id) ]).then((results) => { user_data = results[0] preferences = results[1] activity = results[2] return { user: user_data, preferences: preferences, activity: activity } }) TSK

Ruby implementation

class AsyncTaskManager include TuskLang::Asyncable def execute_basic_async_tasks tusk_config = Rails.application.config.tusk_config # Execute simple async task task_result = tusk_config.execute_async_task('async_task') # Execute async task with parameters params = { user_id: 1, data: { name: 'John', email: 'john@example.com' } } param_result = tusk_config.execute_async_task('async_task_with_params', params) # Execute async task with callback callback_result = tusk_config.execute_async_task_with_callback('async_task_with_callback') do |result| puts "Task completed: #{result}" end # Execute parallel tasks parallel_results = tusk_config.execute_parallel_tasks('parallel_tasks') puts "Async tasks executed successfully" puts "Parallel results: #{parallel_results}" end private def expensive_operation sleep(2) # Simulate expensive operation "Operation completed" end end

Async Job Processing

TuskLang configuration for async job processing

tsk_content = <<~TSK [async_jobs] # Background job processing background_job: @async.job("ProcessUserDataJob", { user_id: @request.user_id, data: @request.data, priority: "high", retry_count: 3, retry_delay: "5m" }) # Delayed job execution delayed_job: @async.delay("SendEmailJob", { user_id: @request.user_id, email_type: "welcome", delay: "1h" }) # Recurring job recurring_job: @async.recurring("CleanupJob", { schedule: "0 2 *", // Daily at 2 AM data: { cleanup_type: "old_records", retention_days: 30 } }) # Job with custom queue custom_queue_job: @async.job("CustomJob", { queue: "high_priority", data: @request.job_data }) # Job with dependencies dependent_job: @async.job("DependentJob", { depends_on: ["Job1", "Job2"], data: @request.data }) TSK

Ruby implementation for async job processing

class AsyncJobManager include TuskLang::Asyncable def process_background_jobs tusk_config = Rails.application.config.tusk_config # Process background job job_result = tusk_config.execute_async_job('background_job', { user_id: 1, data: { name: 'John', email: 'john@example.com' } }) # Schedule delayed job delayed_result = tusk_config.execute_delayed_job('delayed_job', { user_id: 1, email_type: 'welcome' }) # Schedule recurring job recurring_result = tusk_config.execute_recurring_job('recurring_job') # Execute custom queue job custom_result = tusk_config.execute_custom_queue_job('custom_queue_job', { job_data: { priority: 'high', action: 'process' } }) puts "Background jobs processed successfully" end end

Sidekiq job classes

class ProcessUserDataJob include Sidekiq::Worker def perform(user_id, data) # Process user data user = User.find(user_id) user.update!(data) # Send notification UserMailer.data_processed(user).deliver_now end end

class SendEmailJob include Sidekiq::Worker def perform(user_id, email_type) user = User.find(user_id) case email_type when 'welcome' UserMailer.welcome(user).deliver_now when 'reminder' UserMailer.reminder(user).deliver_now end end end

class CleanupJob include Sidekiq::Worker def perform(cleanup_type, retention_days) case cleanup_type when 'old_records' cleanup_old_records(retention_days) when 'temp_files' cleanup_temp_files end end private def cleanup_old_records(days) cutoff_date = days.days.ago OldRecord.where('created_at < ?', cutoff_date).delete_all end def cleanup_temp_files # Cleanup temporary files logic end end

๐Ÿ”ง Advanced Async Patterns

Async/Await Pattern

TuskLang configuration for async/await pattern

tsk_content = <<~TSK [async_await] # Async function with await async_function: @async.function(async () => { // Fetch user data user_data = await @async.await(@fetch_user_data(@request.user_id)) // Fetch user preferences preferences = await @async.await(@fetch_user_preferences(@request.user_id)) // Process data processed_data = await @async.await(@process_data(user_data, preferences)) // Save results result = await @async.await(@save_results(processed_data)) return result }) # Async function with error handling async_function_with_error_handling: @async.function(async () => { try { // Attempt async operations user_data = await @async.await(@fetch_user_data(@request.user_id)) preferences = await @async.await(@fetch_user_preferences(@request.user_id)) return {user_data, preferences} } catch (error) { @log.error("Async operation failed: " + error.message) throw error } }) # Parallel async operations with await parallel_async: @async.function(async () => { // Execute operations in parallel [user_data, preferences, activity] = await @async.awaitAll([ @fetch_user_data(@request.user_id), @fetch_user_preferences(@request.user_id), @fetch_user_activity(@request.user_id) ]) return {user_data, preferences, activity} }) TSK

Ruby implementation for async/await pattern

class AsyncAwaitManager include TuskLang::Asyncable def execute_async_function tusk_config = Rails.application.config.tusk_config # Execute async function result = tusk_config.execute_async_function('async_function', { user_id: 1 }) puts "Async function result: #{result}" end def execute_async_function_with_error_handling tusk_config = Rails.application.config.tusk_config begin # Execute async function with error handling result = tusk_config.execute_async_function('async_function_with_error_handling', { user_id: 1 }) puts "Async function with error handling result: #{result}" rescue => e puts "Async function failed: #{e.message}" end end def execute_parallel_async tusk_config = Rails.application.config.tusk_config # Execute parallel async operations result = tusk_config.execute_async_function('parallel_async', { user_id: 1 }) puts "Parallel async result: #{result}" end end

Event-Driven Async Operations

TuskLang configuration for event-driven async

tsk_content = <<~TSK [event_driven_async] # Event emitter event_emitter: @async.emit("user.created", { user_id: @request.user_id, user_data: @request.user_data, timestamp: @now() }) # Event listener event_listener: @async.on("user.created", (event) => { user_id = event.user_id user_data = event.user_data // Process user creation event @send_welcome_email(user_id) @create_user_profile(user_id) @notify_admin(user_id) }) # Event with multiple listeners multiple_listeners: @async.on("order.completed", [ (event) => @send_order_confirmation(event.order_id), (event) => @update_inventory(event.order_id), (event) => @notify_customer(event.order_id), (event) => @generate_invoice(event.order_id) ]) # Event with conditions conditional_event: @async.on("payment.processed", (event) => { if (event.amount > 1000) { @trigger_fraud_check(event.payment_id) } if (event.payment_method == "credit_card") { @process_credit_card_fee(event.payment_id) } }) TSK

Ruby implementation for event-driven async

class EventDrivenAsyncManager include TuskLang::Asyncable def emit_user_created_event(user_id, user_data) tusk_config = Rails.application.config.tusk_config # Emit user created event tusk_config.emit_event('event_emitter', { user_id: user_id, user_data: user_data }) puts "User created event emitted for user #{user_id}" end def setup_event_listeners tusk_config = Rails.application.config.tusk_config # Setup event listeners tusk_config.setup_event_listener('event_listener') tusk_config.setup_multiple_listeners('multiple_listeners') tusk_config.setup_conditional_event('conditional_event') puts "Event listeners setup completed" end end

Event handlers

class UserEventHandler def self.handle_user_created(event) user_id = event[:user_id] user_data = event[:user_data] # Send welcome email user = User.find(user_id) UserMailer.welcome(user).deliver_now # Create user profile UserProfile.create!(user: user, preferences: user_data[:preferences]) # Notify admin AdminNotifier.new_user_created(user).notify end end

class OrderEventHandler def self.handle_order_completed(event) order_id = event[:order_id] # Send order confirmation OrderMailer.confirmation(order_id).deliver_now # Update inventory InventoryManager.update_for_order(order_id) # Notify customer CustomerNotifier.order_completed(order_id).notify # Generate invoice InvoiceGenerator.generate_for_order(order_id) end end

๐Ÿญ Rails Integration

Rails Async Configuration

TuskLang configuration for Rails async integration

tsk_content = <<~TSK [rails_async] # Rails background job rails_background_job: @async.rails_job("UserDataJob", { user_id: @request.user_id, action: "process_data", data: @request.data }) # Rails mailer async rails_async_mailer: @async.rails_mailer("UserMailer", "welcome_email", { user_id: @request.user_id, template: "welcome", variables: { user_name: @request.user_name, activation_link: @request.activation_link } }) # Rails cache async rails_async_cache: @async.rails_cache("user_data", @request.user_id, () => { return @fetch_user_data(@request.user_id) }, {expires_in: "1h"}) # Rails database async rails_async_database: @async.rails_database(() => { return @User.where("active = ?", true).count }) TSK

Ruby implementation for Rails async integration

class RailsAsyncManager include TuskLang::Asyncable def execute_rails_background_job(user_id, action, data) tusk_config = Rails.application.config.tusk_config # Execute Rails background job job_result = tusk_config.execute_rails_job('rails_background_job', { user_id: user_id, action: action, data: data }) puts "Rails background job executed: #{job_result}" end def send_async_email(user_id, template, variables) tusk_config = Rails.application.config.tusk_config # Send async email mail_result = tusk_config.execute_rails_mailer('rails_async_mailer', { user_id: user_id, template: template, variables: variables }) puts "Async email sent: #{mail_result}" end def cache_async_data(cache_key, user_id) tusk_config = Rails.application.config.tusk_config # Cache data asynchronously cache_result = tusk_config.execute_rails_cache('rails_async_cache', { cache_key: cache_key, user_id: user_id }) puts "Data cached asynchronously: #{cache_result}" end end

Rails job class

class UserDataJob < ApplicationJob queue_as :default def perform(user_id, action, data) user = User.find(user_id) case action when 'process_data' process_user_data(user, data) when 'update_profile' update_user_profile(user, data) when 'send_notification' send_user_notification(user, data) end end private def process_user_data(user, data) # Process user data logic user.update!(data) UserMailer.data_processed(user).deliver_now end def update_user_profile(user, data) # Update user profile logic user.profile.update!(data) end def send_user_notification(user, data) # Send notification logic UserNotifier.send_notification(user, data).notify end end

ActiveRecord Async Operations

TuskLang configuration for ActiveRecord async

tsk_content = <<~TSK [activerecord_async] # Async model operations async_model_operations: @async.activerecord(() => { // Create user asynchronously user = @User.create!({ name: @request.name, email: @request.email, password: @encrypt(@request.password, "bcrypt") }) // Create associated records @UserProfile.create!({ user_id: user.id, bio: @request.bio, preferences: @request.preferences }) return user }) # Async bulk operations async_bulk_operations: @async.activerecord(() => { // Bulk insert users users_data = @request.users.map((user_data) => ({ name: user_data.name, email: user_data.email, created_at: @now(), updated_at: @now() })) @User.insert_all(users_data) // Bulk update users @User.where("active = ?", false).update_all({ status: "inactive", updated_at: @now() }) return {inserted: users_data.length, updated: @User.where("status = ?", "inactive").count} }) # Async queries with caching async_cached_queries: @async.activerecord(() => { // Cache expensive query active_users = @cache.remember("active_users", "1h", () => { return @User.where("active = ?", true).includes(:profile, :posts).to_a }) return active_users }) TSK

Ruby implementation for ActiveRecord async

class ActiveRecordAsyncManager include TuskLang::Asyncable def execute_async_model_operations(user_data) tusk_config = Rails.application.config.tusk_config # Execute async model operations result = tusk_config.execute_activerecord_async('async_model_operations', { name: user_data[:name], email: user_data[:email], password: user_data[:password], bio: user_data[:bio], preferences: user_data[:preferences] }) puts "Async model operations completed: #{result}" end def execute_async_bulk_operations(users_data) tusk_config = Rails.application.config.tusk_config # Execute async bulk operations result = tusk_config.execute_activerecord_async('async_bulk_operations', { users: users_data }) puts "Async bulk operations completed: #{result}" end def execute_async_cached_queries tusk_config = Rails.application.config.tusk_config # Execute async cached queries result = tusk_config.execute_activerecord_async('async_cached_queries') puts "Async cached queries completed: #{result.length} users" end end

๐Ÿงช Testing and Validation

Async Testing

TuskLang configuration for async testing

tsk_content = <<~TSK [async_testing] # Test async task execution test_async_task: @async.test(() => { return @async.execute(() => { return "test result" }) }, { expected_result: "test result", timeout: "5s" }) # Test async job processing test_async_job: @async.test(() => { return @async.job("TestJob", { test_data: "test" }) }, { expected_job_count: 1, job_class: "TestJob" }) # Test parallel execution test_parallel_execution: @async.test(() => { return @async.parallel([ () => "result1", () => "result2", () => "result3" ]) }, { expected_results: ["result1", "result2", "result3"], timeout: "10s" }) # Test event emission and handling test_event_handling: @async.test(() => { @async.emit("test.event", {data: "test"}) return @async.wait_for_event("test.event", "5s") }, { expected_event_data: {data: "test"}, timeout: "10s" }) TSK

Ruby implementation for async testing

class AsyncTester include TuskLang::Asyncable def test_async_task_execution tusk_config = Rails.application.config.tusk_config # Test async task result = tusk_config.execute_async_test('test_async_task') if result[:success] puts "โœ… Async task test passed" else puts "โŒ Async task test failed: #{result[:error]}" end end def test_async_job_processing tusk_config = Rails.application.config.tusk_config # Test async job result = tusk_config.execute_async_test('test_async_job') if result[:success] puts "โœ… Async job test passed" else puts "โŒ Async job test failed: #{result[:error]}" end end def test_parallel_execution tusk_config = Rails.application.config.tusk_config # Test parallel execution result = tusk_config.execute_async_test('test_parallel_execution') if result[:success] puts "โœ… Parallel execution test passed" else puts "โŒ Parallel execution test failed: #{result[:error]}" end end def test_event_handling tusk_config = Rails.application.config.tusk_config # Test event handling result = tusk_config.execute_async_test('test_event_handling') if result[:success] puts "โœ… Event handling test passed" else puts "โŒ Event handling test failed: #{result[:error]}" end end end

RSpec tests for async operations

RSpec.describe AsyncTaskManager, type: :model do let(:async_manager) { AsyncTaskManager.new } describe '#execute_basic_async_tasks' do it 'executes async tasks successfully' do expect { async_manager.execute_basic_async_tasks }.not_to raise_error end end end

RSpec.describe AsyncJobManager, type: :model do let(:job_manager) { AsyncJobManager.new } describe '#process_background_jobs' do it 'processes background jobs successfully' do expect { job_manager.process_background_jobs }.not_to raise_error end end end

๐Ÿ”ง Rails Integration

Rails Async Configuration

config/initializers/tusk_async.rb

Rails.application.config.after_initialize do TuskLang.configure do |config| # Configure async settings config.async_settings = { enable_background_jobs: true, enable_event_driven: true, enable_parallel_execution: true, async_cache_enabled: true, async_cache_ttl: 1.hour, default_timeout: 30.seconds, max_concurrent_tasks: 10 } # Configure async adapters config.async_adapters = { background_jobs: :sidekiq, event_bus: :redis, cache: :redis, database: :postgresql } # Configure async security config.async_security = { allow_system_commands: false, max_execution_time: 5.minutes, allowed_job_classes: ['UserDataJob', 'SendEmailJob'], restricted_operations: ['system', 'eval', 'exec'] } end end

app/models/concerns/tusk_asyncable.rb

module TuskAsyncable extend ActiveSupport::Concern included do include TuskLang::Asyncable end private def tusk_config Rails.application.config.tusk_config end end

Custom Rake Tasks

lib/tasks/async.rake

namespace :async do desc "Execute async operations using TuskLang" task execute: :environment do manager = AsyncTaskManager.new manager.execute_basic_async_tasks puts "Async operations executed" end desc "Process background jobs" task process_jobs: :environment do job_manager = AsyncJobManager.new job_manager.process_background_jobs puts "Background jobs processed" end desc "Test async functionality" task test: :environment do tester = AsyncTester.new tester.test_async_task_execution tester.test_async_job_processing tester.test_parallel_execution tester.test_event_handling puts "Async testing completed" end desc "Setup event listeners" task setup_events: :environment do event_manager = EventDrivenAsyncManager.new event_manager.setup_event_listeners puts "Event listeners setup completed" end end

๐ŸŽฏ Summary

TuskLang's async operations system in Ruby provides:

- Background job processing with Sidekiq integration - Event-driven async operations with Redis event bus - Parallel execution for concurrent task processing - Async/await patterns for clean asynchronous code - Rails integration with ActiveRecord and ActionMailer - Delayed and recurring jobs for scheduled processing - Testing frameworks for async operation validation - Security features for safe async execution - Custom rake tasks for async operation management

The Ruby implementation maintains TuskLang's rebellious spirit while providing enterprise-grade async capabilities that "don't bow to any king" - not even the constraints of traditional synchronous processing patterns.

Ready to revolutionize your Ruby application's asynchronous processing with TuskLang? ๐Ÿš€