---
- name: Check if Airflow worker deployment directory exists
  stat:
    path: "{{ airflow_worker_dir }}"
  register: worker_dir_stat

- name: Ensure Airflow worker deployment directory exists
  file:
    path: "{{ airflow_worker_dir }}"
    state: directory
    owner: "{{ ssh_user }}"
    group: ytdl
    mode: '0755'
  become: yes
  when: not worker_dir_stat.stat.exists

- name: Sync Airflow worker files
  synchronize:
    src: "{{ playbook_dir }}/../{{ item }}"
    dest: "{{ airflow_worker_dir }}/"
    archive: yes
    recursive: yes
    delete: yes
    rsync_opts: "{{ rsync_default_opts }}"
  loop:
    - "airflow/Dockerfile"
    - "airflow/docker-compose-dl.yaml"
    - "airflow/dags/"
    - "airflow/config/"
    - "setup.py"
    - "yt_ops_services/"
    - "thrift_model/"
    - "VERSION"
    - "airflow/init-airflow.sh"
    - "get_info_json_client.py"
    - "proxy_manager_client.py"
    - "token_generator/"
    - "utils/"

- name: Check if inputfiles directory exists
  stat:
    path: "{{ playbook_dir }}/../airflow/inputfiles"
  register: inputfiles_stat

- name: Sync inputfiles directory if it exists
  synchronize:
    src: "{{ playbook_dir }}/../airflow/inputfiles/"
    dest: "{{ airflow_worker_dir }}/inputfiles/"
    archive: yes
    recursive: yes
    delete: yes
    rsync_opts: "{{ rsync_default_opts }}"
  when: inputfiles_stat.stat.exists

- name: Sync pangramia thrift files
  synchronize:
    src: "{{ playbook_dir }}/../thrift_model/gen_py/pangramia/"
    dest: "{{ airflow_worker_dir }}/pangramia/"
    archive: yes
    recursive: yes
    delete: yes
    rsync_opts: "{{ rsync_default_opts }}"

- name: Create .env file for Airflow worker service
  template:
    src: "../../templates/.env.worker.j2"
    dest: "{{ airflow_worker_dir }}/.env"
    mode: "{{ file_permissions }}"
    owner: "{{ ssh_user }}"
    group: ytdl
  vars:
    service_role: "worker"

- name: Create symlink for docker-compose.yaml
  file:
    src: "{{ airflow_worker_dir }}/docker-compose-dl.yaml"
    dest: "{{ airflow_worker_dir }}/docker-compose.yaml"
    state: link
    owner: "{{ ssh_user }}"
    group: ytdl

- name: Build Airflow worker image
  community.docker.docker_image:
    name: "{{ airflow_image_name }}"
    build:
      path: "{{ airflow_worker_dir }}"
    source: build
    force_source: true

- name: Run Airflow init script
  shell:
    cmd: "chmod +x init-airflow.sh && ./init-airflow.sh"
    chdir: "{{ airflow_worker_dir }}"
  become: yes
  become_user: "{{ ssh_user }}"

- name: Start Airflow worker service
  community.docker.docker_compose_v2:
    project_src: "{{ airflow_worker_dir }}"
    files:
      - "docker-compose-dl.yaml"
    state: present
    remove_orphans: true
