scalaz.concurrent.Task Scala Examples
The following examples show how to use scalaz.concurrent.Task.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ScalazAsyncHandler.scala From pulsar4s with Apache License 2.0 | 5 votes |
package com.sksamuel.pulsar4s.scalaz import java.util.concurrent.CompletableFuture import java.util.function.BiConsumer import com.sksamuel.pulsar4s.{AsyncHandler, ConsumerMessage, DefaultProducer, MessageId, Producer} import org.apache.pulsar.client.api import org.apache.pulsar.client.api.Consumer import org.apache.pulsar.client.api.{ProducerBuilder, Reader, TypedMessageBuilder} import scalaz.concurrent.Task import scala.language.implicitConversions import scala.util.{Failure, Success, Try} class ScalazAsyncHandler extends AsyncHandler[Task] { implicit def completableVoidToTask(f: => CompletableFuture[Void]): Task[Unit] = completableToTask(f).map(_ => ()) implicit def completableToTask[T](f: => CompletableFuture[T]): Task[T] = { Task.async[T] { k => f.whenCompleteAsync(new BiConsumer[T, Throwable] { override def accept(t: T, e: Throwable): Unit = { if (e != null) k.apply(scalaz.\/.left(e)) else k.apply(scalaz.\/.right(t)) } }) } } override def failed(e: Throwable): Task[Nothing] = Task.fail(e) override def createProducer[T](builder: ProducerBuilder[T]): Task[Producer[T]] = completableToTask(builder.createAsync()).map(new DefaultProducer(_)) override def send[T](t: T, producer: api.Producer[T]): Task[MessageId] = completableToTask(producer.sendAsync(t)).map(MessageId.fromJava) override def receive[T](consumer: api.Consumer[T]): Task[ConsumerMessage[T]] = completableToTask(consumer.receiveAsync).map(ConsumerMessage.fromJava) override def getLastMessageId[T](consumer: api.Consumer[T]): Task[MessageId] = completableToTask(consumer.getLastMessageIdAsync()).map(MessageId.fromJava) override def unsubscribeAsync(consumer: api.Consumer[_]): Task[Unit] = consumer.unsubscribeAsync() override def seekAsync(consumer: api.Consumer[_], messageId: MessageId): Task[Unit] = consumer.seekAsync(messageId) override def seekAsync(reader: api.Reader[_], messageId: MessageId): Task[Unit] = reader.seekAsync(messageId) override def seekAsync(reader: api.Reader[_], timestamp: Long): Task[Unit] = reader.seekAsync(timestamp) override def transform[A, B](f: Task[A])(fn: A => Try[B]): Task[B] = f.flatMap { a => fn(a) match { case Success(b) => Task.now(b) case Failure(e) => Task.fail(e) } } override def acknowledgeAsync[T](consumer: api.Consumer[T], messageId: MessageId): Task[Unit] = consumer.acknowledgeAsync(messageId) override def acknowledgeCumulativeAsync[T](consumer: api.Consumer[T], messageId: MessageId): Task[Unit] = consumer.acknowledgeCumulativeAsync(messageId) override def negativeAcknowledgeAsync[T](consumer: Consumer[T], messageId: MessageId): Task[Unit] = Task { consumer.negativeAcknowledge(messageId) } override def close(reader: Reader[_]): Task[Unit] = reader.closeAsync() override def close(producer: api.Producer[_]): Task[Unit] = producer.closeAsync() override def close(consumer: api.Consumer[_]): Task[Unit] = consumer.closeAsync() override def flush(producer: api.Producer[_]): Task[Unit] = producer.flushAsync() override def nextAsync[T](reader: Reader[T]): Task[ConsumerMessage[T]] = reader.readNextAsync().map(ConsumerMessage.fromJava) override def send[T](builder: TypedMessageBuilder[T]): Task[MessageId] = builder.sendAsync().map(MessageId.fromJava) } object ScalazAsyncHandler { implicit def handler: AsyncHandler[Task] = new ScalazAsyncHandler }
Example 2
Source File: AutoScaling.scala From ionroller with MIT License | 5 votes |
package ionroller.aws import com.amazonaws.auth.AWSCredentialsProvider import com.amazonaws.services.autoscaling.model._ import com.amazonaws.services.autoscaling.{AmazonAutoScaling, AmazonAutoScalingClient} import com.amazonaws.services.elasticloadbalancing.model.InstanceState import com.typesafe.scalalogging.StrictLogging import scala.collection.JavaConverters._ import scala.concurrent.duration.FiniteDuration import scalaz._ import scalaz.concurrent.Task object AutoScaling extends StrictLogging { val client: Kleisli[Task, AWSCredentialsProvider, AmazonAutoScaling] = { Kleisli { credentialsProvider => Task(new AmazonAutoScalingClient(credentialsProvider))(awsExecutorService) } } def getAutoScalingGroupDetails(asgs: Seq[String]): Kleisli[Task, AWSClientCache, List[AutoScalingGroup]] = { Kleisli { client => def go(asgsSoFar: List[AutoScalingGroup], token: Option[String]): Task[List[AutoScalingGroup]] = Task.delay { val req = if (asgs.isEmpty) new DescribeAutoScalingGroupsRequest() else new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asgs: _*) token foreach { t => req.setNextToken(t) } val response = client.asg.describeAutoScalingGroups(req) (asgsSoFar ::: response.getAutoScalingGroups.asScala.toList, Option(response.getNextToken)) } flatMap { case (events, t @ Some(newToken)) if t != token => logger.debug(s"Needed multiple getAutoScalingGroups calls, token=$token newToken=$t") go(events, t) case (events, _) => Task.now(events) } Task.fork(go(List.empty, None))(awsExecutorService) } } def getUnregisteredInstance(elbInstances: Seq[InstanceState], asg: AutoScalingGroup): Option[String] = { val lbInstances = elbInstances.map(_.getInstanceId).toSet val asgInstances = asg.getInstances.asScala val healthyAsgInstances = asgInstances.filter(_.getHealthStatus == "Healthy").map(_.getInstanceId).toSet (healthyAsgInstances -- lbInstances).toSeq.sorted.headOption } def attachElb(asg: AutoScalingGroup, lb: String): Kleisli[Task, AWSClientCache, AttachLoadBalancersResult] = { Kleisli { cache => val attachRequest = new AttachLoadBalancersRequest().withAutoScalingGroupName(asg.getAutoScalingGroupName).withLoadBalancerNames(lb) Task(cache.asg.attachLoadBalancers(attachRequest))(awsExecutorService) } } def detachElb(asg: AutoScalingGroup, lb: String): Kleisli[Task, AWSClientCache, DetachLoadBalancersResult] = { Kleisli { cache => val detachRequest = new DetachLoadBalancersRequest().withAutoScalingGroupName(asg.getAutoScalingGroupName).withLoadBalancerNames(lb) Task(cache.asg.detachLoadBalancers(detachRequest))(awsExecutorService) } } def updateElbHealthCheck(asgs: Seq[String], healthCheckType: String, gracePeriod: FiniteDuration): Kleisli[Task, AWSClientCache, Unit] = { Kleisli { cache => for { _ <- Task.gatherUnordered( asgs map { name => Task( cache.asg.updateAutoScalingGroup( new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(name) .withHealthCheckType(healthCheckType) .withHealthCheckGracePeriod(gracePeriod.toSeconds.toInt) ) )(awsExecutorService) } ) } yield () } } }
Example 3
Source File: ElasticLoadBalancing.scala From ionroller with MIT License | 5 votes |
package ionroller.aws import com.amazonaws.auth.AWSCredentialsProvider import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient import com.amazonaws.services.elasticloadbalancing.model._ import scala.collection.JavaConverters._ import scalaz.Kleisli import scalaz.concurrent.Task object ElasticLoadBalancing { def client(credentialsProvider: AWSCredentialsProvider) = Task(new AmazonElasticLoadBalancingClient(credentialsProvider))(awsExecutorService) def getLoadBalancer(lb: String): Kleisli[Task, AWSClientCache, Option[LoadBalancerDescription]] = { Kleisli { cache => val req = new DescribeLoadBalancersRequest().withLoadBalancerNames(lb) Task(cache.elb.describeLoadBalancers(req).getLoadBalancerDescriptions.asScala.headOption)(awsExecutorService) } } def registerInstances(lb: String, instances: Seq[String]): Kleisli[Task, AWSClientCache, Seq[String]] = { Kleisli { cache => val req = new RegisterInstancesWithLoadBalancerRequest().withLoadBalancerName(lb).withInstances(instances.map(new Instance(_)): _*) Task(cache.elb.registerInstancesWithLoadBalancer(req).getInstances.asScala.map(_.getInstanceId))(awsExecutorService) } } def deregisterInstances(lb: String, instances: Seq[String]): Kleisli[Task, AWSClientCache, Seq[String]] = { Kleisli { cache => val req = new DeregisterInstancesFromLoadBalancerRequest().withLoadBalancerName(lb).withInstances(instances.map(new Instance(_)): _*) Task(cache.elb.deregisterInstancesFromLoadBalancer(req).getInstances.asScala.map(_.getInstanceId))(awsExecutorService) } } def describeInstanceHealth(lb: String): Kleisli[Task, AWSClientCache, Seq[InstanceState]] = { Kleisli { cache => Task { val req = new DescribeInstanceHealthRequest().withLoadBalancerName(lb) cache.elb.describeInstanceHealth(req).getInstanceStates.asScala.toSeq }(awsExecutorService) } } }
Example 4
Source File: ConfigurationManager.scala From ionroller with MIT License | 5 votes |
package ionroller import com.amazonaws.services.elasticbeanstalk.model.ConfigurationOptionSetting import com.typesafe.config.{Config, ConfigException, ConfigFactory} import com.typesafe.scalalogging.StrictLogging import ionroller.aws.Dynamo import play.api.libs.json._ import scalaz.concurrent.Task import scalaz.stream._ import scalaz.{-\/, \/-} class ConfigurationManager(initialConfig: SystemConfiguration) extends StrictLogging { val configurationSignal = async.signalOf(initialConfig) } object ConfigurationManager extends StrictLogging { def apply(initialConfig: SystemConfiguration): ConfigurationManager = new ConfigurationManager(initialConfig) val confFile: Config = ConfigFactory.load() val defaultSolutionStack: String = confFile.getString("ionroller.solution-stack-name") val whitelistKey = "ionroller.modify-environments-whitelist" val blacklistKey = "ionroller.modify-environments-blacklist" val modifyEnvironmentslist: (String, Boolean) => Set[TimelineName] = { (key, required) => try { confFile.getString(key) .split(",") .map(_.trim) .collect({ case s: String if !s.isEmpty && s != "ALL" => s }) .map(TimelineName.apply).toSet } catch { case ex: ConfigException.Missing => { if (required) { logger.error(s"${key} $required configuration is missing.\nRun ION-Roller with property: -D${key}=[ALL|<TIMELINE_NAME_1,TIMELINE_NAME_2,...>]") throw ex } else Set.empty } } } val modifyEnvironmentsWhitelist = modifyEnvironmentslist(whitelistKey, true) val modifyEnvironmentsBlacklist = modifyEnvironmentslist(blacklistKey, false) logger.debug("Processing timelines: " + { if (modifyEnvironmentsWhitelist.isEmpty) "ALL" else modifyEnvironmentsWhitelist }) + { if (!modifyEnvironmentsBlacklist.isEmpty) "excluding: " + modifyEnvironmentsBlacklist else "" } val modifyEnvironments = confFile.getBoolean("ionroller.modify-environments") val defaultOptionSettings: Seq[ConfigurationOptionSetting] = { val options = confFile.getString("ionroller.option-settings") Task(Json.parse(options).as[Seq[ConfigurationOptionSetting]]).attemptRun match { case \/-(settings) => settings case -\/(t) => { logger.error(t.getMessage, t) Seq.empty } } } val defaultResources: JsObject = { val resources = confFile.getString("ionroller.resources") Task(Json.parse(resources).as[JsObject]).attemptRun match { case \/-(resources) => resources case -\/(t) => { logger.error(t.getMessage, t) JsObject(Seq.empty) } } } def getSavedConfiguration: Task[SystemConfiguration] = { for { table <- Dynamo.configTable(None) systemConfig <- Dynamo.getSystemConfig(table) } yield systemConfig } }
Example 5
Source File: package.scala From ionroller with MIT License | 5 votes |
import java.util.concurrent.{ExecutorService, Executors, ScheduledExecutorService} import com.amazonaws.services.elasticbeanstalk.model.ConfigurationOptionSetting import com.typesafe.scalalogging.StrictLogging import ionroller.aws.Dynamo import ionroller.tracking.Event import play.api.libs.functional.syntax._ import play.api.libs.json._ import scala.concurrent.duration.FiniteDuration import scalaz.concurrent.Task import scalaz.{-\/, \/-} package object ionroller extends StrictLogging { val ionrollerExecutorService: ExecutorService = Executors.newFixedThreadPool(4) implicit val `| Implicit executor service |`: ExecutorService = ionrollerExecutorService implicit val ` | is disabled - define explicitly |`: ExecutorService = ionrollerExecutorService implicit val timer: ScheduledExecutorService = scalaz.concurrent.Strategy.DefaultTimeoutScheduler def ionrollerRole(awsAccountId: String) = s"arn:aws:iam::$awsAccountId:role/ionroller" implicit lazy val finiteDurationFormat = { def applyFiniteDuration(l: Long, u: String): FiniteDuration = { FiniteDuration(l, u.toLowerCase) } def unapplyFiniteDuration(d: FiniteDuration): (Long, String) = { (d.length, d.unit.toString) } ((JsPath \ "length").format[Long] and (JsPath \ "unit").format[String])(applyFiniteDuration, unapplyFiniteDuration) } implicit lazy val configurationOptionSettingFormat: Format[ConfigurationOptionSetting] = { def applyConfigOptionSetting(ns: String, optionName: String, value: String) = new ConfigurationOptionSetting(ns, optionName, value) def unapplyConfigOptionSetting(o: ConfigurationOptionSetting): Option[(String, String, String)] = { for { ns <- Option(o.getNamespace) n <- Option(o.getOptionName) v <- Option(o.getValue) } yield (ns, n, v) } ((JsPath \ "Namespace").format[String] and (JsPath \ "OptionName").format[String] and (JsPath \ "Value").format[String])(applyConfigOptionSetting _, unlift(unapplyConfigOptionSetting)) } def enabled(name: TimelineName) = { ConfigurationManager.modifyEnvironments && (ConfigurationManager.modifyEnvironmentsWhitelist.isEmpty || ConfigurationManager.modifyEnvironmentsWhitelist.contains(name)) && !ConfigurationManager.modifyEnvironmentsBlacklist.contains(name) } def logEvent(evt: Event) = { logger.info(s"$evt (enabled = ${enabled(evt.service)})") if (enabled(evt.service)) Dynamo.EventLogger.log(evt) .flatMap({ case \/-(s) => Task.now(()) case -\/(f) => Task.delay(logger.error(f.getMessage, f)) }) else Task.now(()) } }
Example 6
Source File: RestKeyMiddleware.scala From pizza-auth-3 with MIT License | 5 votes |
package moe.pizza.auth.webapp.rest import org.http4s.server.HttpMiddleware import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.scala.DefaultScalaModule import moe.pizza.auth.interfaces.UserDatabase import moe.pizza.auth.webapp.SessionManager._ import moe.pizza.auth.webapp.Types.{HydratedSession, Session2, Session} import org.http4s.{HttpService, _} import org.http4s.server._ import org.slf4j.LoggerFactory import pdi.jwt.{JwtAlgorithm, JwtCirce, JwtClaim} import io.circe.generic.auto._ import scala.util.Try import org.http4s.dsl.{Root, _} import scalaz.concurrent.Task class RestKeyMiddleware(apikeys: List[String]) extends HttpMiddleware { override def apply(s: HttpService): HttpService = Service.lift { req => req.headers .get(headers.Authorization) .map(_.credentials.value.stripPrefix("Bearer ")) .filter(apikeys.contains) match { case Some(k) => s(req) case None => Unauthorized( Challenge(scheme = "Bearer", realm = "Please enter a valid API key")) } } }
Example 7
Source File: Utils.scala From pizza-auth-3 with MIT License | 5 votes |
package moe.pizza.auth.webapp import moe.pizza.auth.interfaces.UserDatabase import moe.pizza.auth.models.Pilot import moe.pizza.auth.webapp.Types.{HydratedSession, Session2, Session} import moe.pizza.auth.webapp.Utils.Alerts.Alerts import org.http4s.{Uri, Response, Request} import org.http4s.dsl.{Root, _} import scalaz.concurrent.Task object Utils { object Alerts extends Enumeration { type Alerts = Value val success = Value("success") val info = Value("info") val warning = Value("warning") val danger = Value("danger") } implicit class PimpedSession2(s: Session2) { def hydrate(u: UserDatabase): HydratedSession = { new HydratedSession(s.alerts, s.redirect, s.uid.flatMap(u.getUser), s.signupData) } } implicit class PimpedHydratedSession(hs: HydratedSession) { def dehydrate(): Session2 = { new Session2(hs.alerts, hs.redirect, hs.pilot.map(_.uid), hs.signupData) } } implicit class PimpedRequest(r: Request) { def flash(level: Alerts, message: String): Option[HydratedSession] = { getSession.map(s => s.copy(alerts = s.alerts :+ Types.Alert(level.toString, message))) } def getSession = r.attributes.get(SessionManager.HYDRATEDSESSION) def setSession(s: Types.HydratedSession): Unit = r.attributes.put(SessionManager.HYDRATEDSESSION, s) def sessionResponse( f: ((HydratedSession, Pilot) => Task[Response]), error: String = "You must be signed in to do that"): Task[Response] = { (getSession, getSession.flatMap(_.pilot)) match { case (Some(s), Some(p)) => f(s, p) case _ => TemporaryRedirect(Uri(path = "/")) } } } implicit class PimpedResponse(r: Response) { def withSession(s: HydratedSession): Response = r.withAttribute(SessionManager.HYDRATEDSESSION, s) def getSession(): Option[HydratedSession] = r.attributes.get(SessionManager.HYDRATEDSESSION) def withNoSession(): Response = r.withAttribute(SessionManager.LOGOUT, "") } implicit class PimpedTaskResponse(r: Task[Response]) { def attachSessionifDefined(s: Option[HydratedSession]): Task[Response] = r.map(res => s.foldLeft(res) { (resp, sess) => resp.withSession(sess) }) def clearSession(): Task[Response] = r.map(res => res.withNoSession()) } def sanitizeUserName(name: String) = name.toLowerCase.replace("'", "").replace(" ", "_") }
Example 8
Source File: InternalWhitelistPilotGraderSpec.scala From pizza-auth-3 with MIT License | 5 votes |
package moe.pizza.auth.plugins.pilotgraders import moe.pizza.auth.models.Pilot import moe.pizza.auth.plugins.pilotgraders.MembershipPilotGraders.{AlliancePilotGrader, CorporationPilotGrader, PublicAccessPilotGrader} import moe.pizza.crestapi.CrestApi import moe.pizza.crestapi.CrestApi.{CallbackResponse, VerifyResponse} import org.scalatest.mock.MockitoSugar import org.scalatest.{MustMatchers, WordSpec} import org.mockito.Mockito._ import org.http4s.client.blaze.PooledHttp1Client import scalaz.concurrent.Task class InternalWhitelistPilotGraderSpec extends WordSpec with MustMatchers with MockitoSugar { implicit val client = PooledHttp1Client() //TODO: no mocking? "InternalWhitelistPilotGrader" when { "grading" should { "grade pilots who are in the list as internal" in { val crest = mock[CrestApi] when(crest.refresh("REF")).thenReturn(Task { new CallbackResponse("access", "type", 1000, Some("refresh")) }) when(crest.verify("access")).thenReturn(Task { new VerifyResponse(1, "Bob", "", "", "", "", "") }) val iwpg = new InternalWhitelistPilotGrader(crest, List(1L, 2L, 3L)) val p = new Pilot("bob", Pilot.Status.unclassified, "boballiance", "bobcorp", "Bob", "none@none", Pilot.OM.createObjectNode(), List.empty[String], List("1:REF"), List.empty[String]) iwpg.grade(p) must equal(Pilot.Status.internal) verify(crest).refresh("REF") verify(crest).verify("access") } "grade pilots who are not in the list as unclassified" in { val crest = mock[CrestApi] when(crest.refresh("REF")).thenReturn(Task { new CallbackResponse("access", "type", 1000, Some("refresh")) }) when(crest.verify("access")).thenReturn(Task { new VerifyResponse(1, "Bob", "", "", "", "", "") }) val iwpg = new InternalWhitelistPilotGrader(crest, List(2L, 3L)) val p = new Pilot("bob", Pilot.Status.unclassified, "boballiance", "bobcorp", "Bob", "none@none", Pilot.OM.createObjectNode(), List.empty[String], List("1:REF"), List.empty[String]) iwpg.grade(p) must equal(Pilot.Status.unclassified) verify(crest).refresh("REF") verify(crest).verify("access") } } } }
Example 9
Source File: CaldariCrestKeyGraderSpec.scala From pizza-auth-3 with MIT License | 5 votes |
package moe.pizza.auth.plugins.pilotgraders import moe.pizza.auth.models.Pilot import moe.pizza.crestapi.CrestApi import moe.pizza.crestapi.CrestApi.{CallbackResponse, VerifyResponse} import moe.pizza.eveapi.{EVEAPI, XMLApiResponse} import moe.pizza.eveapi.endpoints.Character import org.http4s.client.blaze.PooledHttp1Client import org.joda.time.DateTime import org.mockito.Mockito._ import org.scalatest.mock.MockitoSugar import org.scalatest.{MustMatchers, WordSpec} import scalaz.concurrent.Task class CaldariCrestKeyGraderSpec extends WordSpec with MustMatchers with MockitoSugar { implicit val client = PooledHttp1Client() //TODO: no mocking? "CaldariCrestKeyGrader" when { "grading" should { "grade pilots who are caldari as THE ENEMY" in { val crest = mock[CrestApi] val eveapi = mock[EVEAPI] val char = mock[Character] when(eveapi.char).thenReturn(char) when(crest.refresh("REF")).thenReturn(Task { new CallbackResponse("access", "type", 1000, Some("refresh")) }) when(crest.verify("access")).thenReturn(Task { new VerifyResponse(1, "Bob", "", "", "", "", "") }) val pilotInfo = mock[moe.pizza.eveapi.generated.char.CharacterInfo.Result] when(pilotInfo.race).thenReturn("Caldari") when(char.CharacterInfo(1)).thenReturn(Task { new XMLApiResponse(DateTime.now(), DateTime.now(), pilotInfo) }) val iwpg = new CaldariCrestKeyGrader(crest, eve = Some(eveapi)) val p = new Pilot("bob", Pilot.Status.unclassified, "boballiance", "bobcorp", "Bob", "none@none", Pilot.OM.createObjectNode(), List.empty[String], List("1:REF"), List.empty[String]) iwpg.grade(p) must equal(Pilot.Status.banned) verify(crest).refresh("REF") verify(crest).verify("access") verify(char).CharacterInfo(1) verify(pilotInfo).race } "grade pilots who are not caldari as not THE ENEMY" in { val crest = mock[CrestApi] val eveapi = mock[EVEAPI] val char = mock[Character] when(eveapi.char).thenReturn(char) when(crest.refresh("REF")).thenReturn(Task { new CallbackResponse("access", "type", 1000, Some("refresh")) }) when(crest.verify("access")).thenReturn(Task { new VerifyResponse(1, "Bob", "", "", "", "", "") }) val pilotInfo = mock[moe.pizza.eveapi.generated.char.CharacterInfo.Result] when(pilotInfo.race).thenReturn("Gallente") when(char.CharacterInfo(1)).thenReturn(Task { new XMLApiResponse(DateTime.now(), DateTime.now(), pilotInfo) }) val iwpg = new CaldariCrestKeyGrader(crest, eve = Some(eveapi)) val p = new Pilot("bob", Pilot.Status.unclassified, "boballiance", "bobcorp", "Bob", "none@none", Pilot.OM.createObjectNode(), List.empty[String], List("1:REF"), List.empty[String]) iwpg.grade(p) must equal(Pilot.Status.unclassified) verify(crest).refresh("REF") verify(crest).verify("access") verify(char).CharacterInfo(1) verify(pilotInfo).race } } } }
Example 10
Source File: MultigetSpec.scala From doobie-codegen with MIT License | 5 votes |
package mdmoss.doobiegen.queries import doobie.imports._ import mdmoss.doobiegen.db.gen import org.specs2.matcher.ThrownExpectations import org.specs2.mutable.Specification import scalaz.concurrent.Task object MultigetSpec extends Specification with ThrownExpectations { val xa = DriverManagerTransactor[Task]("org.postgresql.Driver", "jdbc:postgresql:gen", "test", "test") val Fk1RowCount = 3 val Fk2RowPerFk1Count = 3 "multiget query functions" >> { val (fk1Rows, fk2Rows) = { for { fk1 <- gen.TestFk_1.createMany(List.fill(Fk1RowCount)(gen.TestFk_1.Shape())) fk2 <- gen.TestFk_2.createMany(fk1.flatMap(fkRow => List.fill(Fk2RowPerFk1Count)(gen.TestFk_2.Shape(fkRow.id)))) } yield (fk1, fk2) }.transact(xa).unsafePerformSync "over primary keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).unsafePerformSync } } "return the same row multiple times if a duplicate primary key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).unsafePerformSync } } } "over foreign keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).unsafePerformSync.map(_.fk) } } "return the same matching set of rows multiple times if a duplicate foreign key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).unsafePerformSync.map(_.fk) } } "only return all matching rows" >> { val fk2RowsByFk = fk2Rows.groupBy(_.fk).mapValues(_.toSet) val fkToQuery = fk1Rows.tail.map(_.id) val expectedResult = fkToQuery.toSet.flatMap(fk2RowsByFk) expectedResult must_=== gen.TestFk_2.multigetByFk(fkToQuery).transact(xa).unsafePerformSync.toSet } } } }
Example 11
Source File: MultigetSpec.scala From doobie-codegen with MIT License | 5 votes |
package mdmoss.doobiegen.queries import doobie.imports._ import mdmoss.doobiegen.db.gen import org.specs2.matcher.ThrownExpectations import org.specs2.mutable.Specification import scalaz.concurrent.Task object MultigetSpec extends Specification with ThrownExpectations { val xa = DriverManagerTransactor[Task]("org.postgresql.Driver", "jdbc:postgresql:gen", "test", "test") val Fk1RowCount = 3 val Fk2RowPerFk1Count = 3 "multiget query functions" >> { val (fk1Rows, fk2Rows) = { for { fk1 <- gen.TestFk_1.createMany(List.fill(Fk1RowCount)(gen.TestFk_1.Shape())) fk2 <- gen.TestFk_2.createMany(fk1.flatMap(fkRow => List.fill(Fk2RowPerFk1Count)(gen.TestFk_2.Shape(fkRow.id)))) } yield (fk1, fk2) }.transact(xa).run "over primary keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).run } } "return the same row multiple times if a duplicate primary key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).run } } } "over foreign keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).run.map(_.fk) } } "return the same matching set of rows multiple times if a duplicate foreign key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).run.map(_.fk) } } "only return all matching rows" >> { val fk2RowsByFk = fk2Rows.groupBy(_.fk).mapValues(_.toSet) val fkToQuery = fk1Rows.tail.map(_.id) val expectedResult = fkToQuery.toSet.flatMap(fk2RowsByFk) expectedResult must_=== gen.TestFk_2.multigetByFk(fkToQuery).transact(xa).run.toSet } } } }
Example 12
Source File: MultigetSpec.scala From doobie-codegen with MIT License | 5 votes |
package mdmoss.doobiegen.queries import doobie.imports._ import mdmoss.doobiegen.db.gen import org.specs2.matcher.ThrownExpectations import org.specs2.mutable.Specification import scalaz.concurrent.Task object MultigetSpec extends Specification with ThrownExpectations { val xa = DriverManagerTransactor[Task]("org.postgresql.Driver", "jdbc:postgresql:gen", "test", "test") val Fk1RowCount = 3 val Fk2RowPerFk1Count = 3 "multiget query functions" >> { val (fk1Rows, fk2Rows) = { for { fk1 <- gen.TestFk_1.createMany(List.fill(Fk1RowCount)(gen.TestFk_1.Shape())) fk2 <- gen.TestFk_2.createMany(fk1.flatMap(fkRow => List.fill(Fk2RowPerFk1Count)(gen.TestFk_2.Shape(fkRow.id)))) } yield (fk1, fk2) }.transact(xa).unsafePerformSync "over primary keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).unsafePerformSync } } "return the same row multiple times if a duplicate primary key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).unsafePerformSync } } } "over foreign keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).unsafePerformSync.map(_.fk) } } "return the same matching set of rows multiple times if a duplicate foreign key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).unsafePerformSync.map(_.fk) } } "only return all matching rows" >> { val fk2RowsByFk = fk2Rows.groupBy(_.fk).mapValues(_.toSet) val fkToQuery = fk1Rows.tail.map(_.id) val expectedResult = fkToQuery.toSet.flatMap(fk2RowsByFk) expectedResult must_=== gen.TestFk_2.multigetByFk(fkToQuery).transact(xa).unsafePerformSync.toSet } } } }
Example 13
Source File: MultigetSpec.scala From doobie-codegen with MIT License | 5 votes |
package mdmoss.doobiegen.queries import doobie.imports._ import mdmoss.doobiegen.db.gen import org.specs2.matcher.ThrownExpectations import org.specs2.mutable.Specification import scalaz.concurrent.Task object MultigetSpec extends Specification with ThrownExpectations { val xa = DriverManagerTransactor[Task]("org.postgresql.Driver", "jdbc:postgresql:gen", "test", "test") val Fk1RowCount = 3 val Fk2RowPerFk1Count = 3 "multiget query functions" >> { val (fk1Rows, fk2Rows) = { for { fk1 <- gen.TestFk_1.createMany(List.fill(Fk1RowCount)(gen.TestFk_1.Shape())) fk2 <- gen.TestFk_2.createMany(fk1.flatMap(fkRow => List.fill(Fk2RowPerFk1Count)(gen.TestFk_2.Shape(fkRow.id)))) } yield (fk1, fk2) }.transact(xa).run "over primary keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).run } } "return the same row multiple times if a duplicate primary key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).run } } } "over foreign keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).run.map(_.fk) } } "return the same matching set of rows multiple times if a duplicate foreign key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).run.map(_.fk) } } "only return all matching rows" >> { val fk2RowsByFk = fk2Rows.groupBy(_.fk).mapValues(_.toSet) val fkToQuery = fk1Rows.tail.map(_.id) val expectedResult = fkToQuery.toSet.flatMap(fk2RowsByFk) expectedResult must_=== gen.TestFk_2.multigetByFk(fkToQuery).transact(xa).run.toSet } } } }
Example 14
Source File: MultigetSpec.scala From doobie-codegen with MIT License | 5 votes |
package mdmoss.doobiegen.queries import doobie.imports._ import org.specs2.matcher.ThrownExpectations import org.specs2.mutable.Specification import scalaz.concurrent.Task import mdmoss.doobiegen.db.gen object MultigetSpec extends Specification with ThrownExpectations { val xa = DriverManagerTransactor[Task]("org.postgresql.Driver", "jdbc:postgresql:gen", "test", "test") val Fk1RowCount = 3 val Fk2RowPerFk1Count = 3 "multiget query functions" >> { val (fk1Rows, fk2Rows) = { for { fk1 <- gen.TestFk_1.createMany(List.fill(Fk1RowCount)(gen.TestFk_1.Shape())) fk2 <- gen.TestFk_2.createMany(fk1.flatMap(fkRow => List.fill(Fk2RowPerFk1Count)(gen.TestFk_2.Shape(fkRow.id)))) } yield (fk1, fk2) }.transact(xa).unsafePerformSync "over primary keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).unsafePerformSync } } "return the same row multiple times if a duplicate primary key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList permutations must_=== permutations.map { rows => gen.TestFk_1.multiget(rows.map(_.id)).transact(xa).unsafePerformSync } } } "over foreign keys must" >> { "order returned rows by input order" >> { val permutations = fk1Rows.permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).unsafePerformSync.map(_.fk) } } "return the same matching set of rows multiple times if a duplicate foreign key is specified in the input" >> { val permutations = (fk1Rows.tail.head :: fk1Rows).permutations.toList val expectedResult = permutations.map(_.flatMap(x => List.fill(Fk2RowPerFk1Count)(x.id))) expectedResult must_=== permutations.map { rows => gen.TestFk_2.multigetByFk(rows.map(_.id)).transact(xa).unsafePerformSync.map(_.fk) } } "only return all matching rows" >> { val fk2RowsByFk = fk2Rows.groupBy(_.fk).mapValues(_.toSet) val fkToQuery = fk1Rows.tail.map(_.id) val expectedResult = fkToQuery.toSet.flatMap(fk2RowsByFk) expectedResult must_=== gen.TestFk_2.multigetByFk(fkToQuery).transact(xa).unsafePerformSync.toSet } } } }
Example 15
Source File: AWSClientCache.scala From ionroller with MIT License | 5 votes |
package ionroller.aws import com.amazonaws.auth.AWSCredentialsProvider import com.amazonaws.services.autoscaling.AmazonAutoScaling import com.amazonaws.services.ec2.AmazonEC2Client import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalk import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancing import com.amazonaws.services.route53.AmazonRoute53 import com.amazonaws.services.s3.AmazonS3 import scalaz.concurrent.Task import scalaz.{Kleisli, Nondeterminism} class AWSClientCache( val role: String, val credentialsProvider: AWSCredentialsProvider, val route53: AmazonRoute53, val elasticBeanstalk: AWSElasticBeanstalk, val s3: AmazonS3, val asg: AmazonAutoScaling, val elb: AmazonElasticLoadBalancing ) object AWSClientCache { private[ionroller] val cache: java.util.concurrent.ConcurrentHashMap[String, AWSClientCache] = new java.util.concurrent.ConcurrentHashMap val getCache: Kleisli[Task, String, AWSClientCache] = { Kleisli { role => Option(cache.get(role)) match { case None => for { credentials <- CredentialsProvider(role) route53Client = Route53.client(credentials) elasticBeanstalkClient = ElasticBeanstalk.client(credentials) s3Client = S3.client(credentials) asgClient = AutoScaling.client(credentials) elbClient = ElasticLoadBalancing.client(credentials) newItem <- Nondeterminism[Task].apply5(route53Client, elasticBeanstalkClient, s3Client, asgClient, elbClient) { case (r53, eb, s3, asg, elb) => val newEntry = new AWSClientCache(role, credentials, r53, eb, s3, asg, elb) cache.put(role, newEntry) newEntry } } yield newItem case Some(e) => Task.now(e) } } } }
Example 16
Source File: AsyncHttpClientScalazBackend.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.asynchttpclient.scalaz import java.nio.ByteBuffer import io.netty.buffer.ByteBuf import org.asynchttpclient.{ AsyncHttpClient, AsyncHttpClientConfig, BoundRequestBuilder, DefaultAsyncHttpClient, DefaultAsyncHttpClientConfig } import org.reactivestreams.Publisher import scalaz.concurrent.Task import sttp.client.asynchttpclient.{AsyncHttpClientBackend, WebSocketHandler} import sttp.client.impl.scalaz.TaskMonadAsyncError import sttp.client.testing.SttpBackendStub import sttp.client.{FollowRedirectsBackend, SttpBackend, SttpBackendOptions} class AsyncHttpClientScalazBackend private ( asyncHttpClient: AsyncHttpClient, closeClient: Boolean, customizeRequest: BoundRequestBuilder => BoundRequestBuilder ) extends AsyncHttpClientBackend[Task, Nothing](asyncHttpClient, TaskMonadAsyncError, closeClient, customizeRequest) { override protected def streamBodyToPublisher(s: Nothing): Publisher[ByteBuf] = s // nothing is everything override protected def publisherToStreamBody(p: Publisher[ByteBuffer]): Nothing = throw new IllegalStateException("This backend does not support streaming") } object AsyncHttpClientScalazBackend { private def apply( asyncHttpClient: AsyncHttpClient, closeClient: Boolean, customizeRequest: BoundRequestBuilder => BoundRequestBuilder ): SttpBackend[Task, Nothing, WebSocketHandler] = new FollowRedirectsBackend[Task, Nothing, WebSocketHandler]( new AsyncHttpClientScalazBackend(asyncHttpClient, closeClient, customizeRequest) ) def apply( options: SttpBackendOptions = SttpBackendOptions.Default, customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity ): Task[SttpBackend[Task, Nothing, WebSocketHandler]] = Task.delay( AsyncHttpClientScalazBackend(AsyncHttpClientBackend.defaultClient(options), closeClient = true, customizeRequest) ) def usingConfig( cfg: AsyncHttpClientConfig, customizeRequest: BoundRequestBuilder => BoundRequestBuilder = identity ): Task[SttpBackend[Task, Nothing, WebSocketHandler]] = Task.delay(AsyncHttpClientScalazBackend(new DefaultAsyncHttpClient(cfg), closeClient = true, customizeRequest)) def stub: SttpBackendStub[Task, Nothing, WebSocketHandler] = SttpBackendStub(TaskMonadAsyncError) }
Example 17
Source File: TaskMonadAsyncError.scala From sttp with Apache License 2.0 | 5 votes |
package sttp.client.impl.scalaz import sttp.client.monad.{Canceler, MonadAsyncError} import scalaz.concurrent.Task import scalaz.{-\/, \/-} object TaskMonadAsyncError extends MonadAsyncError[Task] { override def unit[T](t: T): Task[T] = Task.point(t) override def map[T, T2](fa: Task[T])(f: (T) => T2): Task[T2] = fa.map(f) override def flatMap[T, T2](fa: Task[T])(f: (T) => Task[T2]): Task[T2] = fa.flatMap(f) override def async[T](register: (Either[Throwable, T] => Unit) => Canceler): Task[T] = Task.async { cb => register { case Left(t) => cb(-\/(t)) case Right(t) => cb(\/-(t)) } } override def error[T](t: Throwable): Task[T] = Task.fail(t) override protected def handleWrappedError[T](rt: Task[T])(h: PartialFunction[Throwable, Task[T]]): Task[T] = rt.handleWith(h) override def eval[T](t: => T): Task[T] = Task(t) }
Example 18
Source File: SSESpec.scala From funnel with Apache License 2.0 | 5 votes |
package funnel package http import com.twitter.algebird.Group import org.scalacheck.Arbitrary._ import org.scalacheck.Prop._ import org.scalacheck._ import scalaz.concurrent.Task._ import scalaz._ import Scalaz._ import java.io.InputStream import scalaz.concurrent.Task import scalaz.stream.Process object SSESpec extends Properties("SSE") { property("url parse process propagates Exceptions") = secure { val x: Process[Task, String] = Process.eval{Task { throw new RuntimeException("boom")}} SSE.readUrl(x) x.attempt().runLast.run match { case Some(x) if (x.isLeft) => true case _ => false } } }
Example 19
Source File: Riemann.scala From funnel with Apache License 2.0 | 5 votes |
package funnel package riemann import com.aphyr.riemann.client.RiemannClient import com.aphyr.riemann.Proto.{Event => REvent} import Events.Event import scala.concurrent.duration._ import scalaz.\/ import scalaz.concurrent.{Strategy,Task,Actor} import scalaz.stream.{async,Process,time} import scala.collection.JavaConverters._ import scalaz.stream.async.mutable.Signal import journal.Logger object Riemann { val log = Logger[this.type] sealed trait Pusher final case class Hold(e: REvent) extends Pusher final case object Flush extends Pusher @volatile private var store: List[REvent] = Nil private[riemann] def collector( R: RiemannClient ): Actor[Pusher] = { implicit val S = Strategy.Executor(Monitoring.serverPool) implicit val P = Monitoring.schedulingPool val a = Actor.actor[Pusher] { case Hold(e) => store = (e :: store) case Flush => { R.sendEvents(store.asJava) log.debug(s"successfully sent batch of ${store.length}") store = Nil } }(S) time.awakeEvery(1.minute)(S, P).evalMap {_ => Task(a(Flush)) }.run.runAsync(_ => ()) a } private def splitStats(key: Key[Any], s: Stats): List[Datapoint[Any]] = { List( Datapoint(key.modifyName(_ + "/count"), s.count.toDouble), Datapoint(key.modifyName(_ + "/variance"), s.variance), Datapoint(key.modifyName(_ + "/mean"), s.mean), Datapoint(key.modifyName(_ + "/last"), s.last.getOrElse(Double.NaN)), Datapoint(key.modifyName(_ + "/standardDeviation"), s.standardDeviation) ) } private def trafficLightToRiemannState(dp: Datapoint[Any]): Datapoint[Any] = if(dp.units == Units.TrafficLight) dp.value match { case TrafficLight.Red => Datapoint(dp.key, "critical") case TrafficLight.Yellow => Datapoint(dp.key, "warning") case TrafficLight.Green => Datapoint(dp.key, "ok") } else dp private def tags(s: String) = s.split("/") def publishToRiemann[A]( M: Monitoring, ttlInSeconds: Float = 20f )(riemannClient: RiemannClient, riemannName: String)( myName: String = "Funnel Mirror" ): Task[Unit] = { val actor = collector(riemannClient) publish(M, ttlInSeconds)(riemannClient, actor) } }
Example 20
Source File: package.scala From modelmatrix with Apache License 2.0 | 5 votes |
package com.collective.modelmatrix import java.time.ZoneId import java.time.format.{FormatStyle, DateTimeFormatter} import java.util.Locale import com.bethecoder.ascii_table.{ASCIITableHeader, ASCIITable} import com.collective.modelmatrix.transform.{Index, Top, Identity, Transform} import scala.language.implicitConversions package object cli { val timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) .withLocale(Locale.US) .withZone(ZoneId.systemDefault()) implicit def stringToASCIITableHeader(s: String): ASCIITableHeader = new ASCIITableHeader(s) implicit class StringOps(val s: String) extends AnyVal { def dataLeftAligned: ASCIITableHeader = new ASCIITableHeader(s, -1) } import scalaz.stream._ import scalaz.concurrent.Task implicit class ConcurrentProcess[O](val process: Process[Task, O]) { def concurrently[O2](concurrencyLevel: Int) (f: Channel[Task, O, O2]): Process[Task, O2] = { val actions = process. zipWith(f)((data, f) => f(data)) val nestedActions = actions.map(Process.eval) merge.mergeN(concurrencyLevel)(nestedActions) } } }
Example 21
Source File: ScalazMain.scala From advanced-scala-code with Apache License 2.0 | 5 votes |
import java.nio.charset.Charset import java.util.concurrent.Executors import org.asynchttpclient.DefaultAsyncHttpClient import scala.concurrent.Future import scalaz.{-\/, \/, \/-} import scalaz.concurrent.Task object ScalazMain { def main(args: Array[String]): Unit = { def performAction(num: Int): Unit = println(s"Task #$num is executing in ${Thread.currentThread().getName}") import scala.concurrent.ExecutionContext.Implicits.global val result1F = Future { performAction(0) } val result2F = Future.successful { performAction(1) } // Executes immediately in the main thread val result2T = Task.now { performAction(2) } // Schedules an execution in a default worker thread // = Executors.newFixedThreadPool(Math.max(4, Runtime.getRuntime.availableProcessors), DefaultDaemonThreadFactory) val result3T = Task { performAction(3) } // Lifts a code block to a Task without scheduling an execution val result4T = Task.delay { performAction(4) } result3T.unsafePerformAsync(_ => ()) implicit val executorService = Executors.newSingleThreadExecutor() val result5T = Task { performAction(5) } result3T.unsafePerformSync val asyncHttpClient = new DefaultAsyncHttpClient() arm.ArmUtils.using(asyncHttpClient) { val result6T = Task.async[String](handler => { asyncHttpClient.prepareGet("https://httpbin.org/get").execute(). toCompletableFuture.whenComplete { (response, exc) => { if (exc == null) { handler(\/.right(response.getResponseBody(Charset.forName("UTF-8")))) } else handler(-\/(exc)) }} }) val responseString = result6T.unsafePerformSync println(responseString) } } }
Example 22
Source File: EitherTransformers.scala From advanced-scala-code with Apache License 2.0 | 5 votes |
package manual import cats.{Functor, Monad} import cats.data.{EitherT} import scala.concurrent.Future import scalaz.concurrent.Task implicit val taskMonad = new Monad[Task] { def tailRecM[A,B](a: A)(f: A => Task[Either[A,B]]): Task[B] = Task.suspend(f(a)).flatMap { case Left(continueA) => tailRecM(continueA)(f) case Right(b) => Task.now(b) } override def flatMap[A, B](fa: Task[A])(f: (A) => Task[B]): Task[B] = fa.flatMap(f) override def pure[A](x: A): Task[A] = Task.now(x) } val resultTXT = for { num1 <- EitherT(num1TX) num2 <- EitherT(num2TX) } yield num1 + num2 val resultX = resultTXT.value.unsafePerformSync println(s"Result: $resultX") } }
Example 23
Source File: KleisliMonad.scala From advanced-scala-code with Apache License 2.0 | 5 votes |
import cats.{Monad, data} import scalaz.concurrent.Task object KleisliMonad { class AuthService { def isLogged(name: String): Task[Boolean] = Task { name.length == 3 } } class UserService { def greet(name: String, isLogged: Boolean): Task[String] = Task { val actualName = if (isLogged) name else "User" s"Hello $actualName" } } case class Environment(userName: String, userService: UserService, authService: AuthService) def main(args: Array[String]) { def isLoggedUser = data.ReaderT[Task, Environment, Boolean] { env => env.authService.isLogged(env.userName) } def greetUser(logged: Boolean) = data.ReaderT[Task, Environment, String] { env => env.userService.greet(env.userName, logged) } implicit val taskMonad = new Monad[Task] { def tailRecM[A,B](a: A)(f: A => Task[Either[A,B]]): Task[B] = Task.suspend(f(a)).flatMap { case Left(continueA) => tailRecM(continueA)(f) case Right(b) => Task.now(b) } override def flatMap[A, B](fa: Task[A])(f: (A) => Task[B]): Task[B] = fa.flatMap(f) override def pure[A](x: A): Task[A] = Task.now(x) } val resultR = for { logged <- isLoggedUser greeting <- greetUser(logged) } yield greeting val environment = Environment("Joe", new UserService, new AuthService) println(resultR.run(environment).unsafePerformSync) // Using Kleisli->local case class ExternalContext(env: Environment) val externalContext = ExternalContext(environment) def context2env(ec: ExternalContext): Environment = ec.env val resultContextR = resultR.local(context2env) println(resultContextR.run(externalContext).unsafePerformSync) } }
Example 24
Source File: FreeMonad.scala From advanced-scala-code with Apache License 2.0 | 5 votes |
import scala.io.StdIn import scalaz.concurrent.Task trait ActionA[A] case class ReadAction() extends ActionA[String] case class WriteAction(output: String) extends ActionA[Unit] object FreeMonad { def main(args: Array[String]) { import cats.free.Free type ActionF[A] = Free[ActionA, A] import cats.free.Free.liftF def read(): ActionF[String] = liftF[ActionA, String](ReadAction()) def write(output: String): ActionF[Unit] = liftF[ActionA, Unit](WriteAction(output)) val result = for { _ <- write("Write your name: ") name <- read() res <- write(s"Hello $name") } yield res // result: Free[ActionA, Unit] import cats.arrow.FunctionK import cats.{Id, Monad} val idInterpreter: FunctionK[ActionA, Id] = new FunctionK[ActionA, Id] { override def apply[A](fa: ActionA[A]): Id[A] = fa match { case ReadAction() => val input = StdIn.readLine() input case WriteAction(output) => println(output) } } def taskInterpreter: FunctionK[ActionA, Task] = new FunctionK[ActionA, Task] { def apply[A](fa: ActionA[A]): Task[A] = (fa match { case ReadAction() => Task.delay { val input = StdIn.readLine() input } case WriteAction(output) => Task.delay { println(output) } }).asInstanceOf[Task[A]] } implicit val taskMonad = new Monad[Task] { override def tailRecM[A,B](a: A)(f: A => Task[Either[A,B]]): Task[B] = Task.suspend(f(a)).flatMap { case Left(continueA) => tailRecM(continueA)(f) case Right(b) => Task.now(b) } override def flatMap[A, B](fa: Task[A])(f: (A) => Task[B]): Task[B] = fa.flatMap(f) override def pure[A](x: A): Task[A] = Task.now(x) } result.foldMap(taskInterpreter).unsafePerformSync } }
Example 25
Source File: ScalafmtCoursierPlugin.scala From neo-sbt-scalafmt with Apache License 2.0 | 5 votes |
package com.lucidchart.scalafmt.coursier import com.lucidchart.sbt.scalafmt.ScalafmtCorePlugin import com.lucidchart.sbt.scalafmt.ScalafmtCorePlugin.autoImport._ import coursier._ import java.io.{FileNotFoundException, IOException} import sbt.KeyRanks._ import sbt.Keys._ import sbt._ import scala.util.control.NonFatal import scalaz.concurrent.Task object ScalafmtCoursierPlugin extends AutoPlugin { object autoImport { val scalafmtCoursierRepositories = SettingKey[Seq[Repository]]("scalafmt-coursier-repositories", "Coursier respositories for scalafmt", BTask) } import autoImport._ override val projectSettings = Seq( externalDependencyClasspath in Scalafmt := { val dependencies = (libraryDependencies in Scalafmt).value .map(module => Dependency(Module(module.organization, module.name), module.revision)) .toSet val cacheFile = streams.value.cacheDirectory / "dependencies" val newHash = dependencies.hashCode val cached = try { IO.readLines(cacheFile) match { case hash +: fileStrings => val files = fileStrings.map(file) if (hash.toInt == newHash && files.forall(_.exists)) Some(files) else None } } catch { case _: FileNotFoundException => None case NonFatal(e) => streams.value.log.error(e.getLocalizedMessage) None } Attributed.blankSeq(cached.getOrElse { synchronized { val fetch = Fetch.from(scalafmtCoursierRepositories.value, coursier.Cache.fetch()) streams.value.log.info(s"Fetching scalafmt for ${Reference.display(thisProjectRef.value)}") val resolution = Resolution(dependencies).process.run(fetch).unsafePerformSync val result = Task .gatherUnordered(resolution.artifacts.map(coursier.Cache.file(_).run)) .unsafePerformSync .map(_.valueOr(error => throw new IOException(error.describe))) .filter(_.ext == "jar") .sorted streams.value.log.info(s"Fetched ${result.size} artifacts for scalafmt") IO.writeLines(cacheFile, newHash.toString +: result.map(_.toString)) result } }) }, scalafmtUseIvy := false ) override val buildSettings = Seq( scalafmtCoursierRepositories := Seq( coursier.Cache.ivy2Local, coursier.MavenRepository("https://repo1.maven.org/maven2") ) ) override val requires = ScalafmtCorePlugin override val trigger = allRequirements }
Example 26
Source File: package.scala From libisabelle with Apache License 2.0 | 5 votes |
package info.hupel.isabelle import scala.concurrent.{Future, Promise} import scalaz.concurrent.Task package object setup { implicit class TaskOps[T](task: Task[T]) { def toScalaFuture: Future[T] = { val promise = Promise[T] task.unsafePerformAsync { res => res.fold(promise.failure, promise.success) () } promise.future } } }
Example 27
Source File: Backtest.scala From Scala-High-Performance-Programming with MIT License | 5 votes |
package highperfscala.concurrency.backtesting import org.joda.time.MonthDay import scalaz.concurrent.Task object Backtest { case class PnL(value: BigDecimal) extends AnyVal object PnL { def merge(x: PnL, y: PnL): PnL = PnL(x.value + y.value) val zero: PnL = PnL(0) } case class BacktestPerformanceSummary(pnl: PnL) case class DecisionDelayMillis(value: Long) extends AnyVal def originalBacktest( testDays: List[MonthDay], decisionDelay: DecisionDelayMillis): BacktestPerformanceSummary = { val pnls = for { d <- testDays _ = Thread.sleep(decisionDelay.value) } yield PnL(10) BacktestPerformanceSummary(pnls.reduceOption(PnL.merge).getOrElse(PnL.zero)) } def backtestWithoutConcurrency( testDays: List[MonthDay], decisionDelay: DecisionDelayMillis): Task[BacktestPerformanceSummary] = { val ts = for (d <- testDays) yield Task.delay { Thread.sleep(decisionDelay.value) PnL(10) } Task.gatherUnordered(ts).map(pnls => BacktestPerformanceSummary( pnls.reduceOption(PnL.merge).getOrElse(PnL.zero))) } def backtestWithAllForked( testDays: List[MonthDay], decisionDelay: DecisionDelayMillis): Task[BacktestPerformanceSummary] = { val ts = for (d <- testDays) yield Task.fork { Thread.sleep(decisionDelay.value) Task.now(PnL(10)) } Task.gatherUnordered(ts).map(pnls => BacktestPerformanceSummary( pnls.reduceOption(PnL.merge).getOrElse(PnL.zero))) } def backtestWithBatchedForking( testDays: List[MonthDay], decisionDelay: DecisionDelayMillis): Task[BacktestPerformanceSummary] = { val ts = for (d <- testDays) yield Task.delay { Thread.sleep(decisionDelay.value) PnL(10) } Task.gatherUnordered(ts.sliding(30, 30).toList.map(xs => Task.fork(Task.gatherUnordered(xs)))).map(pnls => BacktestPerformanceSummary( pnls.flatten.reduceOption(PnL.merge).getOrElse(PnL.zero))) } }
Example 28
Source File: TaskFutureBenchmarks.scala From Scala-High-Performance-Programming with MIT License | 5 votes |
package highperfscala.concurrency.task import java.util.concurrent.{ExecutorService, Executors, TimeUnit} import org.openjdk.jmh.annotations.Mode.Throughput import org.openjdk.jmh.annotations._ import scala.concurrent.{ExecutionContext, Future, Await} import scala.concurrent.duration.Duration import scalaz.concurrent.Task @BenchmarkMode(Array(Throughput)) @OutputTimeUnit(TimeUnit.SECONDS) @Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 30, time = 10, timeUnit = TimeUnit.SECONDS) @Fork(value = 1, warmups = 1, jvmArgs = Array("-Xms1G", "-Xmx1G")) class TaskFutureBenchmarks { import TaskFutureBenchmarks._ @Benchmark def mapWithFuture(state: TaskFutureState): Int = { implicit val ec = state.context val init = Future(0) val res = (1 until state.operations).foldLeft(init)((f, _) => f.map(_ + 1)) Await.result(res, Duration("5 minutes")) } @Benchmark def mapWithTask(state: TaskFutureState): Int = { val init = Task(0)(state.es) val res = (1 until state.operations).foldLeft(init)((t, _) => t.map(_ + 1)) res.unsafePerformSync } @Benchmark def flatMapWithFuture(state: TaskFutureState): Int = { implicit val ec = state.context val init = Future(0) val res = (1 until state.operations).foldLeft(init)((f, _) => f.flatMap(i => Future(i + 1))) Await.result(res, Duration("5 minutes")) } @Benchmark def flatMapWithTask(state: TaskFutureState): Int = { val init = Task(0)(state.es) val res = (1 until state.operations).foldLeft(init)((t, _) => t.flatMap(i => Task(i + 1)(state.es))) res.unsafePerformSync } } object TaskFutureBenchmarks { @State(Scope.Benchmark) class TaskFutureState { @Param(Array("5", "10", "100")) var operations: Int = 0 var es: ExecutorService = null var context: ExecutionContext = null @Setup(Level.Trial) def setup(): Unit = { es = Executors.newFixedThreadPool(20) context = ExecutionContext.fromExecutor(es) } @TearDown(Level.Trial) def tearDown(): Unit = { es.shutdownNow() } } }
Example 29
Source File: QueryStrategy.scala From couchdb-scala with Apache License 2.0 | 5 votes |
package com.ibm.couchdb.api.builders import com.ibm.couchdb.api.Query import com.ibm.couchdb.core.Client import com.ibm.couchdb.{CouchDocs, CouchView, Req, Res, TypeMapping} import upickle.default.Aliases.{R, W} import upickle.default._ import com.ibm.couchdb._ import scala.reflect.ClassTag import scalaz.concurrent.Task case class QueryBasic[C: R]( client: Client, db: String, url: String, params: Map[String, String] = Map.empty, ids: Seq[String] = Seq.empty) { private val queryOps = QueryOps(client) def query: Task[C] = { ids match { case Nil => queryOps.query[C](url, params) case _ => queryOps.queryByIds[String, C](url, ids, params) } } } case class QueryView[K: W, C: R]( client: Client, db: String, design: Option[String], params: Map[String, String] = Map.empty, ids: Seq[K] = Seq.empty, view: Option[String], tempView: Option[CouchView]) { private lazy val url = (view, design) match { case (Some(v), Some(d)) => s"/$db/_design/$d/_view/$v" case _ => s"/$db/_temp_view" } private val queryOps = QueryOps(client) def query: Task[C] = { ids match { case Nil => queryWithoutIds case _ => queryByIds } } private def queryWithoutIds: Task[C] = tempView match { case Some(t) => queryOps.postQuery[CouchView, C](url, t, params) case None => queryOps.query[C](url, params) } private def queryByIds: Task[C] = tempView match { case Some(t) => queryOps.postQuery[Req.ViewWithKeys[K], C]( url, Req.ViewWithKeys(ids, t), params) case None => queryOps.queryByIds[K, C](url, ids, params) } } case class QueryByType[K, V, D: R]( client: Client, db: String, typeFilterView: CouchView, typeMappings: TypeMapping, params: Map[String, String] = Map.empty, ids: Seq[String] = Seq.empty) (implicit tag: ClassTag[D], kr: R[K], kw: W[K], vr: R[V]) { def query: Task[CouchDocs[K, V, D]] = { typeMappings.get(tag.runtimeClass) match { case Some(k) => queryByType(typeFilterView, k.name) case None => Res.Error("not_found", s"type mapping not found").toTask } } private def queryByType(view: CouchView, kind: String, ps: Map[String, String] = Map.empty) = { new Query(client, db).temporaryView[K, V](view) match { case Some(v) => v.startKey(Tuple1(kind)).endKey(Tuple2(kind, {})).includeDocs[D].build.query case None => Res.Error("not_found", "invalid view specified").toTask } } }
Example 30
Source File: Service.scala From functional-microservice-example with Apache License 2.0 | 5 votes |
package example import org.http4s._ import org.http4s.dsl._ import org.http4s.server._ import scalaz.concurrent.Task object ToDoService { import scala.concurrent.ExecutionContext def service(todo: ToDo)(config: todo.TConfig)(implicit executionContext: ExecutionContext = ExecutionContext.global) = HttpService { case req @ GET -> Root => Ok("Hello, World") case GET -> Root / "list" => Ok("not implemented") todo.list(config).map(Ok(_)) case req @ POST -> Root / "create" => req.decode[UrlForm] { data => data.values.get("content").flatMap(_.headOption) match { case Some(a) => todo.create(a)(config).map(Ok(_)) case None => Task.now(BadRequest("dsfds")) } } } }
Example 31
Source File: Repository.scala From functional-microservice-example with Apache License 2.0 | 5 votes |
package example import scalaz.Monad import scalaz.syntax.monad._ import scalaz.concurrent.Task import doobie.imports._ import doobie.contrib.h2.h2types._ abstract class Repository[M[_] : Monad] { def list: M[List[Item]] def create(item: Item): M[Item.Id] } class InMemoryRepository extends Repository[Task] with DAO { // An in-memory database val xa = DriverManagerTransactor[Task]( "org.h2.Driver", "jdbc:h2:mem:todo;DB_CLOSE_DELAY=-1", "sa", "" ) def list: Task[List[Item]] = withInit(allItems.list).transact(xa) def create(item: Item): Task[Item.Id] = withInit(insertItem(item).run).transact(xa).as(item.id) } trait DAO { // On table not found, init the db and try again def withInit[A](a: ConnectionIO[A]): ConnectionIO[A] = a.exceptSomeSqlState { case SqlState("42S02") => init.run *> a } def allItems: Query0[Item] = sql""" SELECT id, content, created_at FROM items ORDER BY created_at ASC """.query[Item] def insertItem(item: Item): Update0 = sql""" INSERT INTO items (id, content, created_at) VALUES (${item.id}, ${item.content}, ${item.createdAt}) """.update def init: Update0 = sql""" CREATE TABLE items ( id UUID PRIMARY KEY, content VARCHAR NOT NULL, created_at LONG NOT NULL ) """.update }
Example 32
Source File: ToDo.scala From functional-microservice-example with Apache License 2.0 | 5 votes |
package example import java.util.UUID import scalaz.{\/,-\/,\/-,Kleisli} import scalaz.concurrent.Task import scalaz.syntax.kleisli._ object Item { type Id = UUID } case class Item( id: Item.Id, content: String, createdAt: Long) class ToDo { type TConfig = ToDoConfig[Task] type ToDoK[A] = Kleisli[Task, TConfig, A] protected def config: ToDoK[TConfig] = Kleisli.ask[Task, TConfig] protected val currentTimeMillis: ToDoK[Long] = Task.delay(System.currentTimeMillis).liftKleisli protected val randomUUID: ToDoK[UUID] = Task.delay(UUID.randomUUID).liftKleisli def create(content: String): ToDoK[Item.Id] = { for { a <- config u <- randomUUID t <- currentTimeMillis i = Item(u, content, t) b <- a.repository.create(i).liftKleisli } yield b } def list: ToDoK[List[Item]] = for { a <- config b <- a.repository.list.liftKleisli } yield b }
Example 33
Source File: ModelServer.scala From reactive-machine-learning-systems with MIT License | 5 votes |
package com.reactivemachinelearning import org.http4s.argonaut._ import org.http4s.server.{Server, ServerApp} import org.http4s.server.blaze._ import org.http4s._ import org.http4s.dsl._ import scalaz.concurrent.Task object ModelServer extends ServerApp { // implicit def responseEncoder: EntityEncoder[Response] = jsonEncoderOf[Response] def splitTraffic(data: String) = { data.hashCode % 10 match { case x if x < 4 => Client.callA(data) case _ => Client.callB(data) } } val apiService = HttpService { case GET -> Root / "predict" / inputData => val response = splitTraffic(inputData).run Ok(response.toString()) } override def server(args: List[String]): Task[Server] = { BlazeBuilder .bindLocal(8080) .mountService(apiService, "/api") .mountService(Models.modelA, "/models") .mountService(Models.modelB, "/models") .start } }
Example 34
Source File: ModelSupervisor.scala From reactive-machine-learning-systems with MIT License | 5 votes |
package com.reactivemachinelearning import org.http4s.server.{Server, ServerApp} import org.http4s.server.blaze._ import org.http4s._ import org.http4s.dsl._ import scalaz.concurrent.Task object ModelSupervisor extends ServerApp { def splitTraffic(data: String) = { data.hashCode % 10 match { case x if x < 4 => Client.callA(data) case x if x < 6 => Client.callB(data) case _ => Client.callC(data) } } val apiService = HttpService { case GET -> Root / "predict" / inputData => val response = splitTraffic(inputData).run response match { case r: Response if r.status == Ok => Response(Ok).withBody(r.bodyAsText) case r => Response(BadRequest).withBody(r.bodyAsText) } } override def server(args: List[String]): Task[Server] = { BlazeBuilder .bindLocal(8080) .mountService(apiService, "/api") .mountService(Models.modelA, "/models") .mountService(Models.modelB, "/models") .mountService(Models.modelC, "/models") .start } }
Example 35
Source File: ToFutureImplicits.scala From octopus with Apache License 2.0 | 5 votes |
package octopus.async.scalaz import octopus.async.ToFuture import scalaz.{-\/, \/-} import scalaz.concurrent.Task import scala.concurrent.{Future, Promise} object ToFutureImplicits { implicit val scalazTaskToFuture: ToFuture[Task] = new ToFuture[Task] { def toFuture[A](value: Task[A]): Future[A] = { val p: Promise[A] = Promise() value.unsafePerformAsync { case -\/(ex) => p.failure(ex) () case \/-(r) => p.success(r) () } p.future } } }
Example 36
Source File: TaskInstances.scala From shims with Apache License 2.0 | 5 votes |
package shims.effect.instances import cats.{Applicative, Monad, Parallel, StackSafeMonad, ~>} import cats.effect.{Effect, ExitCase, IO, SyncIO} import scalaz.{Tag, -\/, \/, \/-} import scalaz.concurrent.Task.ParallelTask import scalaz.concurrent.{Future, Task} import shims.conversions.MonadErrorConversions import java.util.concurrent.atomic.AtomicBoolean import scala.util.control.NonFatal trait TaskInstances extends MonadErrorConversions { // cribbed from quasar, where it was mostly cribbed from scalaz-task-effect implicit object taskEffect extends Effect[Task] with StackSafeMonad[Task] { def pure[A](x: A): Task[A] = Task.now(x) def handleErrorWith[A](fa: Task[A])(f: Throwable => Task[A]): Task[A] = fa.handleWith(functionToPartial(f)) def raiseError[A](e: Throwable): Task[A] = Task.fail(e) // In order to comply with `repeatedCallbackIgnored` law // on async, a custom AtomicBoolean is required to ignore // second callbacks. def async[A](k: (Either[Throwable, A] => Unit) => Unit): Task[A] = Task.async { registered => val a = new AtomicBoolean(true) try k(e => if (a.getAndSet(false)) registered(\/.fromEither(e)) else ()) catch { case NonFatal(t) => registered(-\/(t)) } } def asyncF[A](k: (Either[Throwable, A] => Unit) => Task[Unit]): Task[A] = async(k.andThen(_.unsafePerformAsync(forget))) // emulates using attempt def bracketCase[A, B](acquire: Task[A])(use: A => Task[B])(release: (A, ExitCase[Throwable]) => Task[Unit]): Task[B] = { for { a <- acquire bOr <- use(a).attempt ec = bOr.fold(ExitCase.Error(_), _ => ExitCase.Completed) _ <- release(a, ec) b <- bOr.fold(Task.fail, Task.now) } yield b } def runAsync[A](fa: Task[A])(cb: Either[Throwable, A] => IO[Unit]): SyncIO[Unit] = SyncIO { fa unsafePerformAsync { disjunction => cb(disjunction.toEither).unsafeRunAsync(forget) } } def runSyncStep[A](fa: Task[A]): IO[Either[Task[A], A]] = IO { fa.get match { case Future.Now(-\/(_)) => Left(fa) case other => other.step match { case Future.Now(\/-(a)) => Right(a) case other => Left(new Task(other)) } } } override def map[A, B](fa: Task[A])(f: A => B): Task[B] = fa.map(f) def flatMap[A, B](fa: Task[A])(f: A => Task[B]): Task[B] = fa.flatMap(f) override def delay[A](thunk: => A): Task[A] = Task.delay(thunk) def suspend[A](thunk: => Task[A]): Task[A] = Task.suspend(thunk) } implicit val taskParallel: Parallel.Aux[Task, ParallelTask] = new Parallel[Task] { import Task.taskParallelApplicativeInstance type F[A] = ParallelTask[A] val monad: Monad[Task] = taskEffect val applicative: Applicative[ParallelTask] = Applicative[ParallelTask] val sequential: ParallelTask ~> Task = λ[ParallelTask ~> Task](Tag.unwrap(_)) val parallel: Task ~> ParallelTask = λ[Task ~> ParallelTask](Tag(_)) } private def functionToPartial[A, B](f: A => B): PartialFunction[A, B] = { case a => f(a) } private def forget[A](x: A): Unit = () }
Example 37
Source File: TaskArbitrary.scala From shims with Apache License 2.0 | 5 votes |
package shims.effect import scalaz.{Tag, \/} import scalaz.concurrent.Task import scalaz.concurrent.Task.ParallelTask import org.scalacheck._ object TaskArbitrary { implicit def arbitraryTask[A: Arbitrary: Cogen]: Arbitrary[Task[A]] = Arbitrary(Gen.delay(genTask[A])) implicit def arbitraryParallelTask[A: Arbitrary: Cogen]: Arbitrary[ParallelTask[A]] = Tag.subst(arbitraryTask[A]) // TODO implicit def cogenTask[A]: Cogen[Task[A]] = Cogen[Unit].contramap(_ => ()) def genTask[A: Arbitrary: Cogen]: Gen[Task[A]] = { Gen.frequency( 5 -> genPure[A], // 5 -> genApply[A], // NB: it is unsound to use this case together with TestContext 1 -> genFail[A], 5 -> genAsync[A], 5 -> genNestedAsync[A], 5 -> getMapOne[A], 5 -> getMapTwo[A], 10 -> genFlatMap[A] ) } def genPure[A: Arbitrary]: Gen[Task[A]] = Arbitrary.arbitrary[A].map(Task.now(_)) def genFail[A]: Gen[Task[A]] = Arbitrary.arbitrary[Throwable].map(Task.fail) def genAsync[A: Arbitrary]: Gen[Task[A]] = Arbitrary .arbitrary[(Either[Throwable, A] => Unit) => Unit] .map(f => Task.async { registered => f(e => registered(\/.fromEither(e))) }) def genNestedAsync[A: Arbitrary: Cogen]: Gen[Task[A]] = Arbitrary .arbitrary[(Either[Throwable, Task[A]] => Unit) => Unit] .map(f => Task .async { registered: ((Throwable \/ Task[A]) => Unit) => f(e => registered(\/.fromEither(e))) } .flatMap(x => x)) def genBindSuspend[A: Arbitrary: Cogen]: Gen[Task[A]] = Arbitrary.arbitrary[A].map(Task.apply(_).flatMap(Task.now)) def genFlatMap[A: Arbitrary: Cogen]: Gen[Task[A]] = for { ioa <- Arbitrary.arbitrary[Task[A]] f <- Arbitrary.arbitrary[A => Task[A]] } yield ioa.flatMap(f) def getMapOne[A: Arbitrary: Cogen]: Gen[Task[A]] = for { ioa <- Arbitrary.arbitrary[Task[A]] f <- Arbitrary.arbitrary[A => A] } yield ioa.map(f) def getMapTwo[A: Arbitrary: Cogen]: Gen[Task[A]] = for { ioa <- Arbitrary.arbitrary[Task[A]] f1 <- Arbitrary.arbitrary[A => A] f2 <- Arbitrary.arbitrary[A => A] } yield ioa.map(f1).map(f2) private[this] case object StaticException extends Exception { override def printStackTrace() = () // this is to stop the tests from spewing useless traces } // override built-in private[this] implicit def arbitraryThrowable: Arbitrary[Throwable] = Arbitrary(Gen.const(StaticException)) }
Example 38
Source File: TaskInstancesSpecs.scala From shims with Apache License 2.0 | 5 votes |
package shims.effect import cats.Eq import cats.laws.discipline.{ApplicativeTests, ParallelTests} import cats.effect.{Async, IO} import cats.effect.laws.discipline.{arbitrary, EffectTests}, arbitrary._ import cats.effect.laws.util.{TestContext, TestInstances}, TestInstances._ import cats.instances.either._ import cats.instances.int._ import cats.instances.tuple._ import cats.instances.unit._ import scalaz.Tag import scalaz.concurrent.Task import scalaz.concurrent.Task.ParallelTask import org.specs2.Specification import org.specs2.scalacheck.Parameters import org.specs2.specification.core.Fragments import org.typelevel.discipline.Laws import org.typelevel.discipline.specs2.Discipline import java.util.concurrent.RejectedExecutionException import scala.concurrent.ExecutionContext object TaskInstancesSpecs extends Specification with Discipline { import TaskArbitrary._ import Task.taskParallelApplicativeInstance def is = br ^ taskEff ^ br ^ taskPar ^ br ^ parTaskApp ^ br ^ asyncShiftTask def taskEff = checkAllAsync("Effect[Task]", implicit ctx => EffectTests[Task].effect[Int, Int, Int]) def taskPar = checkAllAsync("Parallel[Task]", implicit ctx => ParallelTests[Task].parallel[Int, Int]) def parTaskApp = checkAllAsync("Parallel[Task]", { implicit ctx => val tests = ApplicativeTests[ParallelTask] tests.applicative[Int, Int, Int] }) def asyncShiftTask = { implicit val context: TestContext = TestContext() val boom = new RejectedExecutionException("Boom") val rejectingEc = new ExecutionContext { def execute(runnable: Runnable): Unit = throw boom def reportFailure(cause: Throwable): Unit = () } "async.shift on rejecting execution context" ! { Eq[Task[Unit]].eqv(Async.shift[Task](rejectingEc), Task.fail(boom)) must beTrue } } def checkAllAsync(name: String, f: TestContext => Laws#RuleSet)(implicit p: Parameters) = { val context = TestContext() val ruleSet = f(context) Fragments.foreach(ruleSet.all.properties.toList) { case (id, prop) => id ! check(prop, p, defaultFreqMapPretty) ^ br } } implicit def taskEq[A: Eq](implicit ctx: TestContext): Eq[Task[A]] = Eq.by(ta => IO.async[A](k => ta.unsafePerformAsync(e => k(e.toEither)))) implicit def parallelTaskEq[A: Eq](implicit ctx: TestContext): Eq[ParallelTask[A]] = Tag.subst(taskEq[A]) }
Example 39
Source File: Res.scala From couchdb-scala with Apache License 2.0 | 5 votes |
package com.ibm.couchdb import org.http4s.Status import scalaz.concurrent.Task object Res { case class Ok(ok: Boolean = true) case class Error( error: String, reason: String, status: Status = Status.ExpectationFailed, request: String = "", requestBody: String = "") { def toTask[T]: Task[T] = Task.fail(CouchException(this)) } case class ServerInfo( couchdb: String, uuid: String, version: String, vendor: ServerVendor) case class ServerVendor(version: String, name: String) case class DbInfo( committed_update_seq: Int, compact_running: Boolean, data_size: Int, db_name: String, disk_format_version: Int, disk_size: Int, doc_count: Int, doc_del_count: Int, instance_start_time: String, purge_seq: Int, update_seq: Int) case class ViewIndexInfo( compact_running: Boolean, data_size: Int, disk_size: Int, language: String, purge_seq: Int, signature: String, update_seq: Int, updater_running: Boolean, waiting_clients: Int, waiting_commit: Boolean) case class DesignInfo(name: String, view_index: ViewIndexInfo) case class Uuids(uuids: Seq[String]) case class DocOk(ok: Boolean, id: String, rev: String) }
Example 40
Source File: Server.scala From couchdb-scala with Apache License 2.0 | 5 votes |
package com.ibm.couchdb.api import com.ibm.couchdb.Res import com.ibm.couchdb.core.Client import org.http4s.Status import scalaz.concurrent.Task class Server(client: Client) { def info: Task[Res.ServerInfo] = { client.get[Res.ServerInfo]("/", Status.Ok) } def mkUuid: Task[String] = { client.get[Res.Uuids]("/_uuids", Status.Ok).map(_.uuids(0)) } def mkUuids(count: Int): Task[Seq[String]] = { client.get[Res.Uuids](s"/_uuids?count=$count", Status.Ok).map(_.uuids) } }
Example 41
Source File: Databases.scala From couchdb-scala with Apache License 2.0 | 5 votes |
package com.ibm.couchdb.api import com.ibm.couchdb.Res import com.ibm.couchdb.core.Client import org.http4s.Status import scalaz.concurrent.Task class Databases(client: Client) { def get(name: String): Task[Res.DbInfo] = { client.get[Res.DbInfo](s"/$name", Status.Ok) } def getAll: Task[Seq[String]] = { client.get[Seq[String]]("/_all_dbs", Status.Ok) } def create(name: String): Task[Res.Ok] = { client.put[Res.Ok](s"/$name", Status.Created) } def delete(name: String): Task[Res.Ok] = { client.delete[Res.Ok](s"/$name", Status.Ok) } }
Example 42
Source File: GetDocumentQueryBuilder.scala From couchdb-scala with Apache License 2.0 | 5 votes |
package com.ibm.couchdb.api.builders import com.ibm.couchdb._ import com.ibm.couchdb.core.Client import org.http4s.Status import upickle.default.Aliases.R import upickle.default.write import scalaz.concurrent.Task case class GetDocumentQueryBuilder( client: Client, db: String, params: Map[String, String] = Map.empty[String, String]) { def attachments(attachments: Boolean = true): GetDocumentQueryBuilder = { set("attachments", attachments) } def attEncodingInfo(attEncodingInfo: Boolean = true): GetDocumentQueryBuilder = { set("att_encoding_info", attEncodingInfo) } def attsSince(attsSince: Seq[String]): GetDocumentQueryBuilder = { set("atts_since", write(attsSince)) } def conflicts(conflicts: Boolean = true): GetDocumentQueryBuilder = { set("conflicts", conflicts) } def deletedConflicts(deletedConflicts: Boolean = true): GetDocumentQueryBuilder = { set("deleted_conflicts", deletedConflicts) } def latest(latest: Boolean = true): GetDocumentQueryBuilder = { set("latest", latest) } def localSeq(localSeq: Boolean = true): GetDocumentQueryBuilder = { set("local_seq", localSeq) } def meta(meta: Boolean = true): GetDocumentQueryBuilder = { set("meta", meta) } def openRevs(openRevs: Seq[String]): GetDocumentQueryBuilder = { set("open_revs", write(openRevs)) } def openRevs(openRevs: String): GetDocumentQueryBuilder = { set("open_revs", openRevs) } def rev(rev: String): GetDocumentQueryBuilder = { set("rev", rev) } def revs(revs: Boolean = true): GetDocumentQueryBuilder = { set("revs", revs) } def revsInfo(revsInfo: Boolean = true): GetDocumentQueryBuilder = { set("revs_info", revsInfo) } private def set(key: String, value: String): GetDocumentQueryBuilder = { copy(params = params.updated(key, value)) } private def set(key: String, value: Any): GetDocumentQueryBuilder = { set(key, value.toString) } def query[D: R](id: String): Task[CouchDoc[D]] = { if (id.isEmpty) Res.Error("not_found", "No ID specified").toTask else client.get[CouchDoc[D]]( s"/$db/$id", Status.Ok, params.toSeq) } }
Example 43
Source File: SlowConsumer.scala From streams-tests with Apache License 2.0 | 5 votes |
package com.softwaremill.streams import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.scaladsl.Source import scala.concurrent.Await import scala.concurrent.duration._ import scalaz.concurrent.{Strategy, Task} import scalaz.stream.{Process, async, time} object AkkaSlowConsumer extends App { implicit val system = ActorSystem() implicit val mat = ActorMaterializer() try { val future = Source.tick(0.millis, 100.millis, 1) .conflate(identity)(_ + _) .runForeach { el => Thread.sleep(1000L) println(el) } Await.result(future, 1.hour) } finally system.terminate() } object ScalazSlowConsumer extends App { implicit val scheduler = Strategy.DefaultTimeoutScheduler val queue = async.boundedQueue[Int](10000) val enqueueProcess = time.awakeEvery(100.millis) .map(_ => 1) .to(queue.enqueue) val dequeueProcess = queue.dequeueAvailable .map(_.sum) .flatMap(el => Process.eval_(Task { Thread.sleep(1000L) println(el) })) (enqueueProcess merge dequeueProcess).run.run }
Example 44
Source File: ShowQueryBuilder.scala From couchdb-scala with Apache License 2.0 | 5 votes |
package com.ibm.couchdb.api.builders import com.ibm.couchdb.Res import com.ibm.couchdb.core.Client import org.http4s.Status import scalaz.concurrent.Task case class ShowQueryBuilder( client: Client, db: String, design: String, show: String, params: Map[String, String] = Map.empty[String, String]) { def details(details: Boolean = true): ShowQueryBuilder = { set("details", details) } def format(format: String): ShowQueryBuilder = { set("format", format) } def addParam(name: String, value: String): ShowQueryBuilder = { set(name, value) } private def set(key: String, value: String): ShowQueryBuilder = { copy(params = params.updated(key, value)) } private def set(key: String, value: Any): ShowQueryBuilder = { set(key, value.toString) } def query: Task[String] = { client.getRaw( s"/$db/_design/$design/_show/$show", Status.Ok, params.toSeq) } def query(id: String): Task[String] = { if (id.isEmpty) Res.Error("not_found", "Document ID must not be empty").toTask else client.getRaw( s"/$db/_design/$design/_show/$show/$id", Status.Ok, params.toSeq) } }
Example 45
Source File: QueryOps.scala From couchdb-scala with Apache License 2.0 | 5 votes |
package com.ibm.couchdb.api.builders import com.ibm.couchdb.core.Client import com.ibm.couchdb.Req import org.http4s.Status import upickle.default.Aliases.{R, W} import upickle.default._ import scalaz.concurrent.Task case class QueryOps(client: Client) { def query[Q: R]( url: String, params: Map[String, String]): Task[Q] = { client.get[Q](url, Status.Ok, params.toSeq) } def queryByIds[K: W, Q: R]( url: String, ids: Seq[K], params: Map[String, String]): Task[Q] = { postQuery[Req.DocKeys[K], Q](url, Req.DocKeys(ids), params) } def postQuery[B: W, Q: R]( url: String, body: B, params: Map[String, String]): Task[Q] = { client.post[B, Q](url, Status.Ok, body, params.toSeq) } }
Example 46
Source File: CouchDbSpecification.scala From couchdb-scala with Apache License 2.0 | 5 votes |
package com.ibm.couchdb.spec import com.ibm.couchdb._ import com.ibm.couchdb.api.Databases import com.ibm.couchdb.core.Client import com.ibm.couchdb.implicits.{TaskImplicits, UpickleImplicits} import org.specs2.matcher._ import org.specs2.mutable._ import org.specs2.scalaz.DisjunctionMatchers import org.specs2.specification.AllExpectations import scalaz._ import scalaz.concurrent.Task trait CouchDbSpecification extends Specification with Fixtures with AllExpectations with DisjunctionMatchers with MatcherMacros with TaskImplicits with UpickleImplicits { sequential val client = new Client( Config(SpecConfig.couchDbHost, SpecConfig.couchDbPort, https = false, None)) def await[T](future: Task[T]): Throwable \/ T = future.unsafePerformSyncAttempt def awaitRight[T](future: Task[T]): T = { val res = await(future) res must beRightDisjunction res.toOption.get } def awaitOk[T](future: Task[Res.Ok]): MatchResult[Any] = { await(future) must beRightDisjunction(Res.Ok(ok = true)) } def awaitDocOk[D](future: Task[Res.DocOk]): MatchResult[Any] = { checkDocOk(awaitRight(future)) } def awaitDocOk[D](future: Task[Res.DocOk], id: String): MatchResult[Any] = { checkDocOk(awaitRight(future), id) } def awaitLeft(future: Task[_]): Res.Error = { val res = await(future) res must beLeftDisjunction val -\/(e) = res e.asInstanceOf[CouchException[Res.Error]].content } def awaitError(future: Task[_], error: String): MatchResult[Any] = { val res = awaitLeft(future) res must beLike { case Res.Error(actualError, _, _, _, _) => actualError mustEqual error } } def beUuid: Matcher[String] = haveLength(32) def beRev: Matcher[String] = (_: String).length must beGreaterThan(32) def checkDocOk(doc: Res.DocOk): MatchResult[Any] = { (doc.ok mustEqual true) and (doc.id must not beEmpty) and (doc.rev must beRev) } def checkDocOk(doc: Res.DocOk, id: String): MatchResult[Any] = { checkDocOk(doc) and (doc.id mustEqual id) } def recreateDb(databases: Databases, name: String): \/[Throwable, Unit] = await { for { _ <- databases.delete(name).or(Task.now(Res.Ok())) _ <- databases.create(name) } yield () } }
Example 47
Source File: KVS.scala From make-your-programs-free with GNU General Public License v3.0 | 5 votes |
package free import scalaz._, Scalaz._ import scalaz.concurrent.Task sealed trait KVS[K, V, A] object KVS { final case class Get[K, V](k: K) extends KVS[K, V, Option[V]] final case class Put[K, V](k: K, v: V) extends KVS[K, V, Unit] final case class Delete[K, V](k: K) extends KVS[K, V, Unit] class Ops[S[_], K, V](implicit s0: KVS[K, V, ?] :<: S) { def get(key: K): Free[S, Option[V]] = lift[S, K,V, Option[V]](Get(key)) def put(key: K, value: V): Free[S, Unit] = lift[S, K, V, Unit](Put(key, value)) def delete(key: K): Free[S, Unit] = lift[S, K, V, Unit](Delete(key)) } object Ops { implicit def apply[S[_], K, V](implicit S: KVS[K, V, ?] :<: S): Ops[S, K, V] = new Ops[S, K, V] } private def lift[S[_], K, V, A](kvs: KVS[K, V, A])(implicit s0: KVS[K,V,?] :<: S) = Free.liftF(s0.inj(kvs)) } object KVSInterpreters { import KVS._ def fromMapState[K, V] = new (KVS[K, V, ?] ~> State[Map[K, V], ?]) { def apply[A](kvs: KVS[K, V, A]): State[Map[K, V], A] = kvs match { case Get(k) => State { case m => (m, m.get(k)) } case Put(k, v) => State { case m => (m + (k -> v), ()) } case Delete(k) => State { case m => (m - k, ()) } } } }
Example 48
Source File: UserApp2.scala From make-your-programs-free with GNU General Public License v3.0 | 5 votes |
package free import scalaz._, Scalaz._ import scalaz.concurrent.Task import free.EnrichNTOps._ object UserApp2 extends App { type Eff[A] = Coproduct[InOut, ManipulateAccount, A] def program[S[_]](implicit io: InOut.Ops[S], ma: ManipulateAccount.Ops[S] ): Free[S, Option[UserAccount]] = for { name <- io.ask("What is your login?") age <- io.ask("What is your age?") h <- ma.create(name, 25) user <- ma.fetch(h) } yield(user) val prog = program[Eff] def interpreter[S[_]](implicit s0: KVS[Long, UserAccount, ?] :<: S, s1: MonotonicSeq :<: S, s2: Task :<: S ): Eff ~> Free[S, ?] = { type G[A] = Free[S, A] val first: InOut ~> G = InOutInterpreter.interpreter[S] val second: ManipulateAccount ~> G = MAInterpreters.interpreter[S] first :+: second } type LowerEff0[A] = Coproduct[MonotonicSeq, KVS[Long, UserAccount, ?], A] type LowerEff[A] = Coproduct[Task, LowerEff0, A] def lowelevelInterpreter: Free[LowerEff, ?] ~> Task = ??? implicit val kvs: KVS[Long, UserAccount, ?] :<: LowerEff = implicitly[KVS[Long, UserAccount, ?] :<: LowerEff] implicit val ms: MonotonicSeq :<: LowerEff = implicitly[MonotonicSeq :<: LowerEff] val theInt: Eff ~> Task = interpreter[LowerEff] andThen lowelevelInterpreter }
Example 49
Source File: Logging.scala From make-your-programs-free with GNU General Public License v3.0 | 5 votes |
package free import scalaz._, Scalaz._ import scalaz.concurrent.Task import org.slf4j.LoggerFactory sealed trait Logging[A] case class Info(line: String) extends Logging[Unit] case class Warn(line: String) extends Logging[Unit] case class Error(line: String) extends Logging[Unit] case class Debug(line: String) extends Logging[Unit] object Logging { class Ops[S[_]](implicit s0: Logging :<: S) { def info(line: String): Free[S, Unit] = Free.liftF(s0.inj(Info(line))) def warn(line: String): Free[S, Unit] = Free.liftF(s0.inj(Warn(line))) def error(line: String): Free[S, Unit] = Free.liftF(s0.inj(Error(line))) def debug(line: String): Free[S, Unit] = Free.liftF(s0.inj(Debug(line))) } object Ops { implicit def apply[S[_]](implicit S: Logging :<: S): Ops[S] = new Ops[S] } } object Log4JInterpreter extends (Logging ~> Task) { def apply[A](inout: Logging[A]): Task[A] = inout match { case Info(line) => Task.delay { LoggerFactory.getLogger(this.getClass).info(line) } case Error(line) => Task.delay { LoggerFactory.getLogger(this.getClass).error(line) } case Warn(line) => Task.delay { LoggerFactory.getLogger(this.getClass).warn(line) } case Debug(line) => Task.delay { LoggerFactory.getLogger(this.getClass).debug(line) } } } object RunLogging extends App { implicit val ops = new Logging.Ops[Logging]() val program = for { _ <- ops.info("starting application!") _ <- ops.debug("omg, app is running!") } yield() val task: Task[Unit] = program.foldMap(Log4JInterpreter) task.unsafePerformSync }
Example 50
Source File: MonotonicSeq.scala From make-your-programs-free with GNU General Public License v3.0 | 5 votes |
package free import scalaz._, Scalaz._ import scalaz.concurrent.Task sealed trait MonotonicSeq[A] object MonotonicSeq { case class Next() extends MonotonicSeq[Long] class Ops[S[_]](implicit s0: MonotonicSeq :<: S) { def next: Free[S, Long] = Free.liftF(s0.inj(Next())) } object Ops { implicit def apply[S[_]](implicit S: MonotonicSeq :<: S): Ops[S] = new Ops[S] } } object MSInterpreters { import MonotonicSeq._ def fromMapState[K, V] = new (MonotonicSeq ~> Id) { def apply[A](ms: MonotonicSeq[A]): Id[A] = ms match { case Next() => System.nanoTime } } }
Example 51
Source File: InOut.scala From make-your-programs-free with GNU General Public License v3.0 | 5 votes |
package free import scalaz._, Scalaz._ import scalaz.concurrent.Task sealed trait InOut[A] case class PrintLine(line: String) extends InOut[Unit] case object GetLine extends InOut[String] object InOut { class Ops[S[_]](implicit s0: InOut :<: S) { def printLine(line: String): Free[S, Unit] = Free.liftF(s0.inj(PrintLine(line))) def getLine(): Free[S, String] = Free.liftF(s0.inj(GetLine)) def ask(question: String): Free[S, String] = for { _ <- printLine(question) answer <- getLine() } yield answer } object Ops { implicit def apply[S[_]](implicit S: InOut :<: S): Ops[S] = new Ops[S] } } object ConsoleInterpreter extends (InOut ~> Task) { def apply[A](inout: InOut[A]): Task[A] = inout match { case PrintLine(line) => Task.delay { println(line) } case GetLine => Task.delay { scala.io.StdIn.readLine() } } } object InOutInterpreter { def injectFT[F[_], S[_]](implicit S: F :<: S): F ~> Free[S, ?] = liftFT[S] compose injectNT[F, S] def liftFT[S[_]]: S ~> Free[S, ?] = new (S ~> Free[S, ?]) { def apply[A](s: S[A]) = Free.liftF(s) } def injectNT[F[_], G[_]](implicit I: F :<: G): F ~> G = new (F ~> G) { def apply[A](fa: F[A]) = I inj fa } def interpreter[S[_]](implicit s0: Task :<: S) = new (InOut ~> Free[S, ?]) { def apply[A](inout: InOut[A]): Free[S, A] = inout match { case PrintLine(line) => injectFT.apply(Task.delay { println(line) }) case GetLine => injectFT.apply(Task.delay { scala.io.StdIn.readLine() }) } } } object OurFirstProgram { def program[S[_]](implicit io: InOut.Ops[InOut]): Free[InOut, Unit] = for { name <- io.ask("What is your name") _ <- io.printLine(s"Nice to meet you $name") } yield () } object RunInOut extends App { implicit val ops = new InOut.Ops[InOut] val task: Task[Unit] = OurFirstProgram.program.foldMap(ConsoleInterpreter) task.unsafePerformSync }
Example 52
Source File: UserApp1.scala From make-your-programs-free with GNU General Public License v3.0 | 5 votes |
package free import scalaz._, Scalaz._ import scalaz.concurrent.Task import free.EnrichNTOps._ import free.MAInterpreters._ object UserApp1 extends App { type Eff[A] = Coproduct[InOut, ManipulateAccount, A] def program[S[_]](implicit io: InOut.Ops[S], ma: ManipulateAccount.Ops[S] ): Free[S, Option[UserAccount]] = for { name <- io.ask("What is your login?") age <- io.ask("What is your age?") h <- ma.create(name, 25) user <- ma.fetch(h) } yield(user) object LiftedConsoleInterpreter extends (InOut ~> StorageState) { def apply[A](inout: InOut[A]): StorageState[A] = StateT((s: InnerS) => ConsoleInterpreter.apply(inout).map(a => (s, a))) } val prog = program[Eff] val interpreter: Eff ~> StorageState = LiftedConsoleInterpreter :+: MAInterpreters.allInOne val result = prog.foldMap(interpreter).eval((0, Map.empty)).unsafePerformSync println(result) }
Example 53
Source File: LogAndConsole.scala From make-your-programs-free with GNU General Public License v3.0 | 5 votes |
package free import scalaz._, Scalaz._ import scalaz.concurrent.Task import free.EnrichNTOps._ object LogAndConsole extends App { type Eff[A] = Coproduct[InOut, Logging, A] def program[S[_]](implicit io: InOut.Ops[S], log: Logging.Ops[S] ): Free[S, String] = for { _ <- log.info("Program started") name <- io.ask("What is your name?") _ <- log.debug(s"The name was $name") } yield(name) val prog = program[Eff] val interpreter: Eff ~> Task = ConsoleInterpreter :+: Log4JInterpreter prog.foldMap(interpreter).unsafePerformSync }
Example 54
Source File: Release.scala From ionroller with MIT License | 5 votes |
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, File} import java.nio.file.{Files, Paths} import com.amazonaws.services.s3.model._ import com.amazonaws.services.s3.transfer.Transfer.TransferState import com.amazonaws.services.s3.transfer.TransferManager import com.amazonaws.util.IOUtils import sbt._ import scalaz.concurrent.Task object Release { lazy val releaseCli = taskKey[Unit]("Releases ION-Roller CLI") def release(ver: String, zip: File, install: File) = { val files = Seq( (install.getName, replaceVersionAndReadBytes(ver, install), "text/plain"), (zip.getName, readBytes(zip), "application/zip")) val tx = new TransferManager val tasks = for { f <- files } yield uploadFile(tx, f._1, f._2, f._3) val t = for { results <- Task.gatherUnordered(tasks) finalResult = if (results.forall(_ == TransferState.Completed)) TransferState.Completed else TransferState.Failed printTask <- Task.delay(println(finalResult)) } yield printTask t.run } def uploadFile(tx: TransferManager, name: String, getBytes: Task[Array[Byte]], contentType: String): Task[TransferState] = { for { bytes <- getBytes meta <- metadata(bytes, contentType) transferState <- upload(tx, bytes, name, meta) } yield transferState } def metadata(bytes: Array[Byte], contentType: String): Task[ObjectMetadata] = { Task.delay({ val out = new ByteArrayOutputStream out.write(bytes) val metadata = new ObjectMetadata metadata.setContentType(contentType) val contentBytes = IOUtils.toByteArray(new ByteArrayInputStream(out.toByteArray)).length.toLong // we need to call new ByteArrayInputStream again, as checking the length reads the stream metadata.setContentLength(contentBytes) metadata }) } def upload(tx: TransferManager, in: Array[Byte], name: String, meta: ObjectMetadata): Task[TransferState] = { Task.delay({ println(s"Uploading $name...") val upload = tx.upload( new PutObjectRequest("ionroller-cli", name, new ByteArrayInputStream(in), meta) .withCannedAcl(CannedAccessControlList.PublicRead) ) while (!upload.isDone) { Thread.sleep(2000) println(upload.getProgress.getPercentTransferred.toInt + "%") } upload.getState }) } def replaceVersionAndReadBytes(ver: String, file: File): Task[Array[Byte]] = { Task.delay({ scala.io.Source.fromFile(file).getLines() .map(in => if (in startsWith "VERSION=") s"VERSION=$ver" else in) .mkString("\n") .getBytes .toSeq .toArray }) } def readBytes(file: File): Task[Array[Byte]] = Task.delay({ Files.readAllBytes(Paths.get(file.getAbsolutePath)) }) }
Example 55
Source File: ProgressMessage.scala From ionroller with MIT License | 5 votes |
package ionroller.cmd import org.fusesource.jansi.Ansi import scala.concurrent.duration.FiniteDuration import scalaz._ import scalaz.Scalaz._ import scalaz.concurrent.Task import scalaz.stream.Cause.End import scalaz.stream.Process.Halt import scalaz.stream.ReceiveY._ import scalaz.stream._ object ProgressMessage { implicit val timeout = scalaz.concurrent.Strategy.DefaultTimeoutScheduler def eitherHaltBoth[I, I2]: Wye[I, I2, I \/ I2] = wye.receiveBoth { case ReceiveL(i) => Process.emit(i.left) ++ eitherHaltBoth case ReceiveR(i) => Process.emit(i.right) ++ eitherHaltBoth case HaltL(End) => Halt(End) case HaltR(End) => Halt(End) case h @ HaltOne(rsn) => Halt(rsn) } def processingMessage(i: Int) = { val frames = Array( " :O ", " \u00D6 ", " O: ", " O\u0324 ", " :O ", " \u00D6 ", " O: ", " O\u0324 ", " :O ", " \u00D6 ", " O:", " \u00D6 ", " :O ", " O\u0324 ", " O: ", " \u00D6 ", " :O ", " O\u0324 ", " O: ", " \u00D6 " ) Ansi.ansi().eraseLine().a(frames(i)).cursorUp(1).toString } def progressMessage(p: Process[Task, String], timeoutDuration: FiniteDuration) = { p.wye(time.awakeEvery(timeoutDuration).zipWithIndex.map(_._2 % 20))(ProgressMessage.eitherHaltBoth).pipe(process1.sliding(2)).flatMap { w => val m: Seq[String] = if (w.size == 1) w(0).fold(l => l, r => processingMessage(r)).some.toSeq else if (w.forall(_.isRight)) { // Two timeout messages in a row! w.headOption.map(_.fold(l => ???, r => processingMessage(r))).toSeq } else { // No timeout... w.filter(_.isLeft).map(_.fold(l => l, r => ???)) } Process.emitAll(m) } .pipe(process1.distinctConsecutive[String]) } }
Example 56
Source File: package.scala From ionroller with MIT License | 5 votes |
package ionroller import com.typesafe.scalalogging.Logger import play.api.libs.json.Json import scalaz.Scalaz._ import scalaz._ import scalaz.concurrent.Task import scalaz.stream.Cause.EarlyCause import scalaz.stream.Process.{Await, Emit, Halt, Step} import scalaz.stream._ trait Stepper[F[_], A] { def next: OptionT[F, Seq[A]] def close: F[Unit] } package object stream { import JsonUtil.Implicits._ implicit class RatelimitedProcessSyntax[O](p: Process[Task, O]) { def logDebug(logger: Logger, f: O => String) = { val logStringProcess: Sink[Task, String] = Process.constant({ i: String => Task.delay(logger.debug(i)) }) p.observe(logStringProcess.contramap(f)) } def logInfo(logger: Logger, f: O => String) = { val logStringProcess: Sink[Task, String] = Process.constant({ i: String => Task.delay(logger.info(i)) }) p.observe(logStringProcess.contramap(f)) } def logDebugJson(logger: Logger) = logDebug(logger, i => Json.prettyPrint(i.toJsonValue)) def logThrowable[T](logger: Logger, msg: String)(implicit ev: O <:< \/[Throwable, T]): Process[Task, T] = { val logThrowableProcess = Process.constant({ i: \/[Throwable, T] => i match { case -\/(t) => Task.delay(logger.error(msg, t)) case _ => Task.now(()) } }) p.map(ev.apply).observe(logThrowableProcess).collect { case \/-(v) => v } } } def emitChanges[A, B](differ: (A, A) => Seq[B])(implicit equal: Equal[A]): Process1[A, B] = { def emitChangesProcess(items: Vector[A]) = { if (items.size <= 1) Process.empty else Process.emitAll(differ(items(0), items(1))) } process1.distinctConsecutive[A].sliding(2).flatMap(emitChangesProcess) } def step[A](p: Process[Task, A]): Stepper[Task, A] = new Stepper[Task, A] { var state = p def next: OptionT[Task, Seq[A]] = state.step match { case Halt(_) => OptionT.none case Step(Emit(as: Seq[A]), cont) => state = cont.continue OptionT(as.point[Task] map some) case Step(Await(req: Task[_] @unchecked, rcv), cont) => for { tail <- (req.attempt map { r => rcv(EarlyCause fromTaskResult r).run +: cont }).liftM[OptionT] _ = state = tail back <- next } yield back } def close = Task.suspend { Task.delay(state = state.kill) >> state.run } } }