`
LilyEnjoyingLife
  • 浏览: 70969 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

javascript基于发布订阅的任务调度框架

阅读更多
gitlab:https://github.com/DoTalkLily/TaskManager

task manager

This is a task manager framework in js. It provides you with an executor for a bunch of tasks and manage the lifecycle of your task, you could subscribe the lifecycle event. Task could be an calculation which takes a long time,or an mission which should be executed periodically.

how to use

You could include the task-manager.js under directory 'dest/' in your html.Like:

 <script type="text/javascript" src="/your_directory/task-manager.js"></script>

If you modified the source code and rebuild it, you need webpack installed globally:

$ npm install --save webpack

Then execute under root directory:

$ webpack

Task

  • create a task sh var taskManager = new TaskManager(); taskManager.createTask({ id : taskId, run : function(){ //process code goes here } }); 
  • subscribe a topic

      var token = PubSub.subscribe(taskId,handler);

    'handler' is the function to process result when the task manager publish some result to the topic. e.g.

        function handler(topic, res) {
        }

    The 'topic' is the topic you subscribed, 'res' is the data received.

    An unique token is returned to unsubscribe the topic.

      var token = PubSub.unsubscribe(token);

    Then a task is created and add to the task manager.To start is,you could call 'start'. e.g.

      taskManager.runTask(taskId);

    Then you could leave it behind and process other code.

  • Configurations

    • interval(optional,unit: millisecond)

      It defines how often the 'run' method of the task is executed. Default is 10000.

    • init (optional,function)

      Some initialization before running the task,it could return a promise , 'run' method is executed after the promise resolved.

    • run (required,function)

      The main job of this task, which could send http request to server(ajax),or an time consuming calculation, etc. After the user defined processing is done, task manager will publish the result to the specific taskId. Your 'handler'(metioned above) will receive the result(if exists) then decide to kill the task or repeat the task after 'interval'(metioned above) time.

      It could return a promise, then the publish action will take after the promise is resolved(It's useful when you are sending some ajax request).

    • kill (optional,function)

If is provided, it could be some work before killing an task.

  • Api of a single task

    • createTask

      Create a single task.The configuration of a task is passed.Including:

      • interval(optional):interval of executing a task
      • id (required):unique id of the task
      • run(required): task mission
      • kill(optional): kill mission

      return : The task just created.

    • createTaskList

      Create a list of tasks. The configuration of the task list is passed. Including:

      • interval(optional):interval of executing a task
      • ids (required):list of id of the task
      • init(optional): init work before executing the task
      • run(required): task mission
      • kill(optional): kill mission return : The list of tasks just created.
    • runTask

      Start to run the task.The task id is passed.

        taskManager.runTask(taskId);
    • runTaskList

      Start to run the task list at the same time .Array of task ids is passed.

      taskManager.runTask([taskid1,taskid2]);
    • pauseTask

      Pause a task. Task id is required.

       taskManager.pauseTask(taskId);
    • pauseTaskList

      Pause a list of tasks at the same time.Array of task ids is required.

        taskManager.pauseTaskList([taskId1,taskId2]);
    • continueTask

      Wake up the task which is paused. Task id is required.

       taskManager.continueTask(taskId);
    • continueTaskList

      Wake up the task which is paused.Array of task ids is required.

        taskManager.continueTaskList([taskId1,taskId2]);
    • killTask

      Kill the task. Task id is required.

       taskManager.killTask(taskId);
    • killTaskList

      Kill a list of tasks at the same time. Array of task ids is required.

        taskManager.killTaskList([taskId1,taskId2]);

Batch Task

There is a condition when serveral tasks share one execution method.alt text

  • Task manager collects the 'batchParam' of each task in the task queue. The 'batchParam' is defined when creating a batch task,which is the data each specific task may need when batch process taking place.
  • Then execution shared by the batch tasks is called.
  • Results are dispatched(published) to each batch task according to their task id.
  • Each task may decide if it would attend next execution period or remove itself from the task queue according to the result they got(subscribe);
  • After the 'interval' time, the task manager will repeat the same process again until there is no task in the task queue.
  • Api of a batch task

    • createBatchTask

      • option
        • id (required) : id of the task,should be unique!
        • init (function,optional): init task
        • afterInit(function,optional): callback of init
        • callback(function,optional): callback of each response result(you may also subscribe the result)
        taskManager.createBatchTask({
             id: taskId,
             batchParams:{
                //params for this task during execution
             }
        });
    • killBatchTask

      • option
        • id (required) : id of the task
        • kill(function,optional): function before the task is killed
         taskManager.killBatchTask({
              id: taskId
         });
      
         //or you may just pass the task id
         taskManager.killBatchTask(taskId);
分享到:
评论

相关推荐

    基于.NET平台常用的框架整理

    自动任务调度框架***和Topshelf提供了快速简便的作业调度和Windows服务创建功能,便于开发者管理后台任务和系统服务。 依赖注入IOC容器框架也是.NET平台中不可或缺的组件。Unity、MEF、***、Autofac和PostSharp等都...

    EventDispatcher:用于 JavaScript 中基于事件的通信的 EventDispatcher 微框架

    **事件调度器(EventDispatcher)是JavaScript中一种常见用于实现基于事件通信的微框架。它遵循发布/订阅(Publish-Subscribe)模式,允许组件之间松耦合地传递信息,从而提高代码的可维护性和复用性。** 在...

    Laravel开发-laravel-faye

    - 考虑使用 Laravel 的任务调度器(`App\Console\Kernel`)定期清理过期的订阅。 总的来说,Laravel-Faye 是 Laravel 开发者实现实时通信功能的一个强大工具,它使得 Faye 的功能得以无缝融入 Laravel 框架,提高了...

    NetDiscovery 是一款基于 Vert.x、RxJava 2 等框架实现的通用爬虫框架_中间件。.zip

    NetDiscovery 是一个基于 Vert.x 和 RxJava 2 构建的通用爬虫框架,它为开发者提供了一套高效、灵活的网络数据抓取解决方案。Vert.x 是一个轻量级的事件驱动框架,常用于构建高性能的分布式应用,而 RxJava 2 是一个...

    毕业设计+Python基于Scrapy+Redis分布式爬虫设计+源码案例+Python + Scrapy + redis

    这个项目不仅有助于学习Python爬虫的基本技巧,还能深入理解分布式系统的设计原理,特别是如何通过Redis实现任务调度和数据共享,对提升你的编程技能和解决实际问题的能力大有裨益。通过实践,你可以掌握一套完整的...

    新闻管理系统 j2ee 自动发布新闻

    4. **调度器**:如Quartz或Spring Scheduler,用于自动化任务,如定时发布新闻和发送邮件。 5. **邮件服务**:JavaMail API处理邮件发送,配置SMTP服务器连接。 6. **安全**:使用Spring Security或JAAS(Java ...

    ActionView一个类Jira的问题需求跟踪工具前端基于reactjsredux

    Laravel提供了许多内置功能,如路由、认证、会话、缓存和任务调度等,大大简化了Web开发过程。它的Artisan命令行工具帮助开发者快速生成基础代码,Eloquent ORM使数据库操作变得更加简单,而Blade模板引擎则允许...

    rosbridge-0.0.2.zip

    ROSbridge 项目的核心功能包括发布和订阅 ROS 主题、服务调用、参数服务器操作等。它提供了一种轻量级的接口,使得非 ROS 环境中的应用也能便捷地与 ROS 系统进行交互。这对于开发基于 Web 的机器人控制系统或者将...

    Atom-feedspora,feedspora将rss/atom提要发布到您的社交网络帐户。.zip

    3. 调度或定时任务:为了让提要更新自动化,Atom-feedspora可能会使用定时任务库(如Python的APScheduler或Node.js的cron),按照设定的时间间隔检查新的提要条目。 4. Web框架或命令行工具:根据项目实现方式,可能...

    JSP开发项目-办公自动化管理系统

    这涉及到了任务调度和提醒机制,可能需要用到队列(Queue)数据结构来管理待办事项。 4. **日程安排**:允许用户创建、修改和删除日程,同时可以设置提醒功能。这需要处理时间戳和事件冲突检测,以确保日程的准确性...

    30种java技术框架

    它支持两种消息模式:点对点(Point-to-Point)和发布/订阅(Publish/Subscribe)。JMS的主要组件包括生产者、消费者、连接工厂和消息代理等。 #### 七、JMX技术架构 JMX(Java Management Extensions)是一套用于...

    Python基于Scrapy-Redis分布式爬虫设计源码案例设计.zip

    Python基于Scrapy-Redis分布式爬虫设计是一种高效的数据抓取技术,它结合了Python的Scrapy框架和Redis数据库,以实现大规模网站数据的并行抓取。Scrapy是一个功能强大的爬虫框架,而Redis则作为一个消息队列,帮助...

    Python-WSCelery使用websockets的实时celery监控

    在现代Web应用程序中,异步任务处理和后台作业调度是必不可少的功能。Celery是一个流行的Python分布式任务队列,它允许开发者在后台执行耗时的任务,而不会阻塞主线程。然而,为了确保系统的稳定性和性能,监控...

    有赞(24问).pdf

    发布者向调度中心发布消息,订阅者订阅消息,当消息到达调度中心时,调度中心负责通知所有订阅了该消息的订阅者。发布-订阅模式更加解耦,通过调度中心可以实现发布者和订阅者之间的解耦。 react异步渲染的概念,...

    Laravel开发-comunik

    - 任务调度,如定时发送营销邮件。 通过利用 Comunik,开发者可以在 Laravel 应用程序中实现高效的通信机制,提高系统的可扩展性和可维护性。在实践中,不断探索和优化,可以使 Comunik 更好地服务于项目需求,为...

    Laravel开发-jobfeed

    8. **任务调度**:Laravel 的任务调度功能允许开发者安排定时任务,如发送邮件、执行统计分析等,可以轻松地在 cron 任务中运行。 9. **队列服务**:Laravel 的队列服务可以将耗时的操作异步处理,提高应用的响应...

    Laravel开发-newsletters

    Laravel 的其他特性,如中间件(Middleware)进行权限控制,验证(Validation)确保用户输入的数据有效,任务调度(Task Scheduling)定期发送新闻通讯,以及事件监听器(Event Listeners)处理订阅或退订时触发的...

    taskmill-core-relay-registry

    总的来说,Taskmill-core-relay-registry是Taskmill框架的核心部分,它利用JavaScript的特性实现了任务的调度和传递,是理解整个系统工作原理的关键。通过研究其源代码和运行测试,我们可以深入了解这个组件的工作...

    Laravel开发-laravel-skeleton

    Laravel遵循MVC(Model-View-Controller)设计模式,强调代码的组织性和可维护性,同时提供了丰富的功能,如路由、中间件、数据库迁移、任务调度、身份验证等。 ### MVC架构 MVC是一种软件设计模式,将业务逻辑、...

    SpringMVC+JDBC+Redis+Mysql+EasyUI simba精简版本

    Spring Data Redis库提供了对Redis的集成,包括操作键值、哈希、集合、有序集合等功能,并支持事务、发布/订阅模式等高级特性。通过配置RedisTemplate或StringRedisTemplate,可以轻松地在SpringMVC应用中使用Redis...

Global site tag (gtag.js) - Google Analytics