<commit_before>// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package internal

import (
	"context"
	"fmt"
	"time"

	"go.chromium.org/luci/common/clock"
	"go.chromium.org/luci/gae/service/datastore"
	"go.chromium.org/luci/server/tq"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/timestamppb"
)

// PokeInterval is target frequency of executions of PokePmTask.
//
// See Dispatch() for details.
const PokeInterval = time.Second

// PokePMTaskRef is used by PM implementation to add its handler.
var PokePMTaskRef tq.TaskClassRef

func init() {
	PokePMTaskRef = tq.RegisterTaskClass(tq.TaskClass{
		ID:        "poke-pm-task",
		Prototype: &PokePMTask{},
		Queue:     "manage-project",
	})

	tq.RegisterTaskClass(tq.TaskClass{
		ID:        "kick-poke-pm-task",
		Prototype: &KickPokePMTask{},
		Queue:     "manage-project",
		Quiet:     true,
		Handler: func(ctx context.Context, payload proto.Message) error {
			task := payload.(*KickPokePMTask)
			var eta time.Time
			if t := task.GetEta(); t != nil {
				eta = t.AsTime()
			}
			return Dispatch(ctx, task.GetLuciProject(), eta)
		},
	})
}

// Dispatch ensures invocation of ProjectManager via PokePMTask.
//
// ProjectManager will be invoced at approximately no earlier than both:
// * eta time (if given)
// * next possible.
func Dispatch(ctx context.Context, luciProject string, eta time.Time) error {
	if datastore.CurrentTransaction(ctx) != nil {
		payload := &KickPokePMTask{LuciProject: luciProject}
		if !eta.IsZero() {
			payload.Eta = timestamppb.New(eta)
		}
		return tq.AddTask(ctx, &tq.Task{
			Title:            luciProject,
			DeduplicationKey: "", // not allowed in a transaction
			Payload:          payload,
		})<commit_msg>cv: record idea as a TODO.<commit_after>// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package internal

import (
	"context"
	"fmt"
	"time"

	"go.chromium.org/luci/common/clock"
	"go.chromium.org/luci/gae/service/datastore"
	"go.chromium.org/luci/server/tq"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/timestamppb"
)

// PokeInterval is target frequency of executions of PokePmTask.
//
// See Dispatch() for details.
const PokeInterval = time.Second

// PokePMTaskRef is used by PM implementation to add its handler.
var PokePMTaskRef tq.TaskClassRef

func init() {
	PokePMTaskRef = tq.RegisterTaskClass(tq.TaskClass{
		ID:        "poke-pm-task",
		Prototype: &PokePMTask{},
		Queue:     "manage-project",
	})

	tq.RegisterTaskClass(tq.TaskClass{
		ID:        "kick-poke-pm-task",
		Prototype: &KickPokePMTask{},
		Queue:     "manage-project",
		Quiet:     true,
		Handler: func(ctx context.Context, payload proto.Message) error {
			task := payload.(*KickPokePMTask)
			var eta time.Time
			if t := task.GetEta(); t != nil {
				eta = t.AsTime()
			}
			return Dispatch(ctx, task.GetLuciProject(), eta)
		},
	})
}

// Dispatch ensures invocation of ProjectManager via PokePMTask.
//
// ProjectManager will be invoced at approximately no earlier than both:
// * eta time (if given)
// * next possible.
func Dispatch(ctx context.Context, luciProject string, eta time.Time) error {
	if datastore.CurrentTransaction(ctx) != nil {
		// TODO(tandrii): use txndefer to immediately trigger a PokePMTask after
		// transaction completes to reduce latency in *most* circumstances.
		// The KickPokePMTask is still required for correctness.
		payload := &KickPokePMTask{LuciProject: luciProject}
		if !eta.IsZero() {
			payload.Eta = timestamppb.New(eta)
		}
		return tq.AddTask(ctx, &tq.Task{
			Title:            luciProject,
			DeduplicationKey: "", // not allowed in a transaction
			Payload:          payload,
		})
{'fraction_non_alphanumeric': 0.11563483735571878, 'fraction_numerical': 0.0033578174186778592, 'mean_word_length': 3.4211502782931356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3730898', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1552, 'n_words': 465}