Overview

The @outspeed.App() decorator is used to define a class as a Outspeed application. This decorator enhances the class, enabling it to interact seamlessly with various Outspeed services and manage the application lifecycle.

Creating an App

To create an app, define a class and decorate it with @outspeed.App(). Here’s a minimal example:

import outspeed as sp

@sp.App()
class MyApplication:

    def setup(self):
        # Initialization code here
        print("App is setting up!")

    def run(self):
        # Main logic here
        print("App is running!")

    def teardown(self):
        # Cleanup code here
        print("App is tearing down!")

A Outspeed App should have setup, run, and teardown methods, although only run is strictly required.

  • The setup method is called when the app starts, allowing for initialization.
  • The run method contains the main logic of the app and is required.
  • The teardown method is called when the app is shutting down, enabling proper resource cleanup.

App Lifecycle

The App class manages the application lifecycle:

  1. It calls the setup method asynchronously.
  2. It runs the run method (as many times as required).
  3. After the main execution, it calls the teardown method.

Outspeed Functions

The app can include Outspeed functions, which are methods decorated with @outspeed.function(). The current implementation supports only one Outspeed function per app class.

If more than one Outspeed function is defined in the app class, a RuntimeError will be raised.

If a Outspeed function is defined, it will be executed concurrently with the OutspeedServer, replacing the need for a separate run method.

import outspeed
@outspeed.App()
class MyApplication:
@outspeed.function()
async def my_outspeed_function(self):
# Outspeed function logic here
pass
async def run(self):
# Main app logic here
pass

Best Practices

  • Implement setup and teardown methods in your app class for proper initialization and cleanup.
  • Use either a run method or a single Outspeed function as the main execution point of your app.
  • Use type hints for all method parameters and return types to enhance clarity.
  • Limit your app to one Outspeed function to comply with current implementation constraints.
  • Implement error handling within your methods to manage exceptions gracefully.
  • Test your app thoroughly locally before deploying to ensure all components interact correctly.

By following these guidelines, you can create robust and efficient applications using the Outspeed platform.

Error Handling

The App includes basic error handling:

  • If an exception occurs during the app lifecycle (setup, run/Outspeed function execution, or teardown), it will be logged as an error.
  • In case of an error, the OutspeedServer will be shut down gracefully.

It’s recommended to implement your own error handling within your app methods for more specific error management.