Skip to content

Commit f16f1ea

Browse files
authored
Merge pull request #31 from avast/feat/XxxResourceApp
feat: Add *ResourceApp trait to allow more generic applications
2 parents 87e443a + ceb11b6 commit f16f1ea

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.avast.sst.bundle
2+
3+
import cats.effect.{ExitCode, Resource}
4+
import monix.eval.{Task, TaskApp}
5+
import org.slf4j.LoggerFactory
6+
7+
/** Extend this `trait` if you want to implement application using [[monix.eval.Task]] effect data type.
8+
*
9+
* Implement method `program` with initialization and business logic of your application. It will be automatically run and cleaned up.
10+
*/
11+
trait MonixResourceApp[A] extends TaskApp {
12+
13+
private val logger = LoggerFactory.getLogger(this.getClass)
14+
15+
def program: Resource[Task, A]
16+
17+
override def run(args: List[String]): Task[ExitCode] = {
18+
program
19+
.use(_ => Task.unit)
20+
.redeem(
21+
ex => {
22+
logger.error("Application initialization failed!", ex)
23+
ExitCode.Error
24+
},
25+
_ => ExitCode.Success
26+
)
27+
}
28+
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.avast.sst.bundle
2+
3+
import cats.effect.Resource
4+
import org.slf4j.LoggerFactory
5+
import zio.interop.catz._
6+
import zio.{Task, ZIO}
7+
8+
/** Extend this `trait` if you want to implement application using [[zio.ZIO]] effect data type.
9+
*
10+
* Implement method `program` with initialization and business logic of your application. It will be automatically run and cleaned up.
11+
*/
12+
trait ZioResourceApp[A] extends CatsApp {
13+
14+
private val logger = LoggerFactory.getLogger(this.getClass)
15+
16+
def program: Resource[Task, A]
17+
18+
override def run(args: List[String]): ZIO[Environment, Nothing, Int] = {
19+
program
20+
.use(_ => Task.unit)
21+
.fold(
22+
ex => {
23+
logger.error("Application initialization failed!", ex)
24+
1
25+
},
26+
_ => 0
27+
)
28+
}
29+
30+
}

0 commit comments

Comments
 (0)