33namespace CodedMonkey \Dirigent \Command ;
44
55use CodedMonkey \Dirigent \Doctrine \Repository \PackageRepository ;
6+ use CodedMonkey \Dirigent \Entity \PackageUpdateSource ;
67use CodedMonkey \Dirigent \Message \SchedulePackageUpdate ;
8+ use CodedMonkey \Dirigent \Message \UpdatePackage ;
79use Symfony \Component \Console \Attribute \AsCommand ;
810use Symfony \Component \Console \Command \Command ;
911use Symfony \Component \Console \Input \InputArgument ;
1618#[AsCommand(
1719 name: 'packages:update ' ,
1820 description: 'Schedules packages for update ' ,
21+ help: <<<'TXT'
22+ The <info>%command.name%</info> command schedules packages in the registry for update:
23+
24+ <info>%command.full_name%</info>
25+
26+ <fg=black;bg=yellow> </>
27+ <fg=black;bg=yellow> Make sure a worker is running, or use the --sync option. </>
28+ <fg=black;bg=yellow> </>
29+
30+ By default, only packages that have passed the periodic update interval will be scheduled for update.
31+
32+ Use the <comment>--all</comment> option to schedule all packages for update instead:
33+
34+ <info>%command.full_name% --all</info>
35+
36+ Package updates are scheduled somewhere in the next 12 minutes, except when specifying package names, then they are
37+ scheduled immediately. Check the worker's message queue if updates are not being executed.
38+
39+ It's possible to update specific packages by passing their name as arguments:
40+
41+ <info>%command.full_name% psr/cache psr/log</info>
42+
43+ Use the <comment>--sync</comment> option to skip the worker and update packages synchronously:
44+
45+ <info>%command.full_name% psr/cache psr/log --sync</info>
46+ TXT,
1947)]
2048class PackagesUpdateCommand extends Command
2149{
@@ -30,48 +58,67 @@ protected function configure(): void
3058 {
3159 $ this
3260 ->addArgument ('package ' , InputArgument::OPTIONAL , 'Package to update ' )
33- ->addOption ('force ' , null , InputOption::VALUE_NONE , 'Forces a re-crawl of all packages ' );
61+ ->addOption ('all ' , null , InputOption::VALUE_NONE , 'Update all packages ' )
62+ ->addOption ('sync ' , null , InputOption::VALUE_NONE , 'Updates packages synchronously ' );
3463 }
3564
3665 protected function execute (InputInterface $ input , OutputInterface $ output ): int
3766 {
3867 $ io = new SymfonyStyle ($ input , $ output );
3968
40- $ force = $ input ->getOption ('force ' );
41- $ packageName = $ input ->getArgument ('package ' );
69+ $ all = $ input ->getOption ('all ' );
70+ $ packageNames = $ input ->getArgument ('package ' );
71+ $ sync = $ input ->getOption ('sync ' );
4272
43- $ randomTimes = true ;
44- $ reschedule = false ;
73+ if ( $ sync && ! count ( $ packageNames )) {
74+ $ io -> error ( ' Specify a package to update when using the --sync option. ' ) ;
4575
46- if ($ packageName ) {
47- if (null === $ package = $ this ->packageRepository ->findOneByName ($ packageName )) {
48- $ io ->error ("Package $ packageName not found " );
76+ return Command::FAILURE ;
77+ }
4978
50- return Command::FAILURE ;
51- }
79+ $ randomTimes = true ; // Randomize time of updates
80+ $ source = PackageUpdateSource::Stale;
81+
82+ if (count ($ packageNames )) {
83+ $ packageIds = [];
84+ foreach ($ packageNames as $ packageName ) {
85+ if (null === $ package = $ this ->packageRepository ->findOneByName ($ packageName )) {
86+ $ io ->error ("Package $ packageName not found " );
5287
53- $ io ->writeln ("Scheduling package $ packageName for update... " );
88+ return Command::FAILURE ;
89+ }
5490
55- $ packages = [['id ' => $ package ->getId ()]];
91+ $ io ->writeln ("Scheduling package $ packageName for update... " );
92+ $ packageIds [] = $ package ->getId ();
93+ }
5694
5795 $ randomTimes = false ;
58- $ reschedule = true ;
59- } elseif ($ force ) {
96+ $ source = PackageUpdateSource::Manual ;
97+ } elseif ($ all ) {
6098 $ io ->writeln ('Scheduling all packages for update... ' );
61- $ packages = $ this ->packageRepository ->getAllPackageIds ();
99+ $ packageIds = $ this ->packageRepository ->getAllPackageIds ();
62100
63- $ reschedule = true ;
101+ $ source = PackageUpdateSource::Manual ;
64102 } else {
65103 $ io ->writeln ('Scheduling stale packages for update... ' );
66- $ packages = $ this ->packageRepository ->getStalePackageIds ();
104+ $ packageIds = $ this ->packageRepository ->getStalePackageIds ();
67105 }
68106
69- foreach ($ packages as $ package ) {
70- $ this ->messenger ->dispatch (new SchedulePackageUpdate ($ package ['id ' ], randomTime: $ randomTimes , reschedule: $ reschedule , forceRefresh: $ force ));
71- }
107+ $ packageCount = count ($ packageIds );
108+
109+ if ($ sync ) {
110+ foreach ($ packageIds as $ packageId ) {
111+ $ this ->messenger ->dispatch (new UpdatePackage ($ packageId , $ source ));
112+ }
72113
73- $ packageCount = count ($ packages );
74- $ io ->success ("Scheduled $ packageCount package(s) for update. " );
114+ $ io ->success ("Updated $ packageCount package(s). " );
115+ } else {
116+ foreach ($ packageIds as $ packageId ) {
117+ $ this ->messenger ->dispatch (new SchedulePackageUpdate ($ packageId , $ source , $ randomTimes ));
118+ }
119+
120+ $ io ->success ("Scheduled $ packageCount package(s) for update. " );
121+ }
75122
76123 return Command::SUCCESS ;
77124 }
0 commit comments