@@ -203,13 +203,9 @@ func (u *ContainerService) CreateCompose(req dto.ComposeCreate) error {
203203 if err := newComposeEnv (req .Path , req .Env ); err != nil {
204204 return err
205205 }
206- pullImages := true
207- if req .PullImage != nil {
208- pullImages = * req .PullImage
209- }
210206 go func () {
211207 taskItem .AddSubTask (i18n .GetMsgByKey ("ComposeCreate" ), func (t * task.Task ) error {
212- err := compose .UpWithTask (req .Path , t , pullImages )
208+ err := compose .UpWithTask (req .Path , t , req . ForcePull )
213209 t .LogWithStatus (i18n .GetMsgByKey ("ComposeCreate" ), err )
214210 if err != nil {
215211 _ , _ = compose .Down (req .Path )
@@ -262,31 +258,43 @@ func (u *ContainerService) ComposeUpdate(req dto.ComposeUpdate) error {
262258 if cmd .CheckIllegal (req .Name , req .Path ) {
263259 return buserr .New ("ErrCmdIllegal" )
264260 }
265- oldFile , err := os .ReadFile (req .DetailPath )
266- if err != nil {
267- return fmt .Errorf ("load file with path %s failed, %v" , req .DetailPath , err )
268- }
269- file , err := os .OpenFile (req .DetailPath , os .O_WRONLY | os .O_TRUNC , 0640 )
261+ taskItem , err := task .NewTaskWithOps (req .Name , task .TaskUpdate , task .TaskScopeCompose , req .TaskID , 1 )
270262 if err != nil {
263+ global .LOG .Errorf ("new task for update compose failed, err: %v" , err )
271264 return err
272265 }
273- defer file .Close ()
274- write := bufio .NewWriter (file )
275- _ , _ = write .WriteString (req .Content )
276- write .Flush ()
266+ go func () {
267+ taskItem .AddSubTask (i18n .GetMsgByKey ("TaskUpdate" ), func (t * task.Task ) error {
268+ oldFile , err := os .ReadFile (req .DetailPath )
269+ if err != nil {
270+ return fmt .Errorf ("load file with path %s failed, %v" , req .DetailPath , err )
271+ }
272+ file , err := os .OpenFile (req .DetailPath , os .O_WRONLY | os .O_TRUNC , 0640 )
273+ if err != nil {
274+ return err
275+ }
276+ defer file .Close ()
277+ write := bufio .NewWriter (file )
278+ _ , _ = write .WriteString (req .Content )
279+ write .Flush ()
277280
278- global .LOG .Infof ("docker-compose.yml %s has been replaced, now start to docker-compose restart" , req .DetailPath )
279- if err := newComposeEnv (req .DetailPath , req .Env ); err != nil {
280- return err
281- }
281+ global .LOG .Infof ("docker-compose.yml %s has been replaced, now start to docker-compose restart" , req .DetailPath )
282+ if err := newComposeEnv (req .DetailPath , req .Env ); err != nil {
283+ return err
284+ }
282285
283- if stdout , err := compose .Up (req .Path ); err != nil {
284- global .LOG .Errorf ("update failed when handle compose up, std: %s, err: %s, now try to recreate the old compose file" , stdout , err )
285- if err := recreateCompose (string (oldFile ), req .Path ); err != nil {
286- return fmt .Errorf ("update failed and recreate old compose file also failed, err: %v" , err )
287- }
288- return fmt .Errorf ("update failed when handle compose up, std: %v, err: %s" , stdout , err )
289- }
286+ if stdout , err := compose .UpWithForcePull (req .Path , req .ForcePull ); err != nil {
287+ global .LOG .Errorf ("update failed when handle compose up, std: %s, err: %s, now try to recreate the old compose file" , stdout , err )
288+ if err := recreateCompose (string (oldFile ), req .Path ); err != nil {
289+ return fmt .Errorf ("update failed and recreate old compose file also failed, err: %v" , err )
290+ }
291+ return fmt .Errorf ("update failed when handle compose up, std: %v, err: %s" , stdout , err )
292+ }
293+
294+ return nil
295+ }, nil )
296+ _ = taskItem .Execute ()
297+ }()
290298
291299 return nil
292300}
0 commit comments