<p>What's the best way to run the <code>messenger:consume</code> task on Kubernetes?</p>

<p><strong>Deployment?</strong></p>

<p>If we do it with a deployment with a certain number of replicas this could work, but what if we do a rolling update of the deployment which then results in a pod to be replaced although a long running message is handled by it right now?</p>

<p>To prevent this we could probably set a high <code>terminationGracePeriodSeconds</code> and use <code>lifecycle.preStop</code> in combination?</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: deploy
  name: deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      run: deploy
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: deploy
    spec:
      terminationGracePeriodSeconds: 6000
      containers:
      - command:
        - sh
        - -c
        - sleep 1d # bin/console messenger:consume
        image: bash
        name: deploy
        lifecycle:
          preStop:
            exec:
              command:
              - sh
              - -c
              - echo "Test if a message is consumed at the moment and prevent POD shutdown till then?"                    
</code></pre>

<p>But during my tests, even if the <code>lifecycle.preStop</code> tasks stops early, the full time defined by <code>terminationGracePeriodSeconds</code> is still waited till the pod is terminated.</p>

<p>Anyone got a good idea out there?</p>

<p>This works for me:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: agent
spec:
  replicas: 8
  selector:
    matchLabels:
      id: agent-pod
  template:
    metadata:
      labels:
        id: agent-pod
    spec:
      terminationGracePeriodSeconds: 3600
      containers:
      - name: agent
        volumeMounts:
          - mountPath: /share
            name: share
        lifecycle:
          preStop:
            exec:
              command: ["sh", "-c", "touch /tmp/kill_me"]
        command:
        - /bin/sh
        - -c
        - &gt;
          sleep 3 &amp;&amp; rm -rf var/cache/* &amp;&amp; bin/console cache:clear &amp;&amp;
          while ! [ -f /tmp/kill_me ];
            do
            bin/console messenger:consume queue --limit=1 --time-limit=60 -vv
          done;
        image: my-symfony-agent-image
</code></pre>

<p>More <a href="https://medium.com/@wuestkamp/symfony-messenger-worker-on-kubernetes-77f75725b5ed?source=friends_link&amp;sk=c0a38e15d6cca86ffdec1496c075bacc" rel="nofollow noreferrer">here</a>.</p>
