Symfony Messenger / RabbitMQ detecting requed messages - configuration

If consuming message fails message is re-queued with a delay by default.
Is there a way to add a counter to a message so I could know if message is on its last attempt?
This is desired behavior:
First attempt:
App\Message\Message {
body: array:2 [
"id" => 2
"alias" => "some_alias",
"attempt" => 0,
]
}
First retry:
App\Message\Message {
body: array:2 [
"id" => 2
"alias" => "some_alias",
"attempt" => 1,
]
}
Second retry:
App\Message\Message {
body: array:2 [
"id" => 2
"alias" => "some_alias",
"attempt" => 2,
]
}
Third retry:
App\Message\Message {
body: array:2 [
"id" => 2
"alias" => "some_alias",
"attempt" => 3,
]
}

Solution
messenger.yaml:
...
buses:
messenger.bus.default:
middleware:
# service ids that implement Symfony\Component\Messenger\Middleware
- 'App\Middleware\RetryMiddleware'
...
App\Middleware\RetryMiddleware:
namespace App\MessageMiddleware;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Psr\Log\LoggerInterface;
class RetryMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
try {
return $stack->next()->handle($envelope, $stack);
} catch (\Throwable $error) {
$msg = $envelope->getMessage();
$body = $msg->getBody();
$body['attempt']++;
$msg->setBody($body);
//rethrow same error
throw $error;
}
}
}
App\Message:
namespace App\Message;
class Message
{
public $body;
public function getBody()
{
return $this->body;
}
public function setBody(array $body): void
{
$this->body = $body;
}
}

Related

How do I validate an array of JSON objects in a json object?

I'm sending have an array of JSON objects that I'm POSTing to the DB with other data:
{
"emotions":[
{
"id":1,
"intensity":67,
"new_intensity":67
},
{
"id":2,
"intensity":67,
"new_intensity":67
},
{
"id":3,
"intensity":64,
"new_intensity":64
},
{
"id":4,
"intensity":64,
"new_intensity":64
},
{
"id":5,
"intensity":64,
"new_intensity":64
}
],
"situation":"An event",
}
What I want to do is validate both the situation and emotions[{},{}] json array:
Controller
public function store(Request $request)
{
$this->validate($request, [
'situation' => 'required',
]);
$data = ['data' => $request->input('emotions')];
$this->validate($data, [
'data.*.id' => 'digits:2|between:1,70',
'data.*.intensity' => 'digits:3|between:1,100',
'data.*.new_intensity' => 'digits:3|between:1,100'
]);
}
Error
Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given
I'd like to be able to combine the two validating methods, but I have no idea how to do that as well?
Here is the answer I got my first try from
You can use array validators. Something like below code may help you achieve this
$validator = Validator::make($data, [
'data.*.id' => 'digits:2|between:1,70',
'data.*.intensity' => 'digits:3|between:1,100',
'data.*.new_intensity' => 'digits:3|between:1,100'
])
You can check whether it fails and do your desired action
if($validator->fails()){
// Do something
}

cakephp 3 rest errors return as html CrudJsonApi

I'm adding a REST API onto an existing cakephp codebase.
It is working as I expect. I hit /api/v1/contacts/12312 and get json data back for contact 12312. If put an id of a contact that doesn't exist then I get the html 404 page error rather than json.
This happens internally on the contacts->get($id) line.
In the api app controller I have
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Crud.Crud', [
'actions' => [
'Crud.Index',
'Crud.View',
'Crud.Add',
'Crud.Edit',
'Crud.Delete'
],
'listeners' => [
'CrudJsonApi.JsonApi',
'CrudJsonApi.Pagination', // Pagination != ApiPagination
'Crud.ApiQueryLog',
],
'Error' => [
'errorLevel' => E_ALL,
'exceptionRenderer' => 'CrudJsonApi\Error\JsonApiExceptionRenderer',
'skipLog' => [],
'log' => true,
'trace' => true,
],
]);
$this->Crud->config(['listeners.jsonApi.exceptionRenderer' => 'CrudJsonApi\Error\JsonApiExceptionRenderer']);
$this->setJsonResponse();
}
public function beforeRender(Event $event)
{
$this->RequestHandler->renderAs($this, 'json');
$this->response->type('application/json');
$this->set('_serialize', true);
}
I thought using the JsonApiExceptionRenderer 404 errors would be handled with json output.
Also the Pagination works but the Pagination data isnt returned with the response...
{
"viewVar": "crmContacts",
"crmContacts": [
{
"id": 1,
"lastname": "testname1"
},
{
"id": 2,
"lastname": "smith"
}
],
"success": true
}
Any ideas?
Thanks

Appending data to JSON

