Skip to content

sunjun/task-server-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Task Server (Go Version)

This is a high-performance, concurrent video processing task server implemented in Go.

The server dynamically manages video processing tasks via a RESTful API, processes them concurrently using a goroutine-based worker pool, and performs video decoding by executing an external ffmpeg command.

✨ Key Features

  • Dynamic Task Management (RESTful API):

    • POST /tasks: Create a new video processing task.
    • GET /tasks: Get a JSON list of all tasks and their current status.
    • DELETE /tasks/{source_id}: Delete a specified task.
  • Task Persistence & Logging:

    • Uses SQLite3 for persistent task storage.
    • Logs key lifecycle events for each task (Created, Added, Running, Finished, Deleted).
  • Periodic Task Scheduling:

    • Features a built-in ticker to periodically query the database for runnable tasks.
    • The scheduling logic respects the execution interval of each task to prevent frequent reprocessing.
  • High-Performance Concurrency:

    • Implements an elegant and efficient worker pool using Go's goroutines and channels to process video tasks concurrently.
    • Avoids complex Cgo dependencies by executing the system's ffmpeg command-line tool for decoding, ensuring easy and stable deployment.
  • Robustness & Stability:

    • Graceful Shutdown: Catches SIGINT and SIGTERM signals to safely shut down the HTTP server, stop the worker pool, and close the database connection, preventing resource leaks.
    • Clean Dependency Management: Uses Go Modules (go.mod) for managing project dependencies.

🛠️ Tech Stack

  • Language: Go
  • Web Framework/Router: gorilla/mux - A powerful and flexible HTTP router.
  • Database: SQLite3 (via the github.com/mattn/go-sqlite3 driver).
  • Core Technologies:
    • Goroutines & Channels (for the concurrent worker pool).
    • Standard library net/http (for building the HTTP server).
    • Standard library os/exec (for invoking the external ffmpeg process).
    • Standard library database/sql (database abstraction interface).

🚀 Getting Started

1. Prerequisites

  • Go (version 1.16+).
  • FFmpeg - Must be installed on your system and the ffmpeg command must be available in your PATH.
  • gcc or clang (as the go-sqlite3 driver requires Cgo for compilation).

2. Download and Run

  1. Clone or download the project code to your local machine.

    git clone https://github.com/sunjun/task-server-go.git
    cd task-server-go
  2. Download the project dependencies.

    go mod init task-server-go
    go mod tidy
  3. Run the service.

    go run .

The server will start and listen for API requests on port 8888 by default.

3. API Usage Examples

  • Create a new task:

    curl -X POST -d "source_id=cam01&url=rtsp://..." http://localhost:8888/tasks
  • Get the list of all tasks:

    curl http://localhost:8888/tasks | jq

    (Using the jq tool to format the JSON output is recommended)

  • Delete a task:

    curl -X DELETE http://localhost:8888/tasks/cam01

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages