56 lines
2.5 KiB
Python
56 lines
2.5 KiB
Python
"""Broker no-ack redelivery and duplicate completed job, using scoped identities."""
|
|
import json
|
|
import sys
|
|
import time
|
|
import pika
|
|
import psycopg
|
|
from internal import context,http
|
|
|
|
|
|
def credentials(role):
|
|
status,data=http('openbao',8200,'/v1/auth/cert/login',role,'POST',{'name':role})
|
|
assert status==200
|
|
token=json.loads(data)['auth']['client_token']
|
|
status,data=http('openbao',8200,'/v1/secret/data/'+role,role,token=token)
|
|
assert status==200
|
|
return json.loads(data)['data']['data']
|
|
|
|
|
|
def connect(role):
|
|
params=pika.URLParameters(credentials(role)['BROKER_URL'])
|
|
params.ssl_options=pika.SSLOptions(context(),'broker')
|
|
return pika.BlockingConnection(params)
|
|
|
|
|
|
def main():
|
|
if sys.argv[1]=='redeliver':
|
|
bodies=[]
|
|
for attempt in range(2):
|
|
conn=connect('worker');channel=conn.channel()
|
|
deadline=time.monotonic()+15
|
|
while True:
|
|
method,properties,body=channel.basic_get('attachment.process.v1',auto_ack=False)
|
|
if method:break
|
|
assert time.monotonic()<deadline,'Job not present in durable broker queue'
|
|
time.sleep(.2)
|
|
assert properties.delivery_mode==2 and len(body)<4096
|
|
assert 'job_id' in json.loads(body)
|
|
if attempt:assert method.redelivered and body==bodies[0]
|
|
bodies.append(body)
|
|
conn.close() # No ack: broker MUST redeliver the same durable body.
|
|
print('PASS durable broker job delivered, disconnected without ack, redelivered=True; left queued for real worker')
|
|
else:
|
|
secrets=credentials('worker')
|
|
with psycopg.connect(secrets['DATABASE_URL']) as conn:
|
|
row=conn.execute('SELECT id,attachment_id,pipeline_version,attempt,created_at FROM attachment_jobs WHERE attachment_id=%s AND state=\'ready\'',(sys.argv[2],)).fetchone()
|
|
assert row
|
|
body={'schema_version':1,'job_id':str(row[0]),'type':'attachment.process.v1','attachment_id':str(row[1]),
|
|
'pipeline_version':row[2],'attempt':row[3],'created_at':row[4].isoformat(timespec='microseconds').replace('+00:00','Z')}
|
|
conn=connect('dispatcher');channel=conn.channel();channel.confirm_delivery()
|
|
channel.basic_publish('','attachment.process.v1',json.dumps(body).encode(),properties=pika.BasicProperties(delivery_mode=2),mandatory=True)
|
|
conn.close()
|
|
print('PASS completed job duplicate published with broker confirm')
|
|
|
|
|
|
if __name__=='__main__':main()
|