@@ -368,7 +368,8 @@ def __init__(self,
368368 eta = None ,
369369 is_command_override = False ,
370370 high_end = False ,
371- extra_info = None ):
371+ extra_info = None ,
372+ is_from_queue = False ):
372373 self .command = command
373374 self .argument = argument
374375 self .job = job
@@ -377,6 +378,19 @@ def __init__(self,
377378 self .high_end = high_end
378379 self .extra_info = extra_info
379380
381+ # is_from_queue is a temporary hack to keep track of which fuzz tasks came
382+ # from the queue. Previously all fuzz tasks were picked by the bot when
383+ # there was nothing on the queue. With the rearchitecture, we want fuzz
384+ # tasks that were put on the queue by the schedule_fuzz cron job to be
385+ # executed on batch. is_from_queue is used to do this.
386+ # TODO(b/378684001): This code is very ugly, get rid of it when no more
387+ # fuzz tasks are executed on the bots themselves (i.e. when the rearch
388+ # is complete).
389+ self .is_from_queue = is_from_queue
390+
391+ def __repr__ (self ):
392+ return f'Task: { self .command } { self .argument } { self .job } '
393+
380394 def attribute (self , _ ):
381395 return None
382396
@@ -414,11 +428,13 @@ def lease(self):
414428class PubSubTask (Task ):
415429 """A Pub/Sub task."""
416430
417- def __init__ (self , pubsub_message ):
431+ def __init__ (self , pubsub_message , is_from_queue = False ):
418432 self ._pubsub_message = pubsub_message
419433 super ().__init__ (
420- self .attribute ('command' ), self .attribute ('argument' ),
421- self .attribute ('job' ))
434+ self .attribute ('command' ),
435+ self .attribute ('argument' ),
436+ self .attribute ('job' ),
437+ is_from_queue = is_from_queue )
422438
423439 self .extra_info = {
424440 key : value
@@ -524,7 +540,7 @@ def initialize_task(message) -> PubSubTask:
524540 """Creates a task from |messages|."""
525541
526542 if message .attributes .get ('eventType' ) != 'OBJECT_FINALIZE' :
527- return PubSubTask (message )
543+ return PubSubTask (message , is_from_queue = True )
528544
529545 # Handle postprocess task.
530546 # The GCS API for pub/sub notifications uses the data field unlike
@@ -533,7 +549,7 @@ def initialize_task(message) -> PubSubTask:
533549 name = data ['name' ]
534550 bucket = data ['bucket' ]
535551 output_url_argument = storage .get_cloud_storage_file_path (bucket , name )
536- return PostprocessPubSubTask (output_url_argument , message )
552+ return PostprocessPubSubTask (output_url_argument , message , is_from_queue = True )
537553
538554
539555class PostprocessPubSubTask (PubSubTask ):
@@ -542,14 +558,21 @@ class PostprocessPubSubTask(PubSubTask):
542558 def __init__ (self ,
543559 output_url_argument ,
544560 pubsub_message ,
545- is_command_override = False ):
561+ is_command_override = False ,
562+ is_from_queue = False ):
546563 command = 'postprocess'
547564 job_type = 'none'
548565 eta = None
549566 high_end = False
550567 grandparent_class = super (PubSubTask , self )
551- grandparent_class .__init__ (command , output_url_argument , job_type , eta ,
552- is_command_override , high_end )
568+ grandparent_class .__init__ (
569+ command ,
570+ output_url_argument ,
571+ job_type ,
572+ eta ,
573+ is_command_override ,
574+ high_end ,
575+ is_from_queue = is_from_queue )
553576 self ._pubsub_message = pubsub_message
554577
555578
@@ -609,18 +632,36 @@ def add_utask_main(command, input_url, job_type, wait_time=None):
609632 extra_info = {'initial_command' : initial_command })
610633
611634
635+ def bulk_add_tasks (tasks , queue = None , eta_now = False ):
636+ """Adds |tasks| in bulk to |queue|."""
637+
638+ # Old testcases may pass in queue=None explicitly, so we must check this here.
639+ if queue is None :
640+ queue = default_queue ()
641+
642+ # If callers want delays, they must do it themselves, because this function is
643+ # meant to be used for batch tasks which don't need this.
644+ # Use an ETA of right now for batch because we don't need extra delay, there
645+ # is natural delay added by batch, waiting for utask_main_scheduler,
646+ # postprocess etc.
647+ if eta_now :
648+ now = utils .utcnow ()
649+ for task in tasks :
650+ task .eta = now
651+
652+ pubsub_client = pubsub .PubSubClient ()
653+ pubsub_messages = [task .to_pubsub_message () for task in tasks ]
654+ pubsub_client .publish (
655+ pubsub .topic_name (utils .get_application_id (), queue ), pubsub_messages )
656+
657+
612658def add_task (command ,
613659 argument ,
614660 job_type ,
615661 queue = None ,
616662 wait_time = None ,
617663 extra_info = None ):
618664 """Add a new task to the job queue."""
619- # Old testcases may pass in queue=None explicitly,
620- # so we must check this here.
621- if not queue :
622- queue = default_queue ()
623-
624665 if wait_time is None :
625666 wait_time = random .randint (1 , TASK_CREATION_WAIT_INTERVAL )
626667
@@ -636,10 +677,8 @@ def add_task(command,
636677 # Add the task.
637678 eta = utils .utcnow () + datetime .timedelta (seconds = wait_time )
638679 task = Task (command , argument , job_type , eta = eta , extra_info = extra_info )
639- pubsub_client = pubsub .PubSubClient ()
640- pubsub_client .publish (
641- pubsub .topic_name (utils .get_application_id (), queue ),
642- [task .to_pubsub_message ()])
680+
681+ bulk_add_tasks ([task ], queue = queue )
643682
644683
645684def get_task_lease_timeout ():
0 commit comments