GithubHelp home page GithubHelp logo

task-server's Introduction

Task-server

This is a test task.

Task description

Design and implement a task-server. It must provide the following REST API.

  • Method to execute one of the three task types on n-workers:
  • Task which always fail execution.
  • Task which waits for 3 seconds and return a value.
  • task which immediately returns a PI value.

Requirements

  • Java8
  • Maven3
  • Multi-module project (to have an ability to add a new task handler w/o touching the code of executor).
  • Back-pressure (skipped due a complexity of the implementation)

Implementation details

I chose a simple implementation based on the Spring Framework async request support. When a new HTTP-request is submitted, the controller method returns a callable which is executed in a separate thread pool, thus, a jetty request thread is freed to serve others API requests. When the execution of the method call() is completed it restores the request context and flushes a result to a client. So, from the server point of view the execution is asynchronous, but from a client side is synchronous.

But, can we make better? Yes, of course!

In a real world, it's a completely bad idea to have a task executor in the same JVM. A better approach, to split it to separate processes and turn it into a fully asynchronous pattern. The frontend server (API) receives a task and submits it to a message broker (rabbitmq, kafka). The backend server (task executor) consumes tasks and executes it. When a task is completed it can notify a client via another channel (e.g. websocket, push). Another option (if a push approach is impossible) to expose API on frontend which can say a client that a job is completed. It's option often called as a pull approach.

How to add a new handler

  • Add a new maven module (see examples of pi-handler, sleep-handler)
  • Add the new module as dependency to the server module

How does it work?

TaskHandlerConfiguration class autowire all implementations of TaskHandler which are available in the classpath. When TaskExecutionService is initialized it registers the implementations inside yourself.

Basic platform, frameworks.

  • Spring boot (easy REST API, configuration, DI)
  • Jetty server
  • Guava
  • Testng/mockito/springockito

API (json format)

{"error":{"code":"EXECUTION_FAILED","message":"Something went wrong"}}

Depending on a task id a result can vary.

task-server's People

Contributors

sherman avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.