momentjs aws-lambda wrong timezone
I have this following node js code:
<pre class="lang-js s-code-block"><code><span>let</span> pureDate = <span>new</span> <span>Date</span>(<span>'2022-12-22T00:00:00.000Z'</span>)
<span>console</span>.<span>log</span>(pureDate)
<span>let</span> formatted1 = <span>moment</span>(pureDate).<span>format</span>(<span>'YYYY-MM-DDTHH:00:00+hh:mm'</span>)
<span>console</span>.<span>log</span>(formatted1)
<span>let</span> formatted2 = moment.<span>utc</span>(pureDate).<span>local</span>().<span>format</span>(<span>'YYYY-MM-DDTHH:00:00+hh:mm'</span>)
<span>console</span>.<span>log</span>(formatted2)
</code></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"><code><span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>.000Z
<span>2022</span>-<span>12</span>-22<span>T07</span>:<span>00</span>:<span>00</span>+<span>07</span>:<span>00</span>
<span>2022</span>-<span>12</span>-22<span>T07</span>:<span>00</span>:<span>00</span>+<span>07</span>:<span>00</span>
</code></pre>
<p>
Now I change timezone to +12:00 and rerun the code, it print:
</p>
<pre class="lang-js s-code-block"><code><span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>.000Z
<span>2022</span>-<span>12</span>-22<span>T12</span>:<span>00</span>:<span>00</span>+<span>12</span>:<span>00</span>
<span>2022</span>-<span>12</span>-22<span>T12</span>:<span>00</span>:<span>00</span>+<span>12</span>:<span>00</span>
</code></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"><code><span>Resources</span>:
<span>timerHandler</span>:
<span>Type</span>: <span>AWS</span>::<span>Serverless</span>::<span>Function</span>
<span>Properties</span>:
<span>Handler</span>: src.<span>timerHandler</span>
<span>Runtime</span>: nodejs16.<span>x</span>
<span>Environment</span>:
<span>Variables</span>:
<span>NODE_ENV</span>: lc
<span>Timeout</span>: <span>100</span>
<span>Events</span>:
<span>ScheduledEvent</span>:
<span>Type</span>: <span>Schedule</span>
<span>Properties</span>:
<span>Schedule</span>: <span>"rate(15 minutes)"</span>
</code></pre>
<p>
After sometime, I access the log and see (please scroll to right-end):
</p>
<pre class="lang-js s-code-block"><code><span>2022</span>-<span>12</span>-22<span>T22</span>:<span>47</span>:<span>04</span><span>.451</span>+<span>07</span>:<span>00</span> <span>2022</span>-<span>12</span>-22<span>T15</span>:<span>47</span>:<span>04</span>.451Z e55ee401-87fd-<span>4614</span>-a7c6-971973ccccc6 <span>INFO</span> <span>pureDate</span>: <span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>.000Z
<span>2022</span>-<span>12</span>-22<span>T22</span>:<span>47</span>:<span>04</span><span>.451</span>+<span>07</span>:<span>00</span> <span>2022</span>-<span>12</span>-22<span>T15</span>:<span>47</span>:<span>04</span>.451Z e55ee401-87fd-<span>4614</span>-a7c6-971973ccccc6 <span>INFO</span> <span>formatted1</span>: <span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>+<span>12</span>:<span>00</span>
<span>2022</span>-<span>12</span>-22<span>T22</span>:<span>47</span>:<span>04</span><span>.451</span>+<span>07</span>:<span>00</span> <span>2022</span>-<span>12</span>-22<span>T15</span>:<span>47</span>:<span>04</span>.451Z e55ee401-87fd-<span>4614</span>-a7c6-971973ccccc6 <span>INFO</span> <span>formatted2</span>: <span>2022</span>-<span>12</span>-22<span>T00</span>:<span>00</span>:<span>00</span>+<span>12</span>:<span>00</span>
</code></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"><code><span>2022</span>-<span>12</span>-22<span>T23</span>:<span>46</span>:<span>32.052</span>+<span>07</span>:<span>00</span> <span>2022</span>-<span>12</span>-22<span>T16</span>:<span>46</span>:<span>32.</span>052Z f1d04019-<span>44e7</span>-463a-af1d-faf2a5043887 <span>INFO</span> <span>zone_name</span>: <span>Africa</span>/<span>Abidjan</span>
<span>2022</span>-<span>12</span>-22<span>T23</span>:<span>46</span>:<span>32.052</span>+<span>07</span>:<span>00</span> <span>2022</span>-<span>12</span>-22<span>T16</span>:<span>46</span>:<span>32.</span>052Z f1d04019-<span>44e7</span>-463a-af1d-faf2a5043887 <span>INFO</span> <span>timezone</span>: <span>GMT</span>
</code></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>
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>
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"><code><span>@Component</span>
<span>public</span> <span>class</span> <span>RequestFilter</span> <span>implements</span> <span>WebFilter</span> {
<span>@Override</span>
<span>public</span> Mono<Void> <span>filter</span><span>(ServerWebExchange exchange, WebFilterChain chain)</span> {
exchange.getResponse().beforeCommit(() -> Mono.deferWithContext(c -> {
<span>//I CHECK WHETHER DATA SHARED FROM CONTROLLER EXIST OR NOT</span>
System.out.println(c.hasKey(<span>"abc"</span>));
<span>Context</span> <span>innerContext</span> <span>=</span> c.get(<span>"CONTEXT"</span>);
System.out.println(innerContext.hasKey(<span>"abc"</span>));
<span>return</span> Mono.empty();
}));
<span>ServerHttpRequest</span> <span>request</span> <span>=</span> exchange.getRequest();
<span>BodyCaptureExchange</span> <span>bodyCaptureExchange</span> <span>=</span> <span>new</span> <span>BodyCaptureExchange</span>(exchange);
<span>return</span> chain.filter(bodyCaptureExchange).subscriberContext(context -> {
<span>//HERE I PUT CONTEXT TO ITSELF</span>
context = context.put(<span>"CONTEXT"</span>, context);
<span>return</span> context;
});
}
}</code>
Here is my controller method:</pre>
<pre class="lang-java s-code-block"><code><span>public</span> Mono<ResponseEntity<Response>> <span>post</span><span>(<span>@RequestBody</span> Mono<Request> request)</span> {
Mono<ResponseEntity<Response>> responseEntityMono = request.flatMap(
rq -> service.requestProcess(rq).map(res -> ResponseEntity.ok().body(res))
).subscriberContext(c -> {
<span>//HERE I GET THE INNER CONTEXT FROM ABOVE FILTER</span>
<span>Context</span> <span>innerContext</span> <span>=</span> c.get(<span>"CONTEXT"</span>);
<span>//PUT DATA TO IT</span>
c = c.put(<span>"CONTEXT"</span>, innerContext.put(<span>"abc"</span>, <span>"abc"</span>));
<span>//ALSO PUT DATA TO PARENT</span>
c = c.put(<span>"abc"</span>, <span>"abc"</span>);
<span>return</span> c;
});
<span>return</span> responseEntityMono;
}</code>
</pre>
<p>
The System.out.println in RequestFilter print:
</p>
<pre class="lang-java s-code-block"><code><span>false</span>
<span>false</span>
</code></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>