Skip to content

submit_publish_job

Submit publishing job to farm.

ProcessSubmittedJobOnFarm

Bases: InstancePlugin, AYONPyblishPluginMixin, ColormanagedPyblishPluginMixin

Process Job submitted on farm.

These jobs are dependent on a deadline job submission prior to this plug-in.

It creates dependent job on farm publishing rendered image sequence.

Options in instance.data: - deadlineSubmissionJob (dict, Required): The returned .json data from the job submission to deadline.

- outputDir (str, Required): The output directory where the metadata
    file should be generated. It's assumed that this will also be
    final folder containing the output files.

- ext (str, Optional): The extension (including `.`) that is required
    in the output filename to be picked up for image sequence
    publishing.

- expectedFiles (list or dict): explained below
Source code in client/ayon_deadline/plugins/publish/global/submit_publish_job.py
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
class ProcessSubmittedJobOnFarm(pyblish.api.InstancePlugin,
                                publish.AYONPyblishPluginMixin,
                                publish.ColormanagedPyblishPluginMixin):
    """Process Job submitted on farm.

    These jobs are dependent on a deadline job
    submission prior to this plug-in.

    It creates dependent job on farm publishing rendered image sequence.

    Options in instance.data:
        - deadlineSubmissionJob (dict, Required): The returned .json
          data from the job submission to deadline.

        - outputDir (str, Required): The output directory where the metadata
            file should be generated. It's assumed that this will also be
            final folder containing the output files.

        - ext (str, Optional): The extension (including `.`) that is required
            in the output filename to be picked up for image sequence
            publishing.

        - expectedFiles (list or dict): explained below

    """

    label = "Submit Image Publishing job to Deadline"
    order = pyblish.api.IntegratorOrder + 0.2
    icon = "tractor"

    targets = ["local"]

    families = ["deadline.submit.publish.job"]
    settings_category = "deadline"

    aov_filter = [
        {
            "name": "maya",
            "value": [r".*([Bb]eauty).*"]
        },
        {
            "name": "blender",
            "value": [r".*([Bb]eauty).*"]
        },
        {
            # for everything from AE
            "name": "aftereffects",
            "value": [r".*"]
        },
        {
            "name": "harmony",
            "value": [r".*"]
        },
        {
            "name": "celaction",
            "value": [r".*"]
        },
        {
            "name": "max",
            "value": [r".*"]
        },
    ]

    # custom deadline attributes
    deadline_department = ""
    deadline_pool = ""
    deadline_group = ""
    deadline_priority = None

    # regex for finding frame number in string
    R_FRAME_NUMBER = re.compile(r'.+\.(?P<frame>[0-9]+)\..+')

    # mapping of instance properties to be transferred to new instance
    #     for every specified family
    instance_transfer = {
        "slate": ["slateFrames", "slate"],
        "review": ["lutPath"],
        "render2d": ["bakingNukeScripts", "version"],
        "renderlayer": ["convertToScanline"]
    }

    # list of family names to transfer to new family if present
    families_transfer = ["render3d", "render2d", "slate"]

    # poor man exclusion
    skip_integration_repre_list = []

    def _submit_deadline_post_job(
        self, instance, render_job, instances, rootless_metadata_path
    ):
        """Submit publish job to Deadline.

        Returns:
            (str): deadline_publish_job_id
        """
        data = instance.data.copy()
        product_name = data["productName"]
        job_name = "Publish - {}".format(product_name)

        context = instance.context
        anatomy = context.data["anatomy"]

        # instance.data.get("productName") != instances[0]["productName"]
        # 'Main' vs 'renderMain'
        override_version = None
        instance_version = instance.data.get("version")  # take this if exists
        if instance_version != 1:
            override_version = instance_version

        output_dir = self._get_publish_folder(
            anatomy,
            deepcopy(instance.data["anatomyData"]),
            instance.data.get("folderEntity"),
            instances[0]["productName"],
            context,
            instances[0]["productType"],
            override_version
        )

        environment = get_instance_job_envs(instance)
        environment.update(JobType.PUBLISH.get_job_env())

        priority = (
            self.deadline_priority
            or instance.data.get("priority", 50)
        )

        batch_name = self._get_batch_name(instance, render_job)
        username = self._get_username(instance, render_job)
        dependency_ids = self._get_dependency_ids(instance, render_job)

        args = [
            "--headless",
            "publish",
            rootless_metadata_path,
            "--targets", "deadline",
            "--targets", "farm",
        ]
        # TODO remove settings variant handling when not needed anymore
        #   which should be when package.py defines 'core>1.1.1' .
        settings_variant = os.environ["AYON_DEFAULT_SETTINGS_VARIANT"]
        if settings_variant == "staging":
            args.append("--use-staging")
        elif settings_variant != "production":
            args.extend(["--bundle", settings_variant])

        server_name = instance.data["deadline"]["serverName"]
        self.log.debug("Submitting Deadline publish job ...")

        deadline_addon: DeadlineAddon = (
            context.data["ayonAddonsManager"]["deadline"]
        )

        job_info = instance.data["deadline"]["job_info"]
        job_info = DeadlineJobInfo(
            Name=job_name,
            BatchName=batch_name,
            Department=self.deadline_department,
            Priority=priority,
            InitialStatus=job_info.publish_job_state,
            Group=self.deadline_group,
            Pool=self.deadline_pool or None,
            JobDependencies=dependency_ids,
            UserName=username,
            Comment=context.data.get("comment"),
        )
        if output_dir:
            job_info.OutputDirectory.append(output_dir)

        job_info.EnvironmentKeyValue.update(environment)
        return deadline_addon.submit_ayon_plugin_job(
            server_name,
            args,
            job_info
        )["response"]["_id"]

    def _get_batch_name(self, instance, render_job):
        batch_name = instance.data.get("jobBatchName")
        if not batch_name and render_job:
            batch_name = render_job["Props"]["Batch"]

        if not batch_name:
            batch_name = os.path.splitext(os.path.basename(
                instance.context.data["currentFile"]
            ))[0]
        return batch_name

    def _get_username(self, instance, render_job):
        username = None
        if render_job:
            username = render_job["Props"]["User"]

        if not username:
            username = instance.context.data.get(
                "deadlineUser", getpass.getuser()
            )
        return username

    def _get_dependency_ids(self, instance, render_job):
        # Collect dependent jobs
        if instance.data.get("tileRendering"):
            self.log.info("Adding tile assembly jobs as dependencies...")
            return instance.data.get("assemblySubmissionJobs")

        if instance.data.get("bakingSubmissionJobs"):
            self.log.info(
                "Adding baking submission jobs as dependencies..."
            )
            return instance.data["bakingSubmissionJobs"]

        if render_job and render_job.get("_id"):
            return [render_job["_id"]]
        return None

    def process(self, instance):
        # type: (pyblish.api.Instance) -> None
        """Process plugin.

        Detect type of render farm submission and create and post dependent
        job in case of Deadline. It creates json file with metadata needed for
        publishing in directory of render.

        Args:
            instance (pyblish.api.Instance): Instance data.

        """
        if not instance.data.get("farm"):
            self.log.debug("Skipping local instance.")
            return

        anatomy = instance.context.data["anatomy"]

        instance_skeleton_data = create_skeleton_instance(
            instance, families_transfer=self.families_transfer,
            instance_transfer=self.instance_transfer)
        """
        if content of `expectedFiles` list are dictionaries, we will handle
        it as list of AOVs, creating instance for every one of them.

        Example:
        --------

        expectedFiles = [
            {
                "beauty": [
                    "foo_v01.0001.exr",
                    "foo_v01.0002.exr"
                ],

                "Z": [
                    "boo_v01.0001.exr",
                    "boo_v01.0002.exr"
                ]
            }
        ]

        This will create instances for `beauty` and `Z` product
        adding those files to their respective representations.

        If we have only list of files, we collect all file sequences.
        More then one doesn't probably make sense, but we'll handle it
        like creating one instance with multiple representations.

        Example:
        --------

        expectedFiles = [
            "foo_v01.0001.exr",
            "foo_v01.0002.exr",
            "xxx_v01.0001.exr",
            "xxx_v01.0002.exr"
        ]

        This will result in one instance with two representations:
        `foo` and `xxx`
        """
        do_not_add_review = False
        if instance.data.get("review") is False:
            self.log.debug("Instance has review explicitly disabled.")
            do_not_add_review = True

        aov_filter = {
            item["name"]: item["value"]
            for item in self.aov_filter
        }
        if isinstance(instance.data.get("expectedFiles")[0], dict):
            instances = create_instances_for_aov(
                instance, instance_skeleton_data,
                aov_filter,
                self.skip_integration_repre_list,
                do_not_add_review
            )
        else:
            representations = prepare_representations(
                instance_skeleton_data,
                instance.data.get("expectedFiles"),
                anatomy,
                aov_filter,
                self.skip_integration_repre_list,
                do_not_add_review,
                instance.context,
                self,
                instance.data["deadline"]["job_info"].Frames
            )

            if "representations" not in instance_skeleton_data.keys():
                instance_skeleton_data["representations"] = []

            # add representation
            instance_skeleton_data["representations"] += representations
            instances = [instance_skeleton_data]

        # attach instances to product
        if instance.data.get("attachTo"):
            instances = attach_instances_to_product(
                instance.data.get("attachTo"), instances
            )

        r''' SUBMiT PUBLiSH JOB 2 D34DLiN3
          ____
        '     '            .---.  .---. .--. .---. .--..--..--..--. .---.
        |     |   --= \   |  .  \/   _|/    \|  .  \  ||  ||   \  |/   _|
        | JOB |   --= /   |  |  ||  __|  ..  |  |  |  |;_ ||  \   ||  __|
        |     |           |____./ \.__|._||_.|___./|_____|||__|\__|\.___|
        ._____.

        '''

        render_job = instance.data.pop("deadlineSubmissionJob", None)
        if not render_job and instance.data.get("tileRendering") is False:
            raise AssertionError(
                "Cannot continue without valid Deadline submission."
            )

        # Transfer the environment from the original job to this dependent
        # job so they use the same environment
        metadata_path, rootless_metadata_path = create_metadata_path(
            instance, anatomy
        )

        deadline_publish_job_id = self._submit_deadline_post_job(
            instance, render_job, instances, rootless_metadata_path
        )

        # Inject deadline url to instances to query DL for job id for overrides
        for inst in instances:
            inst["deadline"] = deepcopy(instance.data["deadline"])
            inst["deadline"].pop("job_info")

        # publish job file
        publish_job = {
            "folderPath": instance_skeleton_data["folderPath"],
            "frameStart": instance_skeleton_data["frameStart"],
            "frameEnd": instance_skeleton_data["frameEnd"],
            "fps": instance_skeleton_data["fps"],
            "source": instance_skeleton_data["source"],
            "user": instance.context.data["user"],
            "intent": instance.context.data.get("intent"),
            "comment": instance.context.data.get("comment"),
            "job": render_job or None,
            "instances": instances
        }

        # Note that a version of 0 is a valid version number,
        # so we explicitly check for `None` value
        # instance override version
        collected_version = instance.data.get("version")
        if collected_version is None:
            # workfile version
            collected_version = instance.context.data.get("version")
        if collected_version is not None:
            publish_job["version"] = collected_version

        if deadline_publish_job_id:
            publish_job["deadline_publish_job_id"] = deadline_publish_job_id

        # add audio to metadata file if available
        audio_file = instance.context.data.get("audioFile")
        if audio_file and os.path.isfile(audio_file):
            publish_job.update({"audio": audio_file})

        self.log.debug(f"Writing metadata json to '{metadata_path}'")
        with open(metadata_path, "w") as f:
            json.dump(publish_job, f, indent=4, sort_keys=True)

    def _get_publish_folder(
        self,
        anatomy,
        template_data,
        folder_entity,
        product_name,
        context,
        product_type,
        version=None
    ):
        """
            Extracted logic to pre-calculate real publish folder, which is
            calculated in IntegrateNew inside of Deadline process.
            This should match logic in:
                'collect_anatomy_instance_data' - to
                    get correct anatomy, family, version for product name and
                'collect_resources_path'
                    get publish_path

        Args:
            anatomy (ayon_core.pipeline.anatomy.Anatomy):
            template_data (dict): pre-calculated collected data for process
            folder_entity (dict[str, Any]): Folder entity.
            product_name (string): Product name (actually group name
                of product)
            product_type (string): for current deadline process it's always
                'render'
                TODO - for generic use family needs to be dynamically
                    calculated like IntegrateNew does
            version (int): override version from instance if exists

        Returns:
            Optional[str]: publish folder where rendered and published files
                will be stored based on 'publish' template

        """
        project_name = context.data["projectName"]
        host_name = context.data["hostName"]
        if not version:
            version_entity = None
            if folder_entity:
                version_entity = ayon_api.get_last_version_by_product_name(
                    project_name,
                    product_name,
                    folder_entity["id"]
                )

            if version_entity:
                version = int(version_entity["version"]) + 1
            else:
                version = get_versioning_start(
                    project_name,
                    host_name,
                    task_name=template_data["task"]["name"],
                    task_type=template_data["task"]["type"],
                    product_type="render",
                    product_name=product_name,
                    project_settings=context.data["project_settings"]
                )

        host_name = context.data["hostName"]
        task_info = template_data.get("task") or {}

        template_name = publish.get_publish_template_name(
            project_name,
            host_name,
            product_type,
            task_info.get("name"),
            task_info.get("type"),
        )

        template_data["version"] = version
        template_data["subset"] = product_name
        template_data["family"] = product_type
        template_data["product"] = {
            "name": product_name,
            "type": product_type,
        }

        render_dir_template = anatomy.get_template_item(
            "publish", template_name, "directory"
        )
        try:
            return (
                render_dir_template
                .format_strict(template_data)
                .replace("\\", "/")
            )

        except TemplateUnsolved:
            self.log.error(
                "Publish directory template is unsolved for: "
                f"{template_name} in anatomy. Output directory won't be set."
            )

