go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/cron/admin.go (about)

     1  // Copyright 2021 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cron
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"html"
    21  	"html/template"
    22  	"net/url"
    23  
    24  	"go.chromium.org/luci/common/clock"
    25  	"go.chromium.org/luci/common/errors"
    26  
    27  	"go.chromium.org/luci/server/portal"
    28  )
    29  
    30  type portalPage struct {
    31  	portal.BasePage
    32  }
    33  
    34  func (portalPage) Title(ctx context.Context) (string, error) {
    35  	return "Cron handlers", nil
    36  }
    37  
    38  func (portalPage) Overview(ctx context.Context) (template.HTML, error) {
    39  	text := template.HTML(`
    40  		<p>This page allows to directly invoke a registered cron handler. It is
    41  		primarily intended for use locally during development to manually test
    42  		handlers.
    43  	`)
    44  	if len(Default.handlerIDs()) != 0 {
    45  		text += template.HTML(`Clicking one of the buttons below will result in
    46  			the direct execution of the corresponding cron handler right inside the
    47  			process that handled this button click request.</p>
    48  		`)
    49  	} else {
    50  		text += template.HTML(
    51  			`Note that no cron handlers are registered now, so this page is mostly
    52  			empty. It will have more buttons when cron handlers are added in the
    53  			service code.
    54  			</p>
    55  		`)
    56  	}
    57  	return text, nil
    58  }
    59  
    60  func (portalPage) Actions(ctx context.Context) ([]portal.Action, error) {
    61  	var actions []portal.Action
    62  	for _, id := range Default.handlerIDs() {
    63  		id := id
    64  		actions = append(actions, portal.Action{
    65  			ID:    genActionID(id),
    66  			Title: fmt.Sprintf("Run %q now", id),
    67  			Callback: func(ctx context.Context) (string, template.HTML, error) {
    68  				start := clock.Now(ctx)
    69  				err := Default.executeHandlerByID(ctx, id)
    70  				dur := clock.Since(ctx, start)
    71  				if err != nil {
    72  					return "", "", errors.Annotate(err, "execution of cron handler %q failed after %s", id, dur).Err()
    73  				}
    74  				report := fmt.Sprintf(
    75  					`<p>Execution of cron handler "%s" finished successfully after %s.</p>`,
    76  					html.EscapeString(id), dur,
    77  				)
    78  				return "Success", template.HTML(report), nil
    79  			},
    80  		})
    81  	}
    82  	return actions, nil
    83  }
    84  
    85  func genActionID(id string) string {
    86  	return fmt.Sprintf("run-%s", url.QueryEscape(id))
    87  }
    88  
    89  func init() {
    90  	portal.RegisterPage("cron", portalPage{})
    91  }