Merge branch 'timestamp-restorer' into 'master'

Timestamp restorer

See merge request mythic-insight/legendary!52
This commit is contained in:
Robert Prehn 2021-04-24 17:10:19 +00:00
commit 0b0b17a840
4 changed files with 18 additions and 11 deletions

View file

@ -38,7 +38,6 @@ build_image_for_commit:
services:
- name: docker:20.10-dind
script:
- script/ci/restore-timestamps
- docker login "https://${CI_REGISTRY}" -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
- docker pull $CI_REGISTRY_IMAGE:latest || true
# This enables fast parallel builds
@ -49,7 +48,6 @@ build_image_for_commit:
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
# Pull out the built _build/prod directory so we can cache it!
- docker cp `docker create $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA`:/root/app/_build/prod _build
- script/ci/restore-timestamps
# If tests pass, tag the commit and update package versions
deploy_to_tags:

View file

@ -28,6 +28,7 @@ ADD ./apps/content/mix.exs /root/app/apps/content/
ADD ./apps/core/mix.exs /root/app/apps/core/
ADD ./_build/ /root/app/_build/
ADD ./deps/ /root/app/deps/
RUN script/restore-timestamps
RUN mix deps.get
RUN mix deps.compile
@ -55,4 +56,6 @@ RUN MAKE=cmake mix compile
COPY --from=1 /root/app/apps/app/priv/static/ /root/app/apps/app/priv/static
RUN mix phx.digest
RUN script/restore-timestamps
CMD ["mix", "phx.server"]

View file

@ -6,7 +6,7 @@ set -e
mix local.hex --force
mix local.rebar --force
script/ci/restore-timestamps
script/restore-timestamps
mix deps.get
mix ecto.create
@ -14,4 +14,4 @@ mix ecto.migrate
mix test
script/ci/restore-timestamps
script/restore-timestamps

View file

@ -4,13 +4,13 @@ defmodule TimestampRestorer do
@environment System.get_env("MIX_ENV", "dev")
@db_path "_build/#{@environment}/timestamp-database"
def sha_all do
def sha_all(opts \\ []) do
timestamp_database = load_timestamp_database()
"**/*.{ex,exs,beam}"
|> Path.wildcard()
|> Enum.reduce(%{}, fn filename, acc ->
{sha, timestamp} = process(filename, timestamp_database)
{sha, timestamp} = process(filename, timestamp_database, opts)
Map.put(acc, sha, timestamp)
end)
|> write_timestamp_database()
@ -40,23 +40,27 @@ defmodule TimestampRestorer do
|> (& File.write!(@db_path, &1)).()
end
defp process(filename, timestamp_database) do
defp process(filename, timestamp_database, opts) do
{verbose, _opts} = Keyword.pop(opts, :verbose, false)
sha = sha(filename)
{:ok, %{mtime: new_timestamp}} = File.lstat(filename, time: :posix)
case Map.get(timestamp_database, sha) do
nil ->
:logger.debug("[NEW SHA ] #{filename}: #{new_timestamp}")
log("[NEW SHA ] #{filename}: #{new_timestamp}", verbose)
timestamp when timestamp < new_timestamp ->
:logger.debug("[RESTORED ] #{filename}: #{timestamp}")
log("[RESTORED ] #{filename}: #{timestamp}", verbose)
File.touch(filename, timestamp)
timestamp when timestamp >= new_timestamp ->
:logger.debug("[UNCHANGED] #{filename}: #{timestamp}")
log("[UNCHANGED] #{filename}: #{timestamp}", verbose)
end
{sha, new_timestamp}
end
defp log(_message, false), do: :ok
defp log(message, true), do: :logger.debug(message)
defp sha(filename) do
hash_ref = :crypto.hash_init(:sha)
@ -71,6 +75,8 @@ defmodule TimestampRestorer do
end
end
{time, _result} = :timer.tc(TimestampRestorer, :sha_all, [])
{opts, _args, _errors} = OptionParser.parse(System.argv(), switches: [verbose: :boolean])
{time, _result} = :timer.tc(TimestampRestorer, :sha_all, [opts])
:logger.info("Restored timestamps in #{time / 1_000_000}s")