worker.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Installation } from "@/installation"
  2. import { Server } from "@/server/server"
  3. import { Log } from "@/util/log"
  4. import { Instance } from "@/project/instance"
  5. import { Rpc } from "@/util/rpc"
  6. await Log.init({
  7. print: process.argv.includes("--print-logs"),
  8. dev: Installation.isLocal(),
  9. level: (() => {
  10. if (Installation.isLocal()) return "DEBUG"
  11. return "INFO"
  12. })(),
  13. })
  14. process.on("unhandledRejection", (e) => {
  15. Log.Default.error("rejection", {
  16. e: e instanceof Error ? e.message : e,
  17. })
  18. })
  19. process.on("uncaughtException", (e) => {
  20. Log.Default.error("exception", {
  21. e: e instanceof Error ? e.message : e,
  22. })
  23. })
  24. let server: Bun.Server<undefined>
  25. export const rpc = {
  26. async server(input: { port: number; hostname: string }) {
  27. if (server) await server.stop(true)
  28. try {
  29. server = Server.listen(input)
  30. return {
  31. url: server.url.toString(),
  32. }
  33. } catch (e) {
  34. console.error(e)
  35. throw e
  36. }
  37. },
  38. async shutdown() {
  39. await Instance.disposeAll()
  40. await server.stop(true)
  41. },
  42. }
  43. Rpc.listen(rpc)