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.
-
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
andSIGTERM
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.
- Graceful Shutdown: Catches
- 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 externalffmpeg
process). - Standard library
database/sql
(database abstraction interface).
- Go (version 1.16+).
- FFmpeg - Must be installed on your system and the
ffmpeg
command must be available in yourPATH
. gcc
orclang
(as thego-sqlite3
driver requires Cgo for compilation).
-
Clone or download the project code to your local machine.
git clone https://github.com/sunjun/task-server-go.git cd task-server-go
-
Download the project dependencies.
go mod init task-server-go go mod tidy
-
Run the service.
go run .
The server will start and listen for API requests on port 8888
by default.
-
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