async def test_redis_sentinel_failure(create_pool): """ FIXME: this is currently causing 3 "Task was destroyed but it is pending!" warnings """ settings = RedisSettings() settings.host = [('localhost', 6379), ('localhost', 6379)] settings.sentinel = True try: pool = await create_pool(settings) await pool.ping('ping') except Exception as e: assert 'unknown command `SENTINEL`' in str(e)
async def main(): redis = await create_pool(RedisSettings()) # no id, random id will be generated job1 = await redis.enqueue_job('the_task') print(job1) """ > <narq job 99edfef86ccf4145b2f64ee160fa3297> """ # random id again, again the job will be enqueued and a job will be returned job2 = await redis.enqueue_job('the_task') print(job2) """ > <narq job 7d2163c056e54b62a4d8404921094f05> """ # custom job id, job will be enqueued job3 = await redis.enqueue_job('the_task', _job_id='foobar') print(job3) """ > <narq job foobar> """ # same custom job id, job will not be enqueued and enqueue_job will return None job4 = await redis.enqueue_job('the_task', _job_id='foobar') print(job4) """
def test_settings_changed(): settings = RedisSettings(port=123) assert settings.port == 123 assert ( "RedisSettings(host='localhost', port=123, database=0, password=None, ssl=None, conn_timeout=1, " "conn_retries=5, conn_retry_delay=1, sentinel=False, sentinel_master='mymaster')" ) == str(settings)
async def main(): redis = await create_pool( RedisSettings(), job_serializer=msgpack.packb, job_deserializer=lambda b: msgpack.unpackb(b, raw=False), ) await redis.enqueue_job('the_task')
async def test_enqueue_job_nondefault_queue(worker): """Test initializing narq_redis with a queue name, and the worker using it.""" narq_redis = await create_pool(RedisSettings(), default_queue_name='test_queue') await test_enqueue_job( narq_redis, lambda functions, **_: worker( functions=functions, narq_redis=narq_redis, queue_name=None), queue_name=None, )
async def main(): redis = await create_pool(RedisSettings()) # deferred by 10 seconds await redis.enqueue_job('the_task', _defer_by=10) # deferred by 1 minute await redis.enqueue_job('the_task', _defer_by=timedelta(minutes=1)) # deferred until jan 28th 2032, you'll be waiting a long time for this... await redis.enqueue_job('the_task', _defer_until=datetime(2032, 1, 28))
async def test_redis_success_log(caplog, create_pool): caplog.set_level(logging.INFO) settings = RedisSettings() pool = await create_pool(settings) assert 'redis connection successful' not in [ r.message for r in caplog.records ] pool.close() await pool.wait_closed() pool = await create_pool(settings, retry=1) assert 'redis connection successful' in [r.message for r in caplog.records] pool.close() await pool.wait_closed()
async def test_redis_log(create_pool): redis = await create_pool(RedisSettings()) await redis.flushall() await redis.set(b'a', b'1') await redis.set(b'b', b'2') log_msgs = [] def _log(s): log_msgs.append(s) await log_redis_info(redis, _log) assert len(log_msgs) == 1 assert re.search(r'redis_version=\d\.', log_msgs[0]), log_msgs assert log_msgs[0].endswith(' db_keys=2')
async def main(): redis = await create_pool(RedisSettings()) job = await redis.enqueue_job('the_task') # get the job's id print(job.job_id) """ > 68362958a244465b9be909db4b7b5ab4 (or whatever) """ # get information about the job, will include results if the job has finished, but # doesn't await the job's result debug(await job.info()) """ > docs/examples/job_results.py:23 main JobDef( function='the_task', args=(), kwargs={}, job_try=None, enqueue_time=datetime.datetime(2019, 4, 23, 13, 58, 56, 781000), score=1556027936781 ) (JobDef) """ # get the Job's status print(await job.status()) """ > JobStatus.queued """ # poll redis for the job result, if the job raised an exception, # it will be raised here # (You'll need the worker running at the same time to get a result here) print(await job.result(timeout=5)) """
async def main(): redis = await create_pool(RedisSettings()) await redis.enqueue_job('the_task')
async def main(): redis = await create_pool(RedisSettings()) await redis.enqueue_job('download_content', 'https://httpbin.org/status/503')
async def test_redis_timeout(mocker, create_pool): mocker.spy(narq.utils.asyncio, 'sleep') with pytest.raises(OSError): await create_pool(RedisSettings(port=0, conn_retry_delay=0)) assert narq.utils.asyncio.sleep.call_count == 5
def parse_redis_settings(cls, v): if isinstance(v, str): return RedisSettings.from_dsn(v) else: return v
async def main(): redis = await create_pool(RedisSettings()) for url in ('https://facebook.com', 'https://microsoft.com', 'https://github.com'): await redis.enqueue_job('download_content', url)