{"id":39660023,"url":"https://github.com/memory-overflow/light-task-scheduler","last_synced_at":"2026-01-18T09:23:07.998Z","repository":{"id":154118408,"uuid":"631854196","full_name":"memory-overflow/light-task-scheduler","owner":"memory-overflow","description":"一个go语言的轻量级的快速实现任务调度的框架，支持并发控制、超时控制和重试。框架预置函数执行器和docker执行器，快捷进行函数调度和 docker 调度。","archived":false,"fork":false,"pushed_at":"2025-08-21T06:27:43.000Z","size":169,"stargazers_count":77,"open_issues_count":3,"forks_count":12,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-08-21T08:37:16.464Z","etag":null,"topics":["docker-scheduled","task-actuator","task-container","task-scheduler"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/memory-overflow.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-24T07:48:05.000Z","updated_at":"2025-08-21T06:27:00.000Z","dependencies_parsed_at":"2025-08-13T08:22:33.802Z","dependency_job_id":"f462ff81-dc45-4232-9420-81b80b21f7c7","html_url":"https://github.com/memory-overflow/light-task-scheduler","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/memory-overflow/light-task-scheduler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memory-overflow%2Flight-task-scheduler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memory-overflow%2Flight-task-scheduler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memory-overflow%2Flight-task-scheduler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memory-overflow%2Flight-task-scheduler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/memory-overflow","download_url":"https://codeload.github.com/memory-overflow/light-task-scheduler/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/memory-overflow%2Flight-task-scheduler/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28534159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-18T00:39:45.795Z","status":"online","status_checked_at":"2026-01-18T02:00:07.578Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker-scheduled","task-actuator","task-container","task-scheduler"],"created_at":"2026-01-18T09:23:07.526Z","updated_at":"2026-01-18T09:23:07.989Z","avatar_url":"https://github.com/memory-overflow.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 轻量级任务调度框架\n\n## 框架设计\n框架对整个任务调度的过程进行4种抽象。\n\n1. [任务容器](https://github.com/memory-overflow/light-task-scheduler/blob/develop/task_container.go)——定义任务如何进行存取和状态流转。\n2. [任务执行器](https://github.com/memory-overflow/light-task-scheduler/blob/develop/task_actuator.go)——定义真正执行任务的逻辑。\n3. [任务数据持久化](https://github.com/memory-overflow/light-task-scheduler/blob/develop/taskdata_persistencer.go)——定义任务数据如果持久化的存储。\n4. [任务调度器](https://github.com/memory-overflow/light-task-scheduler/blob/develop/task_scheduler.go)——定义实现任务调度的流程。\n\n其中，任务容器、任务执行器、数据持久化可能和业务相关、所以用一系列的接口(interface)来抽象，开发者根据自己的业务实现接口。任务调度流程比较固定，由框架实现。\n\n![image](https://github.com/memory-overflow/light-task-scheduler/assets/15645203/55bcf0e1-56cb-44cf-a757-3eb5a0a19b8e)\n\n\n\n任务调度架构如下：\n\n![image](https://github.com/memory-overflow/light-task-scheduler/assets/15645203/0c44a687-66d2-49f6-9aa6-af5955bf5d80)\n\n\n\n主要分成三个主线程\n\n1. 调度线程：负责对等待中的任务进行限频调度。\n\n2. 轮询维护任务状态线程：主要负责感知运行中的任务执行情况，包含超时控制、重试、状态转移，以及执行成功后数据持久化过程。\n\n3. 回调处理线程：任务状态更新支持回调模式。\n\n其中轮询维护任务状态和回调处理状态可以选择其一、或者同时打开。\n\n整体的设计思想是通过抽象实现调度流程的共享，开发者只需要业务关注的部分。\n\n## 任务容器分类\n\n任务可以分成临时任务和持久化任务。\n\n1. 临时任务——比如存储在内存队列里面的任务，执行完成以后，或者服务宕机、重启以后，任务相关的数据消失。\n2. 可持久化任务——任务记录持久化存储，支持查询、修改和删除。\n\n根据是否可持久化，继续对任务容器抽象，分成两类任务容器：\n\n- [MemeoryContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/container/memory_container/memory_container.go)——内存型任务容器，优点：可以快读快写，缺点：不可持久化。MemeoryContainer 实际上是可以和业务无关的，所以框架预置了三种MemeoryContainer——[queueContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/container/memory_container/queue_container.go),[orderedMapContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/container/memory_container/orderedmap_container.go),[redisContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/container/memory_container/redis_container.go)。\n\n  - [queueContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/container/memory_container/queue_container.go)：queueContainer 队列型容器，任务无状态，无优先级，先进先出，任务数据，多进程数据无法共享数据\n\n  - [orderedMapContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/container/memory_container/orderedmap_container.go)：[OrderedMap](https://github.com/memory-overflow/go-orderedmap/blob/main/ordered_map.go) 作为容器，支持任务优先级，多进程数据无法共享数据\n\n  - [redisContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/container/memory_container/redis_container.go)：redis 作为容器，支持任务优先级，并且可以多进程，多副本共享数据\n\n- [PersistContainer](https://github.com/memory-overflow/light-task-scheduler/blob/main/container/persist_container/persist_container.go)——可持久化任务容器，优点：可持久化存储，缺点：依赖db、需要扫描表，对 db 压力比较大。开发者可以参考[exampleSQLContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/videocut_example/video_cut/example_sql_container.go) 实现自己的 SQLContainer，修改数据表的结构。\n\n由于 MemeoryContainer 和 PersistContainer 各有优缺点，如果可以组合两种容器，生成一种新的任务容器[combinationContainer](https://github.com/memory-overflow/light-task-scheduler/blob/develop/upload/container.png)，既能够通过内存实现快写快读，又能够通过DB实现可持久化。\n\n![image](https://github.com/memory-overflow/light-task-scheduler/assets/15645203/3d8a8126-e9bb-4af8-873b-629141b6a8a7)\n\n\n\n## Usage\n```\ngo get -u github.com/memory-overflow/light-task-scheduler\n```\n\n\n### 构建任务调度器\n\n```go\nfunc MakeScheduler(\n\tcontainer TaskContainer,\n\tactuator TaskActuator,\n\tpersistencer TaskdataPersistencer,\n\tconfig Config) (*TaskScheduler, error)\n```\n通过任务容器、执行器、持久化器和任务配置构建一个任务调度器，构建完后自动开始调度。其中持久化器可以为空，\n[任务配置](https://github.com/memory-overflow/light-task-scheduler/blob/develop/task_scheduler.go#L16)\n可以配置是否需要回调，如果需要回调，需要配置一个自定义的\n[回调器](https://github.com/memory-overflow/light-task-scheduler/blob/develop/callback_receiver.go)。\n\n### 函数执行器\n框架预制了[函数执行器](https://github.com/memory-overflow/light-task-scheduler/blob/develop/actuator/function_actuator.go)，借助函数执行器，可以轻松实现函数调度。\n\n使用框架预制的队列容器和函数执行器可以轻松实现一个函数的调度。参考 [a+b example](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/add_example/main.go)。\n\n### docker 执行器\n框架预制了[docker 执行器](https://github.com/memory-overflow/light-task-scheduler/blob/main/actuator/docker_actuator.go)，借助函数执行器，可以轻松实现函数调度。\n\n使用框架预制的队列容器和函数执行器可以轻松实现 docker 的调度。参考 [docker example](https://github.com/memory-overflow/light-task-scheduler/blob/dev/docker_actuator/example/docker_example/main.go)。\n\n### Example: 使用内存容器实现视频裁剪异步任务调度\n本例子演示如何用本框架实现一个持久化的任务调度系统。包含一个简单的 web 管理界面。\n\n首先实现一个异步裁剪的微服务，一共四个接口，需要先安装`ffmpeg`命令\n1. /VideoCut, 输入原视频, 裁剪开始时间，结束时间，返回 taskId。\n2. /Status，输入 taskId, 返回该任务的状态，是否已经完成，或者失败。\n3. /GetOutputVideo, 如果任务已经完成，输入 TaskId，返回已经完成的视频路径结果。\n4. /Stop, 如果任务执行时间过长，可以支持停止。\n\n服务代码参考 [video_cut_service.go](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/videocut_example/video_cut/video_cut_service.go)。\n\n现在我们通过本任务调度框架实现一个对裁剪任务进行调度系统，可以控制任务并发数，和任务超时时间。并且按照队列依次调度。\n\n#### 定义视频裁剪任务\n```go\n// VideoCutTask 视频裁剪任务结构\ntype VideoCutTask struct {\n\tTaskId                   string\n\tCutStartTime, CutEndTime float32\n\tInputVideo               string\n}\n```\n\n#### 实现视频裁剪任务执行器\n\n实现一个视频裁剪任务的执行器，执行器实际上就是调用视频裁剪微服务的 API 接口，执行器的实现参考[video_cut_actuator.go](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/videocut_example/video_cut/video_cut_actuator.go)。\n\n#### 实现视频裁剪任务容器\n首先实现一个 [sql 任务容器](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/videocut_example/video_cut/example_sql_container.go)，从数据库存取任务，然后和队列容器组合一起成为一个组合容器作为[裁剪任务的容器](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/videocut_example/main_web/main.go#L23)。\n\n#### 实现数据持久化\n这里为了连接 db 方便，把任务容器和持久化合并到了一起。\n参考代码[DataPersistence](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/videocut_example/video_cut/example_sql_container.go#L387)。\n\n\n#### 构建调度器\n参考代码[main.go](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/videocut_example/main_web/main.go#L26)。\n\n#### 实现简单管理接口的 web 页面\n参考代码 [web.go](https://github.com/memory-overflow/light-task-scheduler/blob/develop/example/videocut_example/video_cut/web.go)。\n\n\n#### 启动服务\n执行 `go run example/videocut_example/main_web/main.go` 启动服务，然后在浏览器输入`http://127.0.0.1:8000/html`即可进入管理界面。\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmemory-overflow%2Flight-task-scheduler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmemory-overflow%2Flight-task-scheduler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmemory-overflow%2Flight-task-scheduler/lists"}