Skip to content

Commit

Permalink
Fix for /games + small enhancements (#29)
Browse files Browse the repository at this point in the history
* Revert mosquito to 1.0.0.rc3 + filter /games per strategy + script helper

* Embed worker within app is needed
  • Loading branch information
fdocr authored Sep 30, 2023
1 parent e18c112 commit 7466903
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 56 deletions.
6 changes: 4 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
EMBED_WORKER="true"
DATABASE_URL="postgres://localhost:5432/crystal_snake"
REDIS_URL="redis://localhost:6379/0"
DISABLE_DB_PERSIST="true"
LOG_LEVEL="DEBUG"
LOG_LEVEL="INFO"
# Optionally uncomment:
# DISABLE_DB_PERSIST="true"
6 changes: 1 addition & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ RUN shards install -v
# Build a binary
COPY . /opt/
RUN crystal build --static --release ./src/app.cr
RUN crystal build --static --release ./src/worker.cr
RUN crystal build --static --release ./src/bundle.cr
# ===============
# Result image with one layer
FROM alpine:latest
WORKDIR /
COPY --from=builder /opt/app .
COPY --from=builder /opt/worker .
COPY --from=builder /opt/bundle .
ENTRYPOINT ["./bundle"]
ENTRYPOINT ["./app"]
4 changes: 2 additions & 2 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ shards:

mosquito:
git: https://github.com/mosquito-cr/mosquito.git
version: 1.0.0
version: 1.0.0.rc3

mpsc:
git: https://github.com/jgaskins/mpsc.git
Expand Down Expand Up @@ -74,7 +74,7 @@ shards:

sentry:
git: https://github.com/samueleaton/sentry.git
version: 0.5.0
version: 0.3.2+git.commit.e448ce83486f99ef016c311e10ec0cac805cded3

spec-kemal:
git: https://github.com/kemalcr/spec-kemal.git
Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ dependencies:
version: ~> 0.5.0
mosquito:
github: mosquito-cr/mosquito
version: ~> 1.0.0
version: 1.0.0.rc3

development_dependencies:
sam:
github: imdrasil/sam.cr
version: ~> 0.4.2
sentry:
github: samueleaton/sentry
version: ~> 0.5.0
branch: master
spec-kemal:
github: kemalcr/spec-kemal
version: ~> 1.0.0
Expand Down
13 changes: 8 additions & 5 deletions src/app.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ before_all do |env|
env.set("strategy", strategy)
end

get "/strategy_not_found" do |env|
halt env, status_code: 404, response: not_found_response
get "/" do |env|
env.redirect "https://github.com/fdocr/CrystalSnake"
end

# Battlesnake API Endpoints
Expand Down Expand Up @@ -80,11 +80,14 @@ post "/:strategy/end" do |env|
end

# DB-persisted games
get "/games" do |env|
get "/games/:strategy" do |env|
strategy = env.params.url["strategy"]
offset = (env.params.query["page"]? || 0).to_i * 50
count = Turn.where { _path == "/end" }.count
end_turns = Turn.where { _path == "/end" }.limit(50).offset(offset).order(id: :desc)
count = Turn.where { _path == "/#{strategy}/end" }.count
end_turns = Turn.where { _path == "/#{strategy}/end" }.limit(50).offset(offset).order(id: :desc)
render "src/views/games.ecr", "src/views/layout.ecr"
end

spawn { Mosquito::Runner.start } unless ENV["EMBED_WORKER"]?.nil?

Kemal.run
16 changes: 0 additions & 16 deletions src/bundle.cr

This file was deleted.

32 changes: 8 additions & 24 deletions src/sam.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,14 @@ require "../db/migrations/*"
load_dependencies "jennifer"

task "dev" do
sentry = [
Sentry::ProcessRunner.new(
display_name: "App",
build_command: "crystal build ./src/app.cr",
run_command: "./app",
run_args: ["-p", "8080"],
files: [ "./src/**/*" ]
),
Sentry::ProcessRunner.new(
display_name: "Worker",
build_command: "crystal build ./src/worker.cr",
run_command: "./worker",
files: [ "./src/jobs/*.cr" ]
)
]

# Execute runners in separate threads
sentry.each { |s| spawn { s.run } }

begin
sleep
rescue
sentry.each(&.kill)
end
sentry = Sentry::ProcessRunner.new(
display_name: "App",
build_command: "crystal build ./src/app.cr",
run_command: "./app",
run_args: ["-p", "8080"],
files: [ "./src/**/*", "./config/*.cr" ]
)
sentry.run
end

task "test" do
Expand Down
12 changes: 12 additions & 0 deletions src/script.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "./battle_snake/**"
require "./strategy/**"

require "dotenv"
dev_env = Kemal.config.env == "development"
Dotenv.load if File.exists?(".env") && dev_env

require "../config/**"
require "./models/**"
require "./jobs/**"

# Script goes here

0 comments on commit 7466903

Please sign in to comment.