Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document it, Use what I did for EventSource 1.0.1, EventSource/eventsource#74 Use new name, Publish :)
- Loading branch information
1 parent
caee430
commit 942823f
Showing
13 changed files
with
206 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules/ | ||
target/ | ||
lib/main.js | ||
*.log | ||
*.log | ||
*.tsv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ node_modules | |
.idea | ||
target | ||
*.log | ||
target | ||
target | ||
*.tsv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import scala.scalajs.js | ||
import js._ | ||
import io.scalajs.nodejs._ | ||
|
||
object AfCloneLogsApp extends JSApp { | ||
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue | ||
|
||
private val DefaultStartTimeEnv = "DEFAULT_START_TIME" | ||
|
||
private val AuthorizationEnv = "AUTHORIZATION" | ||
|
||
private val defaultStartTime: String = | ||
process.env.get(DefaultStartTimeEnv).getOrElse("2016-01-02T03:04:05Z") | ||
|
||
private val authorization: Option[String] = | ||
process.env.get(AuthorizationEnv) | ||
|
||
def main(): Unit = { | ||
|
||
val atFile = { | ||
val targetFile = process.argv.toList.drop(2).lastOption.getOrElse { | ||
console.error("Target file not specified.") | ||
process.exit(1) | ||
null | ||
} | ||
console.info(s"Reading file ${targetFile}...") | ||
AtEventFile(targetFile) | ||
} | ||
|
||
for { | ||
optionalLastTime <- atFile.getLastLogTime | ||
} { | ||
val fromTime = optionalLastTime.map(_.timeString).getOrElse(defaultStartTime) | ||
var remainingIgnore = optionalLastTime.map(_.recordsAtTime).getOrElse(0) | ||
|
||
console.info(s"Resuming from time ${fromTime}, with ${remainingIgnore} lines at this time") | ||
val headers = Dictionary("Last-Event-Id" -> fromTime) | ||
authorization.foreach { authorization => | ||
headers += "Authorization" -> authorization | ||
} | ||
val eventSourceInitDict = | ||
Dictionary("headers" -> headers) | ||
val es = new EventSource("https://actionfps.com/logs", eventSourceInitDict) | ||
es.on("open", { _: js.Dynamic => | ||
console.log("EventSource connection opened.") | ||
}) | ||
es.on("error", { e: js.Dynamic => | ||
console.log("EventSource error: ", JSON.stringify(e)) | ||
}) | ||
es.on( | ||
"log", { e: js.Dynamic => | ||
val line = e.data.toString | ||
if (remainingIgnore == 0) { | ||
atFile.appendLine(line) | ||
} else { | ||
remainingIgnore = remainingIgnore - 1 | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import io.scalajs.nodejs.fs | ||
|
||
import scala.concurrent.Future | ||
|
||
/** | ||
* Created by william on 10/5/17. | ||
*/ | ||
case class AtEventFile(targetFile: String) { | ||
|
||
def appendLine(line: String): Unit = { | ||
fs.Fs.appendFileSync(targetFile, line + "\n") | ||
} | ||
|
||
def getLastLogTime: Future[Option[LastLogTime]] = LastLogTime.fromFile(targetFile) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import io.scalajs.nodejs.events.IEventEmitter | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.Dictionary | ||
import scala.scalajs.js.annotation.JSImport | ||
|
||
/** | ||
* Created by william on 10/5/17. | ||
* | ||
* Mapping to <a href="https://github.com/EventSource/eventsource/">EventSource library</a> | ||
*/ | ||
@js.native | ||
@JSImport("eventsource", JSImport.Namespace) | ||
class EventSource() extends IEventEmitter { | ||
def this(url: String) = this() | ||
def this(url: String, eventSourceInitDict: Dictionary[_]) = this() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import io.scalajs.nodejs | ||
import io.scalajs.nodejs.readline.ReadlineOptions | ||
|
||
import scala.concurrent.{Future, Promise} | ||
|
||
/** | ||
* Read the log to determine the last timestamp and number of times it appears. | ||
* | ||
* The stream may be terminated at any point. If we didn't do this, | ||
* we'd potentially have duplicate messages, and the chance that these | ||
* duplicates happen is quite high. | ||
*/ | ||
case class LastLogTime(timeString: String, recordsAtTime: Int) { | ||
def accept(newTimeString: String): LastLogTime = { | ||
if (timeString == newTimeString) { | ||
copy(recordsAtTime = recordsAtTime + 1) | ||
} else LastLogTime(timeString = newTimeString, recordsAtTime = 1) | ||
} | ||
} | ||
|
||
object LastLogTime { | ||
def fromFile(file: String): Future[Option[LastLogTime]] = { | ||
val readInterface = nodejs.readline.Readline.createInterface( | ||
new ReadlineOptions( | ||
input = nodejs.fs.Fs.createReadStream(file) | ||
)) | ||
val promise = Promise[Option[LastLogTime]] | ||
var haveLogTime = Option.empty[LastLogTime] | ||
readInterface.onLine { line => | ||
val timeString = line.split("\t").head | ||
haveLogTime = haveLogTime.map(_.accept(timeString)).orElse(Some(LastLogTime(timeString, 1))) | ||
} | ||
readInterface.onClose(() => { | ||
promise.success(haveLogTime) | ||
}) | ||
promise.future | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.