This lib is the framework for dependency tasks, specially for the asynchronous tasks.

Overview

DependencyTask

This lib is the framework for dependency tasks, specially for the asynchronous tasks.

Backgroud

Image that there is a progress with some steps, and the order of the dependencies for those steps maybe not fixed. Most often ,like in the android app, we need to do the initilaize progress, such as require permissions , check the account or do some asynchronous initialized progress before letting the user use the app, and there may be some dependency between the steps, for some requirements , the order of the dependencies would be changed, this lib is just for that scenario.

Installation

For gradle

Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Add the dependency:

dependencies {
  implementation 'com.github.Arrowyi:DependencyTask:TheVersion'
}

For maven

Add the JitPack repository to your build file

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io
  </repository>
</repositories>

Add the dependency

<dependency>
  <groupId>com.github.ArrowyigroupId>
  <artifactId>DependencyTaskartifactId>
  <version>TheVersionversion>
</dependency>

How to use

Step 1 : inherite from the Task class

  • First, you need to divide your progress into serval tasks
  • each task need to inherite from the Task class
  • call addDependency function with the task instance to build the releationship of the tasks
  • implement the action suspend function for your job
  • if the task done its job, call the actionResult function with true(job done successfully) or false(job done failed) to notify the framework the result of the task

Step 2 : start with the TaskProcessor

  • create the TaskProcessor class initance with a InitTask (which is your first order task)
  • call the start funcation to get a 'flow'
  • call collet function of the flow to get the ProgressStatus class( which is represent for the progress status ) sequentially

Example

println("check result is : ${it.result}") is Progress -> println("task : ${it.task.getDescription()} is done") is Complete -> println("all the task is done successfully,and the flow is ended") is Failed -> println("task : ${it.failedTask.getDescription()} is failed , and the flow is ended ") } } } ">
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking

class SuspendTask : Task() {
  override suspend fun doAction() {
      //do suspend job
      delay(100)
      actionResult(true)
  }
}

class BlockTask : Task() {
  override suspend fun doAction() {
      //some block job
      Thread.sleep(100)
      actionResult(true)
  }
}

class SomeTask(private val name: String) : Task() {
  override suspend fun doAction() {
      actionResult(true)
  }

  override fun getTaskDescription(): String {
      return name
  }
}

fun main() = runBlocking {
  val task1 = SuspendTask()
  val task2 = BlockTask()
  val task3 = SomeTask("task 3")
  val task4 = SomeTask("task 4")

  task2.addDependency(task1)
  task3.addDependency(task1)
  task3.addDependency(task2)
  task4.addDependency(task3)

  TaskProcessor(task1).start().collect() {
      when (it) {
          is Check -> println("check result is : ${it.result}")
          is Progress -> println("task : ${it.task.getDescription()} is done")
          is Complete -> println("all the task is done successfully,and the flow is ended")
          is Failed -> println("task : ${it.failedTask.getDescription()} is failed , and the flow is ended ")
      }
  }

}

The progress status is :

//check if there is circular dependency , true is for check ok (no circular dependency), false will end the processor, and the tasks is all
//the tasks this processor will do.
class Check(val result: Boolean, val tasks: List<Task>?) : ProgressStatus()
//called if the task is done
class Progress(val task: Task) : ProgressStatus()
//called if all the tasks is done successfully, and the processor will be ended
class Complete : ProgressStatus()
//called if any of the task failed, and the processor will be ended immediately
class Failed(val failedTask: Task) : ProgressStatus()
//The Processor is already running, and this flow will be ended
class AlreadyRunning() : ProgressStatus()

Features

  • The processor will check the circular dependency
  • The task's action will run concurrently if their dependency tasks are done (you could specify the dispatcher the action running on by the parameter of TaskProcessor)
  • If you use the same processor instance to start many times, the successful tasks will not do action again, the processor will call the action function from the last failed one.
  • If you use the same processor instance to start more than one Flow simultaneously, only one Flow will run and the others will receive the AlreadyRunning status and be ended.
  • For the detail of the features you could ref to the unit test

Attention

  • there should be only one init task node as the root node of the dependence tree
  • you must pass the root node to initlaze the TaskProcessor
  • if there is a circular dependency, the Check status will pass a result value false.
  • you must call actionResult function of the task after your job is done, whatever it is failed or has an exception,or the Flow will not end.

Progress example

text

  • As the above dependency task tree, when you begin to call the function collect of the Flow(which is got from start function), it will pass the check status with true to indicate that the circular dependency check is OK.
  • And then ,it will pass the Progress status to indicate which task is done. the TaskProcessor will call task's action function concurrently if their dependency tasks are done.
  • As the above tree, the task7's action function will be called after task2 and task5 are done, and task7 and task8 maybe called concurrently.
  • For example, after task2 is done, the onDependencyDone function of task7 will be called, you could override this function to get some info from the dependency task, you must call the super function if you override this function
  • And the Complete status will be received if all the tasks are done successfully, otherwise 'Failed` status will be received immediately if there is a failed task. any of those two status will end the Flow.
  • If the Check status is with a result false, the Flow will also be ended.

