name: Cron Metrics

on:
  schedule:
    - cron: '*/20 * * * *'
  workflow_dispatch:

jobs:
  update:
    name: Calculate metrics
    runs-on: ubuntu-latest
    strategy:
      matrix:
        env: ['staging', 'production']
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v2
        with: 
          fetch-depth: 0
      - name: Checkout latest cron release tag
        run: |
          LATEST_TAG=$(git describe --tags --abbrev=0 --match='cron-*')
          git checkout $LATEST_TAG
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - uses: bahmutov/npm-install@v1
      - name: Run job
        env:
          DEBUG: '*'
          ENV: ${{ matrix.env }}
          STAGING_PG_CONNECTION: ${{ secrets.STAGING_PG_CONNECTION }}
          STAGING_RO_PG_CONNECTION: ${{ secrets.STAGING_PG_CONNECTION }} # no replica for staging
          PROD_PG_CONNECTION: ${{ secrets.PROD_PG_CONNECTION }}
          PROD_RO_PG_CONNECTION: ${{ secrets.PROD_RO_PG_CONNECTION }}
        run: yarn --cwd packages/cron start:metrics

      - name: Heartbeat
        if: ${{ success() }}
        run: ./packages/tools/scripts/cli.js heartbeat --token ${{ secrets.OPSGENIE_KEY }} --name cron-web3storage-metrics        
