11package org .example .project .service .job ;
22
3+ import jakarta .annotation .PostConstruct ;
4+ import jakarta .annotation .PreDestroy ;
35import lombok .RequiredArgsConstructor ;
6+ import lombok .extern .slf4j .Slf4j ;
47import org .example .project .configuration .InvestConfig ;
58import org .example .project .domain .Stoncks ;
69import org .example .project .repository .StoncksRepository ;
1215import java .math .BigDecimal ;
1316import java .time .LocalDateTime ;
1417
18+ @ Slf4j
1519@ Service
1620@ RequiredArgsConstructor
1721public class CheckStoncksJob {
18-
1922 private final InvestConfig investConfig ;
2023 private final StoncksRepository stoncksRepository ;
24+ private InvestApi api ;
25+
26+ @ PostConstruct
27+ public void init () {
28+ if (investConfig .getToken () != null ) {
29+ this .api = InvestApi .create (investConfig .getToken ());
30+ }
31+ }
2132
2233 @ Scheduled (fixedRate = 10000L )
23- private void getStoncks () {
24- if (investConfig .getToken () == null )
34+ public void getStoncks () {
35+ if (api == null || investConfig .getToken () == null ) {
2536 return ;
26- var api = InvestApi .create (investConfig .getToken ());
27- Portfolio portfolio = api .getOperationsService ().getPortfolioSync (investConfig .getUserId ());
28- BigDecimal value = portfolio .getTotalAmountPortfolio ().getValue ();
29- stoncksRepository .save (Stoncks .builder ()
30- .time (LocalDateTime .now ())
31- .value (value )
32- .build ());
37+ }
38+
39+ try {
40+ Portfolio portfolio = api .getOperationsService ()
41+ .getPortfolioSync (investConfig .getUserId ());
42+
43+ BigDecimal value = portfolio .getTotalAmountPortfolio ().getValue ();
44+
45+ stoncksRepository .save (Stoncks .builder ()
46+ .time (LocalDateTime .now ())
47+ .value (value )
48+ .build ());
49+ } catch (Exception e ) {
50+ log .error (e .getLocalizedMessage ());
51+ }
3352 }
34- }
53+ }
0 commit comments