process(instance)

Process plugin.

Detect type of render farm submission and create and post dependent job in case of Deadline. It creates json file with metadata needed for publishing in directory of render.

Parameters:

Name Type Description Default
instance Instance

Instance data.

required
Source code in client/ayon_deadline/plugins/publish/global/submit_publish_job.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
def process(self, instance):
    # type: (pyblish.api.Instance) -> None
    """Process plugin.

    Detect type of render farm submission and create and post dependent
    job in case of Deadline. It creates json file with metadata needed for
    publishing in directory of render.

    Args:
        instance (pyblish.api.Instance): Instance data.

    """
    if not instance.data.get("farm"):
        self.log.debug("Skipping local instance.")
        return

    anatomy = instance.context.data["anatomy"]

    instance_skeleton_data = create_skeleton_instance(
        instance, families_transfer=self.families_transfer,
        instance_transfer=self.instance_transfer)
    """
    if content of `expectedFiles` list are dictionaries, we will handle
    it as list of AOVs, creating instance for every one of them.

    Example:
    --------

    expectedFiles = [
        {
            "beauty": [
                "foo_v01.0001.exr",
                "foo_v01.0002.exr"
            ],

            "Z": [
                "boo_v01.0001.exr",
                "boo_v01.0002.exr"
            ]
        }
    ]

    This will create instances for `beauty` and `Z` product
    adding those files to their respective representations.

    If we have only list of files, we collect all file sequences.
    More then one doesn't probably make sense, but we'll handle it
    like creating one instance with multiple representations.

    Example:
    --------

    expectedFiles = [
        "foo_v01.0001.exr",
        "foo_v01.0002.exr",
        "xxx_v01.0001.exr",
        "xxx_v01.0002.exr"
    ]

    This will result in one instance with two representations:
    `foo` and `xxx`
    """
    do_not_add_review = False
    if instance.data.get("review") is False:
        self.log.debug("Instance has review explicitly disabled.")
        do_not_add_review = True

    aov_filter = {
        item["name"]: item["value"]
        for item in self.aov_filter
    }
    if isinstance(instance.data.get("expectedFiles")[0], dict):
        instances = create_instances_for_aov(
            instance, instance_skeleton_data,
            aov_filter,
            self.skip_integration_repre_list,
            do_not_add_review
        )
    else:
        representations = prepare_representations(
            instance_skeleton_data,
            instance.data.get("expectedFiles"),
            anatomy,
            aov_filter,
            self.skip_integration_repre_list,
            do_not_add_review,
            instance.context,
            self,
            instance.data["deadline"]["job_info"].Frames
        )

        if "representations" not in instance_skeleton_data.keys():
            instance_skeleton_data["representations"] = []

        # add representation
        instance_skeleton_data["representations"] += representations
        instances = [instance_skeleton_data]

    # attach instances to product
    if instance.data.get("attachTo"):
        instances = attach_instances_to_product(
            instance.data.get("attachTo"), instances
        )

    r''' SUBMiT PUBLiSH JOB 2 D34DLiN3
      ____
    '     '            .---.  .---. .--. .---. .--..--..--..--. .---.
    |     |   --= \   |  .  \/   _|/    \|  .  \  ||  ||   \  |/   _|
    | JOB |   --= /   |  |  ||  __|  ..  |  |  |  |;_ ||  \   ||  __|
    |     |           |____./ \.__|._||_.|___./|_____|||__|\__|\.___|
    ._____.

    '''

    render_job = instance.data.pop("deadlineSubmissionJob", None)
    if not render_job and instance.data.get("tileRendering") is False:
        raise AssertionError(
            "Cannot continue without valid Deadline submission."
        )

    # Transfer the environment from the original job to this dependent
    # job so they use the same environment
    metadata_path, rootless_metadata_path = create_metadata_path(
        instance, anatomy
    )

    deadline_publish_job_id = self._submit_deadline_post_job(
        instance, render_job, instances, rootless_metadata_path
    )

    # Inject deadline url to instances to query DL for job id for overrides
    for inst in instances:
        inst["deadline"] = deepcopy(instance.data["deadline"])
        inst["deadline"].pop("job_info")

    # publish job file
    publish_job = {
        "folderPath": instance_skeleton_data["folderPath"],
        "frameStart": instance_skeleton_data["frameStart"],
        "frameEnd": instance_skeleton_data["frameEnd"],
        "fps": instance_skeleton_data["fps"],
        "source": instance_skeleton_data["source"],
        "user": instance.context.data["user"],
        "intent": instance.context.data.get("intent"),
        "comment": instance.context.data.get("comment"),
        "job": render_job or None,
        "instances": instances
    }

    # Note that a version of 0 is a valid version number,
    # so we explicitly check for `None` value
    # instance override version
    collected_version = instance.data.get("version")
    if collected_version is None:
        # workfile version
        collected_version = instance.context.data.get("version")
    if collected_version is not None:
        publish_job["version"] = collected_version

    if deadline_publish_job_id:
        publish_job["deadline_publish_job_id"] = deadline_publish_job_id

    # add audio to metadata file if available
    audio_file = instance.context.data.get("audioFile")
    if audio_file and os.path.isfile(audio_file):
        publish_job.update({"audio": audio_file})

    self.log.debug(f"Writing metadata json to '{metadata_path}'")
    with open(metadata_path, "w") as f:
        json.dump(publish_job, f, indent=4, sort_keys=True)

get_resource_files(resources, frame_range=None)

Get resource files at given path.

If frame_range is specified those outside will be removed.

Parameters:

Name Type Description Default
resources list

List of resources

required
frame_range list

Frame range to apply override

None

Returns:

Type Description

list of str: list of collected resources

Source code in client/ayon_deadline/plugins/publish/global/submit_publish_job.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def get_resource_files(resources, frame_range=None):
    """Get resource files at given path.

    If `frame_range` is specified those outside will be removed.

    Arguments:
        resources (list): List of resources
        frame_range (list): Frame range to apply override

    Returns:
        list of str: list of collected resources

    """
    res_collections, _ = clique.assemble(resources)
    assert len(res_collections) == 1, "Multiple collections found"
    res_collection = res_collections[0]

    # Remove any frames
    if frame_range is not None:
        for frame in frame_range:
            if frame not in res_collection.indexes:
                continue
            res_collection.indexes.remove(frame)

    return list(res_collection)