~xenrox/ansible

99575211eb02617e5fba58e8c1ba0959d53db6d0 — Thorben Günther 1 year, 6 months ago efe8522
srht: Fix failing builds when triggered by git

Without this fix builds, that were triggered by a git push, would cause
the worker to crash:
    panic: GetJob: pq: unnamed prepared statement does not exist

Those builds would stay "pending" forever.
This is caused by Go's Postgres driver [1] [2] in combination with using
PgBouncer in "Transaction pooling" mode.
The recommended workaround is to set `binary_parameters=yes` in the
connection string. If that is done in sourcehut's main config.ini file
though, the python part of builds will fail:
    sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) invalid dsn: invalid connection option "binary_parameters"

For that reason a separate config, which is used by the worker, is
created and only differs in the connection-string.

[1]: https://github.com/lib/pq/issues/889
[2]: https://github.com/lib/pq/pull/759
A roles/srht/files/worker_override.conf => roles/srht/files/worker_override.conf +3 -0
@@ 0,0 1,3 @@
[Service]
ExecStart=
ExecStart=/usr/bin/builds.sr.ht-worker --config /etc/sr.ht/worker.ini

M roles/srht/handlers/main.yml => roles/srht/handlers/main.yml +5 -0
@@ 5,6 5,11 @@
    state: restarted
  with_items: "{{ srht_services }}"

- name: Restart worker
  ansible.builtin.systemd:
    name: builds.sr.ht-worker
    state: restarted

- name: restart nginx
  ansible.builtin.systemd:
    name: nginx

M roles/srht/tasks/main.yml => roles/srht/tasks/main.yml +27 -0
@@ 70,6 70,33 @@
    - git.sr.ht-webhooks.service
    - git.sr.ht-periodic.service

- name: Create systemd unit override path for worker
  ansible.builtin.file:
    path: /etc/systemd/system/builds.sr.ht-worker.service.d
    state: directory
    owner: root
    group: root
    mode: 0755

- name: Install worker systemd unit override file
  ansible.builtin.copy:
    src: worker_override.conf
    dest: /etc/systemd/system/builds.sr.ht-worker.service.d/override.conf
    owner: root
    group: root
    mode: 0644

- name: Configure worker
  ansible.builtin.template:
    src: config.ini.j2
    dest: /etc/sr.ht/worker.ini
    owner: root
    group: root
    mode: 0644
  vars:
    worker_config: true
  notify: Restart worker

- name: Create db user
  community.general.postgresql_user:
    name: srht

M roles/srht/templates/builds.ini.j2 => roles/srht/templates/builds.ini.j2 +4 -0
@@ 8,7 8,11 @@ debug-host=0.0.0.0
debug-port=5002
#
# Configures the SQLAlchemy connection string for the database.
{% if worker_config is defined and worker_config %}
connection-string=postgresql://srht:{{ srht_secrets['psql_password'] }}@localhost:6432/srht-builds?binary_parameters=yes
{% else %}
connection-string=postgresql://srht:{{ srht_secrets['psql_password'] }}@localhost:6432/srht-builds
{% endif %}
#
# Set to "yes" to automatically run migrations on package upgrade.
migrate-on-upgrade=yes