@@ -33,8 +33,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
3333 {
3434 parent ::execute ($ input , $ output );
3535
36- $ manager = Manager::fromFile (MANAGER_DIRECTORY . '/manager.yaml ' );
3736 $ forceUpdates = false !== $ input ->getOption ('force ' );
37+ $ onlyInstances = false !== $ input ->getOption ('only-instances ' );
38+
39+ $ manager = Manager::fromFile (MANAGER_DIRECTORY . '/manager.yaml ' );
3840
3941 if (false !== $ input ->getOption ('crontab ' )) {
4042 $ output ->writeln ('This line is to add to your crontabs, in order to run periodic tasks needed by your instances and their behaviours. ' );
@@ -48,13 +50,48 @@ protected function execute(InputInterface $input, OutputInterface $output)
4850 return Command::SUCCESS ;
4951 }
5052
51- $ instancesToRestart = [];
53+ $ output ->writeln ('⚙️ Applying Instances trading hours limitations... ' );
54+ $ this ->applyTradingHours ($ manager , $ output );
55+
56+ $ output ->writeln ('' );
57+ $ output ->writeln ('⚙️ Updating behaviours... ' );
58+ $ updatedInstances = $ this ->updateBehaviours ($ manager , $ output , $ forceUpdates , $ onlyInstances );
59+
60+ $ this ->restartInstances ($ updatedInstances , $ output );
61+
62+ $ output ->writeln ('' );
63+ $ output ->writeln ('🎉 <info>Done!</info> ' );
64+
65+ return Command::SUCCESS ;
66+ }
67+
68+ private function applyTradingHours (Manager $ manager , OutputInterface $ output ): void
69+ {
70+ foreach ($ manager ->getInstances () as $ instance ) {
71+ $ handler = InstanceHandler::init ($ instance );
72+
73+ if ($ instance ->isRunning () && true === $ instance ->isOutOfTradingHours ()) {
74+ $ output ->write (sprintf (' <comment>[%s]</comment> Out of trading hours -> Stopping... ' , (string ) $ instance ));
75+ $ handler ->stop ();
76+ $ output ->writeln ('✅ ' );
77+ } else {
78+ $ output ->writeln (sprintf (' <comment>[%s]</comment> ⏺ ' , (string ) $ instance ));
79+ }
80+ }
81+ }
82+
83+ private function updateBehaviours (
84+ Manager $ manager ,
85+ OutputInterface $ output ,
86+ bool $ forceUpdates = false ,
87+ bool $ onlyInstances = false
88+ ): array {
89+ $ updatedInstances = [];
5290
53- $ output ->writeln ('⚙️ Updating... ' );
5491 foreach ($ manager ->getBehaviours () as $ behaviour ) {
5592 $ behaviourName = ucfirst ($ behaviour ->getSlug ());
5693
57- if ($ forceUpdates || false === $ input -> getOption ( ' only-instances ' ) ) {
94+ if ($ forceUpdates || false === $ onlyInstances ) {
5895 $ output ->write (sprintf (' <comment>[%s]</comment> Main update... ' , $ behaviourName ));
5996 if ($ forceUpdates || $ behaviour ->needsCronUpdate ()) {
6097 $ behaviour ->updateCron ();
@@ -80,7 +117,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
80117 if ($ forceUpdates || $ behaviour ->needsInstanceUpdate ($ instance )) {
81118 $ behaviour ->updateInstanceFromCron ($ instance );
82119 InstanceFilesystem::writeInstanceConfig ($ instance );
83- $ instancesToRestart [] = $ instance ;
120+ $ updatedInstances [] = $ instance ;
84121 $ output ->writeln ('✅ ' );
85122 } else {
86123 $ output ->writeln ('⏺ ' );
@@ -90,27 +127,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
90127 $ behaviour ->write ();
91128 }
92129
93- if ($ instancesToRestart ) {
94- $ output ->writeln ('' );
95- $ output ->writeln ('⚙️ Restarting updated running instances... ' );
96- foreach ($ instancesToRestart as $ instance ) {
97- $ handler = InstanceHandler::init ($ instance );
130+ return $ updatedInstances ;
131+ }
98132
99- if ($ instance ->isRunning ()) {
100- $ output ->write (sprintf (
101- ' <comment>[%s]</comment> Restarting instance `%s`... ' ,
102- $ behaviourName ,
103- (string ) $ instance
104- ));
105- $ handler ->restart (false );
106- $ output ->writeln ('✅ ' );
107- }
108- }
133+ private function restartInstances (array $ updatedInstances , OutputInterface $ output ): void
134+ {
135+ $ instancesToRestart = array_filter ($ updatedInstances , function (Instance $ instance ): bool {
136+ return $ instance ->isRunning ();
137+ });
138+
139+ if (!$ instancesToRestart ) {
140+ return ;
109141 }
110142
111143 $ output ->writeln ('' );
112- $ output ->writeln ('🎉 <info>Done!</info> ' );
144+ $ output ->writeln ('⚙️ Restarting updated running instances... ' );
145+ foreach ($ instancesToRestart as $ instance ) {
146+ $ handler = InstanceHandler::init ($ instance );
113147
114- return Command::SUCCESS ;
148+ if ($ instance ->isRunning ()) {
149+ $ output ->write (sprintf (
150+ ' <comment>[%s]</comment> Restarting... ' ,
151+ (string ) $ instance
152+ ));
153+ $ handler ->restart (false );
154+ $ output ->writeln ('✅ ' );
155+ }
156+ }
115157 }
116158}
0 commit comments