Replace Twenty::Path

This commit is contained in:
0x1eef 2024-04-26 23:55:33 -03:00
parent e7f910be5f
commit 3105be205e
4 changed files with 49 additions and 51 deletions

View file

@ -3,7 +3,7 @@
class Twenty::Command::Down < Twenty::Command class Twenty::Command::Down < Twenty::Command
set_banner usage: "twenty down [OPTIONS]", set_banner usage: "twenty down [OPTIONS]",
description: "Stop the twenty web server" description: "Stop the twenty web server"
include Twenty::Path
prepend Twenty::Command::SQLiteMixin prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin prepend Twenty::Command::RescueMixin
@ -15,19 +15,18 @@ class Twenty::Command::Down < Twenty::Command
private private
def run_command(options) def run_command(options)
if File.readable?(pidfile) if File.readable?(pid)
Process.kill("SIGINT", Integer(pid)) pid = Integer(File.binread(pid).gsub(/[^\d]/, ""))
Process.kill("SIGINT", pid)
else else
warn "PID file is not readable." warn "[x] #{pid} is not readable"
end end
rescue Errno::ESRCH rescue Errno::ESRCH
warn "No such process." warn "[x] Process not found"
FileUtils.rm(pidfile) rm(pid)
end end
def pid def pid
@pid ||= File Twenty.pid
.binread(pidfile)
.gsub(/[^\d]/, "")
end end
end end

View file

@ -17,7 +17,6 @@ class Twenty::Command::Up < Twenty::Command
"Listen on a UNIX socket" "Listen on a UNIX socket"
include CommonOptionMixin include CommonOptionMixin
include Twenty::Path
prepend Twenty::Command::MigrationMixin prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin prepend Twenty::Command::RescueMixin
@ -30,12 +29,16 @@ class Twenty::Command::Up < Twenty::Command
private private
def run_command(options) def run_command(options)
File.binwrite(pidfile, Process.pid.to_s) File.binwrite(pid, Process.pid.to_s)
thr = Twenty::Rack.server(options).start thr = Twenty::Rack.server(options).start
thr.join thr.join
rescue Interrupt rescue Interrupt
thr.kill thr.kill
ensure ensure
FileUtils.rm(pidfile) FileUtils.rm(pid)
end
def pid
Twenty.pid
end end
end end

View file

@ -3,14 +3,42 @@
module Twenty module Twenty
require "fileutils" require "fileutils"
require "sequel" require "sequel"
require_relative "server/path"
## extend Module.new {
# @return [String] extend FileUtils
# Returns the default path for the SQLite database require "tmpdir"
def self.default_database
@default_database ||= File.join(Path.datadir, "database.sqlite") ##
end # @return [String]
# Returns the path for the default SQLite database
def default_database
@default_database ||= File.join(datadir, "database.sqlite")
end
##
# @return [String]
# Returns the directory where twenty stores data
def datadir
File.join(Dir.home, ".local", "share", "twenty")
end
##
# @return [String]
# Returns the directory where twenty stores temporary data
def tmpdir
File.join(Dir.tmpdir, "twenty")
end
##
# @return [String]
# Returns the path to a PID file
def pid
File.join(tmpdir, "server.pid")
end
mkdir_p(datadir)
mkdir_p(tmpdir)
}
## ##
# Establishes a database connection # Establishes a database connection
@ -34,7 +62,6 @@ module Twenty
@connection @connection
end end
FileUtils.touch(default_database)
require_relative "server/graphql" require_relative "server/graphql"
require_relative "server/rack" require_relative "server/rack"
end end

View file

@ -1,31 +0,0 @@
# frozen_string_literal: true
module Twenty::Path
require "tmpdir"
extend self
##
# @return [String]
# Returns the directory where twenty stores persistent data.
def datadir
File.join(Dir.home, ".local", "share", "twenty")
end
##
# @return [String]
# Returns the directory where twenty stores temporary data.
def tmpdir
File.join(Dir.tmpdir, "twenty")
end
##
# @return [String]
# Returns the file where twenty can write the PID of
# a web server running in the background.
def pidfile
File.join(tmpdir, "server.pid")
end
FileUtils.mkdir_p(datadir)
FileUtils.mkdir_p(tmpdir)
end