Skip to content

Commit

Permalink
feat(job): add log method [python] (#2476) ref #2472
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Mar 16, 2024
1 parent b3860df commit 34946c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
16 changes: 16 additions & 0 deletions python/bullmq/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ async def moveToFailed(self, err, token:str, fetchNext:bool = False):

self.attemptsMade = self.attemptsMade + 1

def log(self, logRow: str):
return Job.addJobLog(self.queue, self.id, logRow, self.opts.get("keepLogs", 0))

async def saveStacktrace(self, pipe, err:str):
stacktrace = traceback.format_exc()
stackTraceLimit = self.opts.get("stackTraceLimit")
Expand Down Expand Up @@ -261,6 +264,19 @@ async def fromId(queue: Queue, jobId: str):
if len(raw_data):
return Job.fromJSON(queue, raw_data, jobId)

@staticmethod
async def addJobLog(queue: Queue, jobId: str, logRow: str, keepLogs: int = 0):
logs_key = f"{queue.prefix}:{queue.name}:{jobId}:logs"
multi = await queue.client.pipeline()

multi.rpush(logs_key, logRow)

if keepLogs:
multi.ltrim(logs_key, -keepLogs, -1)

result = await multi.execute()

return min(keepLogs, result[0]) if keepLogs else result[0]

def optsFromJSON(rawOpts: dict) -> dict:
# opts = json.loads(rawOpts)
Expand Down
5 changes: 0 additions & 5 deletions python/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#!/bin/bash
redis-cli flushall
python3 -m unittest -v tests.bulk_tests
python3 -m unittest -v tests.delay_tests
python3 -m unittest -v tests.flow_tests
python3 -m unittest -v tests.job_tests
python3 -m unittest -v tests.queue_tests
python3 -m unittest -v tests.worker_tests
12 changes: 12 additions & 0 deletions python/tests/job_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ async def test_get_job_state(self):

await queue.close()

async def test_job_log(self):
queue = Queue(queueName)
firstLog = 'some log text 1'
secondLog = 'some log text 2'
job = await queue.add("test-job", {"foo": "bar"}, {})
await job.log(firstLog)
log_count = await job.log(secondLog)

self.assertEqual(log_count, 2)

await queue.close()

async def test_update_job_data(self):
queue = Queue(queueName)
job = await queue.add("test", {"foo": "bar"}, {})
Expand Down

0 comments on commit 34946c4

Please sign in to comment.