I'm returning some json data as response in a Controller, i want to add some information to
In my DriversController extend's Apicontroller in DriversController i'm returning some data on api call, i want to appent the status code information to below response
if ($request->wantsJson()) {
return Response::json([
'data' => [
'user_details' => $agent_percentage,
'dropdown_data' => [
'employment_types' => $employment_types->all(),
'roles' => $roles->all(),
'vehicle_brands' => $vehicle_brands->all(),
'vehicle_types' => $vehicle_types->all()
]
]
]);
}
//to the above response
return Response::json([
$this->respondSuccess(), // i am append this information
'data' => [
'user_details' => $agent_percentage,
'dropdown_data' => [
'employment_types' => $employment_types->all(),
'roles' => $roles->all(),
'vehicle_brands' => $vehicle_brands->all(),
'vehicle_types' => $vehicle_types->all()
]
]
]);
In ApiControllre I'm setting all the status code and messages
class ApiController extends Controller
{
protected $statusCode = 200;
//getter status code
public function getStatusCode()
{
return $this->statusCode;
}
//setter status code
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
//failure messages
public function respondFailure($message='Account is not active contact admin', $status='failure')
{
return $this->setStatusCode(400)->respondWithMessage($message, $status);
}
//success messages
public function respondSuccess($message='Agent is active', $status='success')
{
return $this->setStatusCode(200)->respondWithMessage($message, $status);
}
//a layer of abstraction to avoide repetation
public function respond($data, $headers = [])
{
return Response::json($data, $this->getStatusCode(), $headers);
}
//get ststus code and message parse it for errors
public function respondWithMessage($message, $status)
{
return $this->respond([
'status_code' => $this->getStatusCode(),
'status' => $status,
'message' => $message
]);
}
}
But the response i'm getting is different as expected
//expected result
{
"status_code": "200",
"status": "success",
"message": "User details with dropdown data",
"data": {
"user_details": {
"id": 2017001,
"name": "User Name",
"email": "user#email.com",
},
"dropdown_data": {
}
}
}
//getting response
{
"0": {
"headers": {},
"original": {
"status_code": 200,
"status": "success",
"message": "Agent is active"
},
"exception": null
},
"data": {
"user_details": {
"id": 2017001,
"name": "User Name",
"email": "user#email.com",
},
"dropdown_data": {
}
}
}
the middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Response;
use App\Http\Controllers\ApiController;
class UserStatus extends ApiController
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if($request->user() === null)
{
return $this->respondFailure();
}
if($request->user()->isActive($request->user()))
{
return $next($request);
}
return $this->respondFailure();
}
}
You are only appending the response from the respondSuccess() and not merging the response.
$this->setStatusCode(200)->respondWithMessage($message, $status);
on this response:
return Response::json([
$this->respondSuccess(), // i am append this information
'data' => [
'user_details' => $agent_percentage,
'dropdown_data' => [
'employment_types' => $employment_types->all(),
'roles' => $roles->all(),
'vehicle_brands' => $vehicle_brands->all(),
'vehicle_types' => $vehicle_types->all()
]
]
]);
It gives the response as you got not the response you expected.
To get the expected response you need to do something like this:
public function respondWithMessage($message, $status)
{
return [
'status_code' => $this->getStatusCode(),
'status' => $status,
'message' => $message
];
}
I have used only array and not $this->respond() because you only have this message:
"status_code": "200",
"status": "success",
"message": "User details with dropdown data",
For the type of response, you might need to merge the two arrays into one.
Look on array_merge() to get more understanding.
$responseMessage= $this->respondSuccess();
$data = ['data' => [
'user_details' => $agent_percentage,
'dropdown_data' => [
'employment_types' => $employment_types->all(),
'roles' => $roles->all(),
'vehicle_brands' => $vehicle_brands->all(),
'vehicle_types' => $vehicle_types->all()
]
]
];
$responseArray = array_merge(responseMessage, data);
return Response::json($responseArray);
I have not yet tested the code but this might give you some understanding of how to get the expected array response you want.
If I am wrong anyone could suggest the edit.

Access nested JSON Field in Logstash