Release Version

The last release version please ref to the Release page

You might also like...
My own approach to what I think an Android MVVM project with Clean Architecture should look like with Dagger-Hilt as Dependency Injector engine
My own approach to what I think an Android MVVM project with Clean Architecture should look like with Dagger-Hilt as Dependency Injector engine

MVVM Project Hilt Introducción Este proyecto es mi visión particular, ni mejor ni peor (sólo una más) que cualquier otra aproximación a lo que yo enti

TakeNotes, taking care of your tasks and your health
TakeNotes, taking care of your tasks and your health

Take Notes - Para tornar sua rotina mais Saudável TakeNotes, cuidando de suas tarefas e de sua saúde Sobre • Funcionalidades • Layout • Como executar

InterAcao: tasks shared between people in a condominium or the same community
InterAcao: tasks shared between people in a condominium or the same community

Projeto Integrador - Generation Brasil 🤝 🤝 Interação 🤜 🤛 Sobre • Funcionalidades • Implementações futuras • Layout • Como executar • Tecnologias •

To-do-List - Creating a Reminders and Tasks app with Kotlin
To-do-List - Creating a Reminders and Tasks app with Kotlin

To do List 📋 App de Lembretes e Tarefas com Kotlin Telas do App Tecnologias Kot

Taskify is a mobile application used to create and schedule tasks in your TODO list
Taskify is a mobile application used to create and schedule tasks in your TODO list

Taskify is a mobile application used to create and schedule tasks in your TODO list. It is built upon the new Maaterial 3 UI components with the MVVM pattern and the latest Jetpack components.

Jetpack Compose for Desktop and Web, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
Jetpack Compose for Desktop and Web, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.

Jetpack Compose for Desktop and Web, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.

A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations.
A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations.

kotlin-ktor-rest-api A Modern Kotlin-Ktor RESTful API example. Connects to a PostgreSQL database and uses Exposed framework for database operations. F

an open source algorithmic trading framework written in Kotlin for anyone serious about algo-trading
an open source algorithmic trading framework written in Kotlin for anyone serious about algo-trading

Roboquant Roboquant is an algorithmic trading platform that is fast and flexible while at the same time strives to be easy to use. It is fully open so

A simple MVI framework for Kotlin Multiplatform and Android
A simple MVI framework for Kotlin Multiplatform and Android

Orbit Multiplatform Get in touch What is Orbit Orbit is a Redux/MVI-like library - but without the baggage. It's so simple we think of it as MVVM+. Si

Releases(1.0.9)
Owner
null
Ktor is an asynchronous framework for creating microservices, web applications and more.

ktor-sample Ktor is an asynchronous framework for creating microservices, web applications and more. Written in Kotlin from the ground up. Application

mohamed tamer 5 Jan 22, 2022
A pragmatic lightweight dependency injection framework for Kotlin developers.

A pragmatic lightweight dependency injection framework for Kotlin developers. Koin is a DSL, a light container and a pragmatic API

insert-koin.io 11 Dec 14, 2022
🍓CookHelper - food social network. The Api and Websocket are based on Ktor framework. Dependency injection with Koin library.

CookHelper [ ?? Work in Progress ?? ] CookHelper is a cross-platform application that will allow you to cook a delicious dish from an existing recipe

Arthur 11 Nov 9, 2022
High performance and fully asynchronous pulsar client with Kotlin and Vert.x

pulsarkt High performance pulsar client with Kotlin and Vert.x Features Basic Producer/Consumer API Partitioned topics Batching Chunking Compression T

null 1 Nov 5, 2021
Webclient-kotlin-sample - An example of using the http web client to promote synchronous and asynchronous https calls

Web Client Consumer Kotlin Sample The project is an example of using the http we

null 1 May 1, 2022
Asynchronous Spring Initializr API wrapper for Kotlin/JVM

initializr-kt Asynchronous Spring Initializr API wrapper for Kotlin/JVM. This library provides the simplest DSL for initializing Spring Boot projects

Mikhail Titov 2 May 8, 2022
This is LIB - BACKBLELogger

This is LIB - BACKBLELogger Add it in your root build.gradle at the end of repositories: allprojects { repositories { ... maven { url 'https://ji

Arsen Tagaev 1 Sep 23, 2022
A Zero-Dependency Kotlin Faker implementation built to leave you fully satisfied

Satisfaketion A Zero-Dependency Kotlin Faker implementation built to leave you fully satisfied ?? ... With your fake data How to Install ?? Satisfaket

Ryan Brink 7 Oct 3, 2022
Compile-time dependency injection for Kotlin Multiplatform

Kinzhal Kinzhal is a Kotlin Multiplatform library for compile-time dependency injection. The goal is to emulate basic features of Dagger to achieve si

Artem Daugel-Dauge 59 Dec 21, 2022
Depenject - a lightweight, minimalistic dependency injection library for Kotlin/JVM.

depenject depenject is a lightweight, minimalistic dependency injection library for Kotlin/JVM. Our goal is similar to flavor's to simplify the usage

Patrick 1 Mar 22, 2022