asynqmon Documentation

repository·master·Indexed 21 days ago

https://github.com/hibiken/asynqmon

A web-based monitoring and administration tool for Asynq task queues. It can be deployed as a standalone binary, a Docker container, or integrated into Go web applications as a library. Features include support for various Redis topologies (Single, Sentinel, Cluster), Prometheus metrics integration, and customizable payload and result formatting.

Tokens
7.1K
Snippets
25
Records
36
Agent score
75%

What's inside asynqmon

  1. Run the React UI local development server

    master

    You can run the React UI in a standalone development mode (independent of the Asynqmon server) to test UI changes. The server will automatically reload the browser when you edit source code.

    1. Navigate to the ui/ directory.
    2. Run yarn start.
    3. The app will be available at http://localhost:3000/.
    cd ui
    yarn start
  2. Connect Asynqmon to different Redis topologies

    master

    Depending on your Redis setup, use the appropriate connection method:

    Single Redis Server Use --redis-url or a combination of --redis-addr, --redis-db, and --redis-password.

    ./asynqmon --redis-url=redis://:mypassword@localhost:6380/2
    ./asynqmon --redis-addr=localhost:6380 --redis-db=2 --redis-password=mypassword

    Redis Sentinels Use --redis-url with the redis-sentinel:// scheme.

    ./asynqmon --redis-url=redis-sentinel://:mypassword@localhost:5000,localhost:5001,localhost:5002?master=mymaster

    Redis Cluster Use --redis-cluster-nodes with a comma-separated list of host:port addresses.

    ./asynqmon --redis-cluster-nodes=localhost:7000,localhost:7001,localhost:7002,localhost:7003,localhost:7004,localhost:7006
    # Single Redis
    ./asynqmon --redis-url=redis://:mypassword@localhost:6380/2
    
    # Redis Sentinels
    ./asynqmon --redis-url=redis-sentinel://:mypassword@localhost:5000,localhost:5001,localhost:5002?master=mymaster
    
    # Redis Cluster
    ./asynqmon --redis-cluster-nodes=localhost:7000,localhost:7001,localhost:7002,localhost:7003,localhost:7004,localhost:7006
  3. Integrate Asynqmon with Prometheus

    master

    Asynqmon supports Prometheus for displaying time-series data in the Web UI through a two-step process:

    1. Expose Metrics: Enable the metrics exporter in Asynqmon using the --enable-metrics-exporter flag. Metrics will be available at the /metrics endpoint for Prometheus to scrape.
    2. Query Metrics: Provide the Prometheus server address to Asynqmon using the --prometheus-addr flag. This allows the Web UI to query and display time-series data.

    Example:

    ./asynqmon --enable-metrics-exporter --prometheus-addr=http://localhost:9090
  4. Build the React UI for production

    master

    To generate a production-optimized version of the React app in a build subdirectory, run the build command from within the ui/ directory.

    Note: If you are building the full Asynqmon binary, you typically do not need to run this manually, as the main Makefile handles this step.

    cd ui
    yarn build
  5. Set up the Asynqmon React UI development environment

    master

    To develop or modify the Asynqmon React UI, ensure you have the following tools installed:

    • Node.js JavaScript runtime
    • Yarn package manager
    • Editor: Visual Studio Code is recommended.

    Important Editor Configuration: When using Visual Studio Code, you must open the ui/ directory directly in the editor rather than the repository root. This ensures that the editor correctly picks up the project's specific ESLint and TypeScript configurations.

  6. Install Asynqmon via binary, Docker, or source

    master

    You can install Asynqmon using several methods:

    1. Release Binaries: Download pre-built binaries from the GitHub releases page.
    2. Docker Image: Pull the official image from Docker Hub.
    3. Build from Source: Requires Go 1.16+, Node.js, and Yarn. Run make build to create the asynqmon binary.
    4. Build Docker Image locally: Run make docker to build a local image.
    # Pull the latest image
    docker pull hibiken/asynqmon
    
    # Build from source
    make build
    
    # Build docker image locally
    make docker
  7. Run Asynqmon binary or Docker container

    master

    To run Asynqmon with default settings (listening on port 8080 and connecting to Redis at 127.0.0.1:6379), use the following commands:

    Using the binary:

    ./asynqmon

    Using Docker:

    docker run --rm \
        --name asynqmon \
        -p 8080:8080 \
        hibiken/asynqmon
  8. Build Asynqmon with integrated web assets

    master

    To create a single Asynqmon binary that has the production React UI assets compiled directly into it, use the main Makefile from the repository root. This command automates the installation of npm dependencies, the production build of the React app, and the final compilation of all web assets into the binary.

    # From the repository root
    make build
  9. Use Read-Only mode in asynqmon

    master

    To prevent accidental modifications to your task queues (like deleting tasks or pausing queues) via the Web UI, set ReadOnly: true in your asynqmon.Options.

    In this mode, the asynqmon middleware will intercept all non-GET requests and return an http.StatusMethodNotAllowed error with the message: API Server is running in read-only mode: <METHOD> request is not allowed.

  10. Customize payload and result formatting

    master

    Asynqmon uses PayloadFormatter and ResultFormatter interfaces to convert raw task bytes into human-readable strings for the Web UI.

    By default, DefaultPayloadFormatter and DefaultResultFormatter attempt to print the bytes as a string if they are printable UTF-8; otherwise, they return "non-printable bytes".

    You can implement your own formatting logic by satisfying the interface or by using the PayloadFormatterFunc and ResultFormatterFunc types, which allow you to use a simple function as a formatter.

    // Example: Implementing a custom JSON formatter
    type JSONPayloadFormatter struct{}
    
    func (f JSONPayloadFormatter) FormatPayload(taskType string, payload []byte) string {
        // logic to pretty-print JSON
        return string(payload)
    }
    
    // Or using the function type shortcut
    var myFormatter asynqmon.PayloadFormatter = asynqmon.PayloadFormatterFunc(func(taskType string, payload []byte) string {
        return "Custom: " + string(payload)
    })
  11. Embed asynqmon as an HTTP handler

    master

    You can embed the asynqmon Web UI into your existing Go web applications by using the asynqmon.New function. This returns an *asynqmon.HTTPHandler which implements the http.Handler interface and can be mounted to any router (e.g., net/http, gorilla/mux, gin).

    To use it, provide an asynqmon.Options struct containing a valid RedisConnOpt from the asynq package. You should also call .Close() on the handler when your application shuts down to ensure Redis connections and inspectors are properly closed.

    import (
    	"net/http"
    	"github.com/hibiken/asynq"
    	"github.com/hibiken/asynqmon"
    )
    
    func main() {
    	// 1. Configure options
    	opts := asynqmon.Options{
    		RedisConnOpt: asynq.RedisConnOpt{ /* your redis config */ },
    		RootPath:     "/asynqmon", // Optional: mount at a specific path
    	}
    
    	// 2. Create the handler
    	handler := asynqmon.New(opts)
    	defer handler.Close()
    
    	// 3. Register with your router
    	http.Handle("/asynqmon/", handler)
    	http.ListenAndServe(":8080", nil)
    }