I have a Problem with accessing a nested JSON field in logstash (latest version).
My config file is the following:
input {
http {
port => 5001
codec => "json"
}
}
filter {
mutate {
add_field => {"es_index" => "%{[statements][authority][name]}"}
}
mutate {
gsub => [
"es_index", " ", "_"
]
}
mutate {
lowercase => ["es_index"]
}
ruby {
init => "
def remove_dots hash
new = Hash.new
hash.each { |k,v|
if v.is_a? Hash
v = remove_dots(v)
end
new[ k.gsub('.','_') ] = v
if v.is_a? Array
v.each { |elem|
if elem.is_a? Hash
elem = remove_dots(elem)
end
new[ k.gsub('.','_') ] = elem
} unless v.nil?
end
} unless hash.nil?
return new
end
"
code => "
event.instance_variable_set(:#data,remove_dots(event.to_hash))
"
}
}
output {
stdout {
codec => rubydebug
}
elasticsearch {
hosts => "elasticsearch:9200"
index => "golab-%{+YYYY.MM.dd}"
}
}
I have a filter with mutate. I want to add a field that I can use as a part of the index name. When I use this "%{[statements][authority][name]}" the content in the brackets is used as string.%{[statements][authority][name]} is saved in the es_indexfield. Logstash seems to think this is a string, but why?
I've also tried to use this expression: "%{statements}". It's working like expected. Everything in the field statements is passed to es_index. If I use "%{[statements][authority]}" strange things happen. es_index is filled with the exact same output that "%{statements}" produces. What am I missing?
Logstash Output with "%{[statements][authority]}":
{
"statements" => {
"verb" => {
"id" => "http://adlnet.gov/expapi/verbs/answered",
"display" => {
"en-US" => "answered"
}
},
"version" => "1.0.1",
"timestamp" => "2016-07-21T07:41:18.013880+00:00",
"object" => {
"definition" => {
"name" => {
"en-US" => "Example Activity"
},
"description" => {
"en-US" => "Example activity description"
}
},
"id" => "http://adlnet.gov/expapi/activities/example"
},
"actor" => {
"account" => {
"homePage" => "http://example.com",
"name" => "xapiguy"
},
"objectType" => "Agent"
},
"stored" => "2016-07-21T07:41:18.013880+00:00",
"authority" => {
"mbox" => "mailto:info#golab.eu",
"name" => "GoLab",
"objectType" => "Agent"
},
"id" => "0771b9bc-b1b8-4cb7-898e-93e8e5a9c550"
},
"id" => "a7e31874-780e-438a-874c-964373d219af",
"#version" => "1",
"#timestamp" => "2016-07-21T07:41:19.061Z",
"host" => "172.23.0.3",
"headers" => {
"request_method" => "POST",
"request_path" => "/",
"request_uri" => "/",
"http_version" => "HTTP/1.1",
"http_host" => "logstasher:5001",
"content_length" => "709",
"http_accept_encoding" => "gzip, deflate",
"http_accept" => "*/*",
"http_user_agent" => "python-requests/2.9.1",
"http_connection" => "close",
"content_type" => "application/json"
},
"es_index" => "{\"verb\":{\"id\":\"http://adlnet.gov/expapi/verbs/answered\",\"display\":{\"en-us\":\"answered\"}},\"version\":\"1.0.1\",\"timestamp\":\"2016-07-21t07:41:18.013880+00:00\",\"object\":{\"definition\":{\"name\":{\"en-us\":\"example_activity\"},\"description\":{\"en-us\":\"example_activity_description\"}},\"id\":\"http://adlnet.gov/expapi/activities/example\",\"objecttype\":\"activity\"},\"actor\":{\"account\":{\"homepage\":\"http://example.com\",\"name\":\"xapiguy\"},\"objecttype\":\"agent\"},\"stored\":\"2016-07-21t07:41:18.013880+00:00\",\"authority\":{\"mbox\":\"mailto:info#golab.eu\",\"name\":\"golab\",\"objecttype\":\"agent\"},\"id\":\"0771b9bc-b1b8-4cb7-898e-93e8e5a9c550\"}"
}
You can see that authority is part of es_index. So it was not chosen as a field.
Many thanks in advance
I found a solution. Credits go to jpcarey (Elasticsearch Forum)
I had to remove codec => "json". That leads to another data structure. statements is now an array and not an object. So I needed to change %{[statements][authority][name]} to %{[statements][0][authority][name]}. That works without problems.
If you follow the given link you'll also find an better implementation of my mutate filters.

Invalid JSON data in request body: Syntax error POST Call Rest API YII2

When I try to post using Postman,I get this error {"name":"Bad Request","message":"Invalid JSON data in request body: Syntax error.","code":0,"status":400,"type":"yii\\web\\BadRequestHttpException"}
My controller is
`class CountryController extends ActiveController
{
public $modelClass = 'app\models\Country';
public function behaviors()
{
return [
[
'class' => 'yii\filters\ContentNegotiator',
'only' => ['index', 'view','create'],
'formats' => ['application/json' => Response::FORMAT_JSON,],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'index'=>['get'],
'view'=>['get'],
'create'=>['post'],
'update'=>['put'],
'delete' => ['delete'],
'deleteall'=>['post'],
],
],
];
}
}`
added
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
in api/config.php file.
Where I am wrong??
Try this
public function behaviors()
{
return [
[
'class' => 'yii\filters\ContentNegotiator',
'only' => ['index', 'view','create'],
'formats' => ['application/json' => Response::FORMAT_JSON]
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'index'=>['get'],
'view'=>['get'],
'create'=>['post'],
'update'=>['post'],
'delete' => ['delete'],
'deleteall'=>['post'],
]
]
];
}
I am using wordpress 4.7 on php 7, also getting problems with the empty request body.
My Controller :
add_action('rest_api_init', function()
{
register_rest_route('v0', '/accounts/(?P<slug>[a-z0-9_\-]+)/accounts', array(
'methods' => 'GET',
'callback' => function($request)
{
try {
return 'hello';
}
catch (Exception $e) {
$error = json_decode($e->getMessage(), true);
return new WP_Error($error['status_code'], $error['message'], "");
}
}
));
});
Response Error :
{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}
I did not found any solution rather than changing in WP core : wp-includes/rest-api/class-wp-rest-request.php and chaning line 672 for conditional check for empty body or not.
$params = json_decode( $this->get_body(), true );