
























Hello! RC took a few weeks ago, and it’s back now! I’m batching these posts a few days at a time, so here’s what happened on Tuesday.
On Monday I set up a dev environment with Docker Compose (blog post), but I wasn’t sure if it would work well in production.
My “production” in this case is a single DigitalOcean droplet, and I thought that setting up a new server to use docker-compose instead of what I already had would be a lot of work.
It turned out to be easier than I thought and it only took a couple of hours and I felt like my new setup was WAY more robust afterwards.
I think all of my steps were:
net-tools so that I could use netstatgolang so that I could build go programs outside the container (though I’ll probably switch to doing this in a container soon, it’ll be better)git clone https://github.com/my/repo --bare)docker-compose-prod.yml (you can see it here) with a slightly different configuration for productiongit remote add railsbox root@1.2.3.4:/my-repo.gitgit push railboxpg_dump and restore it in the new databaseThis is pretty far from “just click one thing and you’re done” but I feel like if I had to do it again because I lost my production server it wouldn’t be too bad. It’s definitely much better than “just create a lot of systemd files by hand” which is what I was doing before.
All of this docker-compose stuff wasn’t actually my goal for the day, though!
My real goal was: I have this sort of puzzle game, and I was storing the
definitions for my puzzles (basically the title and a cloud-init.yaml file)
in the database.
This really wasn’t working for me because I needed to make a lot of updates to the puzzles (at least to start) and having to do it through a web interface felt way too slow.
Andther problem I was having was that the puzzles tables wasn’t synced between dev and prod, which made it hard to test.
ActiveHash is a Ruby gem that let you just define all of your data for a model in a hashmap or file instead of having a database table.
This feels good for now because I only have like 6 puzzles and so having them all in a file makes it way easier to edit them.
The main thing that makes me feel nervous about it is that right now I’m
entering the puzzle IDs manually (like id: 1) and I need to make sure to not
accidentally reuse/change the IDs. This is important because some other fields
in the database reference the puzzle ID.
Here’s what my Puzzle with ActiveHash looks like. It’s really simple.
class Puzzle < ActiveHash::Base
def to_param
"#{id}-#{slug}"
end
def finished?(user)
PuzzleStatus.where(user_id: user.id).where(puzzle_id: self.id).first&.finished || false
end
def cloud_init
File.read("puzzles/#{group}/#{slug}/cloud-init.yaml")
end
self.data = [
{
id: 1,
group: "networking",
slug: "connection-timeout",
title: "The Case of the Connection Timeout",
published: false,
},
... more data here
Things like belongs_to and has_many don’t work (which kinda makes sense to
me), but I can still do Puzzle.find(id) to find a puzzle by its ID, so I
didn’t have to change too much of my code.
And because I’m using ActiveRecord methods, if I ever want to switch back to using a database to manage them, it should be pretty easy!
I needed to:
puzzles table from the databasepuzzles.yml fake data from my tests (because it was inserting that data into the database, which was failing)and probably some more things that I’ve forgotten
I felt pretty happy about both these changes.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。