Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_normal_run(self):
consumer = mock.Mock()
Channel('test').send({'test': 'test'}, immediately=True)
channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
channel_layer.router.add_route(route('test', consumer))
old_send = channel_layer.send
channel_layer.send = mock.Mock(side_effect=old_send) # proxy 'send' for counting
worker = PatchedWorker(channel_layer)
worker.termed = 2
worker.run()
self.assertEqual(consumer.call_count, 1)
self.assertEqual(channel_layer.send.call_count, 0)
def producing_view(request):
Channel(channel_name).send(request.channel_encode())
raise HttpResponse.ResponseLater()
return producing_view
def _dispatch_task(self, task_id):
"""
To actually apply a task, by pushing the task id into the running
set and sending the id to a channel worker.
"""
self._running_set.add(task_id)
AsyncResult(task_id).run()
Channel('task').send({'task_id': task_id})
if slack_nag == True:
# send slack to user
data = { 'slack_user_id': slack_user_id, 'slack_user_name': slack_user_name, 'slack_user_email': slack_user_email,
'detection':{ 'email': DETECTED_EMAIL,
'user_pk': DETECTED_UPK
}
}
logger.debug('slack_user_detect scheduling background-slack-nag slack user id %s user id: %s' % (slack_user_id, DETECTED_UPK))
Channel('background-slack-nag').send(data)
else:
# send new user to register job
logger.debug('slack_user_detect scheduling background-register-user slack user id %s slack email: %s' % (slack_user_id, slack_user_email))
data = { 'register_user_name': slack_user_name, 'register_user_email': slack_user_email, 'slack_user_id': slack_user_id}
Channel('background-register-user').send(data)
logger.debug('slack_user_detect task end')
job = Job(
name=data['job_name'],
status="started",
)
job.save()
# Start long running task here (using Celery)
sec3_task = sec3.delay(job.id, reply_channel)
# Store the celery task id into the database if we wanted to
# do things like cancel the task in the future
job.celery_id = sec3_task.id
job.save()
# Tell client task has been started
Channel(reply_channel).send({
"text": json.dumps({
"action": "started",
"job_id": job.id,
"job_name": job.name,
"job_status": job.status,
})
def job_update_entities():
logger.info("%s - schedule running job job_update_entities" % (__name__))
Channel('background-update-entities').send({'comment': 'from jobs schedule'})
return
def send(self, channel_layer=None, requeue_delay=1000):
"""
Sends the message on the configured channel with the stored content.
Deletes the DelayedMessage record if successfully sent.
Args:
channel_layer: optional channel_layer to use
requeue_delay: if the channel is full, milliseconds to wait before requeue
"""
channel_layer = channel_layer or channel_layers[DEFAULT_CHANNEL_LAYER]
try:
Channel(self.channel_name, channel_layer=channel_layer).send(json.loads(self.content), immediately=True)
self.delete()
except channel_layer.ChannelFull:
self.delay = requeue_delay
self.save()
def resolve(request):
mimetype = 'application/json'
data = {}
if request.method == 'POST' and 'entity' in request.POST and request.POST['entity'] != '':
data['entity'] = request.POST['entity']
data['status'] = 0
data['timestamp'] = datetime.datetime.now().timestamp()
data['output'] = "resolve request by %s" % (request.user.username)
data['result'] = 'okay'
sensu_event_resolve(data)
Channel('background-alert').send(dict(data))
return HttpResponse(json.dumps(data), mimetype)