anaconda875
3 questions
5 answers
Avatar
0
anaconda875 Beginner
anaconda875 Beginner
momentjs aws-lambda wrong timezone
I have this following node js code: <pre class="lang-js s-code-block">&lt;code&gt;&lt;span&gt;let&lt;/span&gt; pureDate = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Date&lt;/span&gt;(&lt;span&gt;'2022-12-22T00:00:00.000Z'&lt;/span&gt;) &lt;span&gt;console&lt;/span&gt;.&lt;span&gt;log&lt;/span&gt;(pureDate) &lt;span&gt;let&lt;/span&gt; formatted1 = &lt;span&gt;moment&lt;/span&gt;(pureDate).&lt;span&gt;format&lt;/span&gt;(&lt;span&gt;'YYYY-MM-DDTHH:00:00+hh:mm'&lt;/span&gt;) &lt;span&gt;console&lt;/span&gt;.&lt;span&gt;log&lt;/span&gt;(formatted1) &lt;span&gt;let&lt;/span&gt; formatted2 = moment.&lt;span&gt;utc&lt;/span&gt;(pureDate).&lt;span&gt;local&lt;/span&gt;().&lt;span&gt;format&lt;/span&gt;(&lt;span&gt;'YYYY-MM-DDTHH:00:00+hh:mm'&lt;/span&gt;) &lt;span&gt;console&lt;/span&gt;.&lt;span&gt;log&lt;/span&gt;(formatted2) &lt;/code&gt;</pre> <p> When I run it at my Windows 10 local machine (+07:00), it print to console: </p> <pre class="lang-js s-code-block">&lt;code&gt;&lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;.000Z &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;+&lt;span&gt;07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;+&lt;span&gt;07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;/code&gt;</pre> <p> Now I change timezone to +12:00 and rerun the code, it print: </p> <pre class="lang-js s-code-block">&lt;code&gt;&lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;.000Z &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T12&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;+&lt;span&gt;12&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T12&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;+&lt;span&gt;12&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;/code&gt;</pre> <p> Everything were fine </p> <p> </p> <p> Now I deploy it to aws lambda using cloudformation: </p> <pre class="lang-js s-code-block">&lt;code&gt;&lt;span&gt;Resources&lt;/span&gt;: &lt;span&gt;timerHandler&lt;/span&gt;: &lt;span&gt;Type&lt;/span&gt;: &lt;span&gt;AWS&lt;/span&gt;::&lt;span&gt;Serverless&lt;/span&gt;::&lt;span&gt;Function&lt;/span&gt; &lt;span&gt;Properties&lt;/span&gt;: &lt;span&gt;Handler&lt;/span&gt;: src.&lt;span&gt;timerHandler&lt;/span&gt; &lt;span&gt;Runtime&lt;/span&gt;: nodejs16.&lt;span&gt;x&lt;/span&gt; &lt;span&gt;Environment&lt;/span&gt;: &lt;span&gt;Variables&lt;/span&gt;: &lt;span&gt;NODE_ENV&lt;/span&gt;: lc &lt;span&gt;Timeout&lt;/span&gt;: &lt;span&gt;100&lt;/span&gt; &lt;span&gt;Events&lt;/span&gt;: &lt;span&gt;ScheduledEvent&lt;/span&gt;: &lt;span&gt;Type&lt;/span&gt;: &lt;span&gt;Schedule&lt;/span&gt; &lt;span&gt;Properties&lt;/span&gt;: &lt;span&gt;Schedule&lt;/span&gt;: &lt;span&gt;"rate(15 minutes)"&lt;/span&gt; &lt;/code&gt;</pre> <p> After sometime, I access the log and see (please scroll to right-end): </p> <pre class="lang-js s-code-block">&lt;code&gt;&lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T22&lt;/span&gt;:&lt;span&gt;47&lt;/span&gt;:&lt;span&gt;04&lt;/span&gt;&lt;span&gt;.451&lt;/span&gt;+&lt;span&gt;07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T15&lt;/span&gt;:&lt;span&gt;47&lt;/span&gt;:&lt;span&gt;04&lt;/span&gt;.451Z e55ee401-87fd-&lt;span&gt;4614&lt;/span&gt;-a7c6-971973ccccc6 &lt;span&gt;INFO&lt;/span&gt; &lt;span&gt;pureDate&lt;/span&gt;: &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;.000Z &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T22&lt;/span&gt;:&lt;span&gt;47&lt;/span&gt;:&lt;span&gt;04&lt;/span&gt;&lt;span&gt;.451&lt;/span&gt;+&lt;span&gt;07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T15&lt;/span&gt;:&lt;span&gt;47&lt;/span&gt;:&lt;span&gt;04&lt;/span&gt;.451Z e55ee401-87fd-&lt;span&gt;4614&lt;/span&gt;-a7c6-971973ccccc6 &lt;span&gt;INFO&lt;/span&gt; &lt;span&gt;formatted1&lt;/span&gt;: &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;+&lt;span&gt;12&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T22&lt;/span&gt;:&lt;span&gt;47&lt;/span&gt;:&lt;span&gt;04&lt;/span&gt;&lt;span&gt;.451&lt;/span&gt;+&lt;span&gt;07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T15&lt;/span&gt;:&lt;span&gt;47&lt;/span&gt;:&lt;span&gt;04&lt;/span&gt;.451Z e55ee401-87fd-&lt;span&gt;4614&lt;/span&gt;-a7c6-971973ccccc6 &lt;span&gt;INFO&lt;/span&gt; &lt;span&gt;formatted2&lt;/span&gt;: &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt;+&lt;span&gt;12&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;/code&gt;</pre> <p> The time 00:00:00 is correct, but the zone +12:00 is not. I would expect the formatted should be 2022-12-22T12:00:00+12:00 or 2022-12-22T00:00:00+00:00 </p> <p> </p> <p> This is zone information of the lambda execution: </p> <pre class="lang-js s-code-block">&lt;code&gt;&lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T23&lt;/span&gt;:&lt;span&gt;46&lt;/span&gt;:&lt;span&gt;32.052&lt;/span&gt;+&lt;span&gt;07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T16&lt;/span&gt;:&lt;span&gt;46&lt;/span&gt;:&lt;span&gt;32.&lt;/span&gt;052Z f1d04019-&lt;span&gt;44e7&lt;/span&gt;-463a-af1d-faf2a5043887 &lt;span&gt;INFO&lt;/span&gt; &lt;span&gt;zone_name&lt;/span&gt;: &lt;span&gt;Africa&lt;/span&gt;/&lt;span&gt;Abidjan&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T23&lt;/span&gt;:&lt;span&gt;46&lt;/span&gt;:&lt;span&gt;32.052&lt;/span&gt;+&lt;span&gt;07&lt;/span&gt;:&lt;span&gt;00&lt;/span&gt; &lt;span&gt;2022&lt;/span&gt;-&lt;span&gt;12&lt;/span&gt;-22&lt;span&gt;T16&lt;/span&gt;:&lt;span&gt;46&lt;/span&gt;:&lt;span&gt;32.&lt;/span&gt;052Z f1d04019-&lt;span&gt;44e7&lt;/span&gt;-463a-af1d-faf2a5043887 &lt;span&gt;INFO&lt;/span&gt; &lt;span&gt;timezone&lt;/span&gt;: &lt;span&gt;GMT&lt;/span&gt; &lt;/code&gt;</pre> <p> Is that the problem of momentjs, or my code, or lambda? How can I fix it? </p> <p> </p> <p> moment version: "^2.29.4" </p>
Answer
Avatar
1
anaconda875 Beginner
anaconda875 Beginner
Sync to Async http request and response architecture
We are developing a <strong>REST service</strong> (2) that act as a adapter between a blackbox <strong>Client</strong> (1) and blackbox <strong>Processor microservices</strong> (4). <p> </p> <p> (2) and (4) communicate with each other using <strong>Kafka</strong> (3) </p> <p> </p> <p> All components are stateless (except Kafka, right?) </p> <p> </p> <p> First, <strong>Client</strong> initial a command to <strong>REST service</strong> using http POST (sync), then <strong>REST service</strong> take this command and forward it to <strong>Processor ms</strong> (after doing some conversion) via <strong>Kafka</strong> (async). After this step, <strong>REST service</strong> will sleep (this http scope) and wait the result from <strong>Processor ms</strong> </p> <p> </p> <p> Then <strong>Processor ms</strong> handle this command and send the result back to <strong>REST service</strong> via <strong>Kafka</strong> (async). <strong>REST service</strong> wake up and and return the ResponseEntity to <strong>Client</strong> within same http scope. </p> <p> </p> <p> Now we decide to migrate (2) to microservice to take advantage of this architecture. And the problem occurred: instance 1 of (2) send command to (4), and (4) send the result to instance 2. But the http session is established between (1) and instance 1, and instance 2 cannot return to (1). This returning must be done by and only by instance 1. </p> <p> </p> <p> My first idea is, for example, let instance 1 be aware of the result from (4) no matter it was sent to which instance. But currently we have no solution to archive this idea. </p> <p> </p> <p> This is the diagram </p> <p> </p> <img class="content-img" src="https://stackask.com/wp-content/uploads/2022/12/Untitled.png" /> <p> </p> <p> We need a "non work-around" solution for this architecture. Please help. Thank you </p> <p> </p> <p> Note: we are unable to make change to blackbox components </p>
Answer
Avatar
0
anaconda875 Beginner
anaconda875 Beginner
Spring weblux how to share data from Controller to Filter using Context
<div class="votecell post-layout--left"> <div class="js-voting-container d-flex jc-center fd-column ai-stretch gs4 fc-black-200"></div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body"> <p> </p> <p> In controller I need to share some data to WebFilter (I don't want to use <code>ServerWebExchange.getAttributes().put("KEY", "VALUE")</code>, and my boss either), this is my current implemetation: </p> <pre class="lang-java s-code-block">&lt;code&gt;&lt;span&gt;@Component&lt;/span&gt; &lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;RequestFilter&lt;/span&gt; &lt;span&gt;implements&lt;/span&gt; &lt;span&gt;WebFilter&lt;/span&gt; { &lt;span&gt;@Override&lt;/span&gt; &lt;span&gt;public&lt;/span&gt; Mono&lt;Void&gt; &lt;span&gt;filter&lt;/span&gt;&lt;span&gt;(ServerWebExchange exchange, WebFilterChain chain)&lt;/span&gt; { exchange.getResponse().beforeCommit(() -&gt; Mono.deferWithContext(c -&gt; { &lt;span&gt;//I CHECK WHETHER DATA SHARED FROM CONTROLLER EXIST OR NOT&lt;/span&gt; System.out.println(c.hasKey(&lt;span&gt;"abc"&lt;/span&gt;)); &lt;span&gt;Context&lt;/span&gt; &lt;span&gt;innerContext&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; c.get(&lt;span&gt;"CONTEXT"&lt;/span&gt;); System.out.println(innerContext.hasKey(&lt;span&gt;"abc"&lt;/span&gt;)); &lt;span&gt;return&lt;/span&gt; Mono.empty(); })); &lt;span&gt;ServerHttpRequest&lt;/span&gt; &lt;span&gt;request&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; exchange.getRequest(); &lt;span&gt;BodyCaptureExchange&lt;/span&gt; &lt;span&gt;bodyCaptureExchange&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;BodyCaptureExchange&lt;/span&gt;(exchange); &lt;span&gt;return&lt;/span&gt; chain.filter(bodyCaptureExchange).subscriberContext(context -&gt; { &lt;span&gt;//HERE I PUT CONTEXT TO ITSELF&lt;/span&gt; context = context.put(&lt;span&gt;"CONTEXT"&lt;/span&gt;, context); &lt;span&gt;return&lt;/span&gt; context; }); } }&lt;/code&gt; Here is my controller method:</pre> <pre class="lang-java s-code-block">&lt;code&gt;&lt;span&gt;public&lt;/span&gt; Mono&lt;ResponseEntity&lt;Response&gt;&gt; &lt;span&gt;post&lt;/span&gt;&lt;span&gt;(&lt;span&gt;@RequestBody&lt;/span&gt; Mono&lt;Request&gt; request)&lt;/span&gt; { Mono&lt;ResponseEntity&lt;Response&gt;&gt; responseEntityMono = request.flatMap( rq -&gt; service.requestProcess(rq).map(res -&gt; ResponseEntity.ok().body(res)) ).subscriberContext(c -&gt; { &lt;span&gt;//HERE I GET THE INNER CONTEXT FROM ABOVE FILTER&lt;/span&gt; &lt;span&gt;Context&lt;/span&gt; &lt;span&gt;innerContext&lt;/span&gt; &lt;span&gt;=&lt;/span&gt; c.get(&lt;span&gt;"CONTEXT"&lt;/span&gt;); &lt;span&gt;//PUT DATA TO IT&lt;/span&gt; c = c.put(&lt;span&gt;"CONTEXT"&lt;/span&gt;, innerContext.put(&lt;span&gt;"abc"&lt;/span&gt;, &lt;span&gt;"abc"&lt;/span&gt;)); &lt;span&gt;//ALSO PUT DATA TO PARENT&lt;/span&gt; c = c.put(&lt;span&gt;"abc"&lt;/span&gt;, &lt;span&gt;"abc"&lt;/span&gt;); &lt;span&gt;return&lt;/span&gt; c; }); &lt;span&gt;return&lt;/span&gt; responseEntityMono; }&lt;/code&gt; </pre> <p> The System.out.println in RequestFilter print: </p> <pre class="lang-java s-code-block">&lt;code&gt;&lt;span&gt;false&lt;/span&gt; &lt;span&gt;false&lt;/span&gt; &lt;/code&gt;</pre> <p> Means that data I put from controller are lost when it come to Filter.  Please correct me if I'm wrong somewhere. I really need your help </p> <p> </p> <p> Reactor version: 3.3.14 </p> <p> </p> <p> Spring boot stater webflux: 2.3.9.RELEASE </p> <p> </p> </div> </div>
Answer