Skip to main content

BaseAutomationService

The BaseAutomationService class is the foundation for all Automation Scripts. It provides a standardized interface, common functionality, and essential utilities that all automation workflows can leverage.

Class Structure​

Every Automation Script inherits from BaseAutomationService and must implement the execute method:

class MyAutomationScript < BaseAutomationService
def execute
# Your automation logic here
end
end

Core Components​

Required Methods​

execute​

The main entry point for your automation script. This method contains the primary workflow logic.

def execute
Rails.logger.info("Starting automation process")

# Initialize any required clients or services
initialize_clients

# Your automation logic
process_data

# Handle completion
send_results
rescue StandardError => e
Rails.logger.error("Automation error: #{e.message}")
raise e
end

Available Instance Variables​

When your script executes, several instance variables are automatically available:

  • @automation_workflow_execution: The current execution context
  • @variables: Configuration variables and parameters
  • @credentials: Authentication credentials for external services
  • @errors: ActiveModel::Errors object for error tracking

Common Patterns​

Client Initialization​

private

def initialize_clients
@gmail_client = CommerceAutomationNodes::GmailNode.new(credentials: @credentials)
@openai_client = CommerceAutomationNodes::OpenaiNode.new(credentials: @credentials)
@slack_client = CommerceAutomationNodes::SlackNode.new(credentials: @credentials)

Rails.logger.info("Clients initialized successfully")
end

Time Range Setup​

def setup_time_range
time_zone = @variables.dig("time_zone") || "America/New_York"

@start_time = if @variables.dig("start_time")
Time.parse(@variables.dig("start_time")).in_time_zone(time_zone)&.to_i
elsif @automation_workflow_execution.automation_workflow.last_successful_execution_at
@automation_workflow_execution.automation_workflow.last_successful_execution_at.in_time_zone(time_zone)&.to_i
else
30.minutes.ago.in_time_zone(time_zone)&.to_i
end

@end_time = if @variables.dig("end_time")
Time.parse(@variables.dig("end_time")).in_time_zone(time_zone)&.to_i
else
Time.now.in_time_zone(time_zone)&.to_i
end

Rails.logger.info("Time range: #{@start_time} to #{@end_time}")
end

Error Handling​

def execute
Rails.logger.info("Starting automation process")

@errors = ActiveModel::Errors.new(self)

# Your automation logic here

rescue StandardError => e
Rails.logger.error("Automation error: #{e.message} [Account: #{@automation_workflow_execution.account.name}]")
raise e
end

Response Handling​

def send_response(response_data)
send_response(
response_json: {
status: "success",
processed_items: response_data.length,
data: response_data
}
)
end

Best Practices​

Logging​

Always include comprehensive logging throughout your script:

Rails.logger.info("Starting process with #{items.length} items")
Rails.logger.debug("Processing item: #{item.inspect}")
Rails.logger.error("Failed to process item #{item.id}: #{e.message}")

Error Recovery​

Implement proper error handling that allows partial success:

items.each do |item|
begin
process_item(item)
rescue => e
Rails.logger.error("Error processing item #{item.id}: #{e.message}")
next # Continue with other items
end
end

Configuration Access​

Use safe navigation when accessing configuration variables:

model = @variables.dig("openai_model") || "gpt-4"
channel = @variables.dig("slack_channel")
user_id = @variables.dig("gmail_user_id")

Service Integration​

Initialize and use service clients consistently:

def initialize_clients
@gmail_client = CommerceAutomationNodes::GmailNode.new(credentials: @credentials)
@openai_client = CommerceAutomationNodes::OpenaiNode.new(credentials: @credentials)
end

def send_message(content)
response = @openai_client.send_message(
message: content,
model: @variables.dig("openai_model") || "gpt-4"
)

response
end

Available Service Nodes​

Common service nodes you can use in your automation scripts:

  • CommerceAutomationNodes::GmailNode - Gmail API integration
  • CommerceAutomationNodes::SlackNode - Slack messaging
  • CommerceAutomationNodes::OpenaiNode - OpenAI API integration
  • CommerceAutomationNodes::ShopifyNode - Shopify API integration
  • CommerceAutomationNodes::WebhookNode - Webhook handling

Next Steps​

  • Explore Automation Script Examples to see complete implementations
  • Learn about specific service integrations
  • Understand workflow orchestration patterns