Skip to content

Commit

Permalink
fixing syncing multiple videos at near same time
Browse files Browse the repository at this point in the history
  • Loading branch information
dcolvin committed Oct 3, 2024
1 parent 4e00e8d commit 182c855
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,9 @@ private void setImages(JSONObject objObject, Asset newAsset) {
//Map<String,Object> rendition_map = new HashMap<String,Object>();
newAsset.addRendition(Constants.BRC_THUMBNAIL_PNG, ris, StandardImageHandler.PNG1_MIMETYPE);
} else {
newAsset.removeRendition(Constants.BRC_THUMBNAIL_PNG);
if (newAsset.getRendition(Constants.BRC_THUMBNAIL_PNG) != null) {
newAsset.removeRendition(Constants.BRC_THUMBNAIL_PNG);
}
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ public static boolean uploadToUrl(URL url, InputStream inputStream) {

if (responseCode == 200) {
LOGGER.info("Successfully uploaded.");
} else {
LOGGER.error("***************************S3UploadUtil: Error uploading asset: " + responseCode);
}
} catch (IOException e) {
LOGGER.error("IOException",e);
} catch (Exception e) {
LOGGER.error("***************************S3UploadUtil: Error uploading asset");
}
return responseCode == 200;

Expand Down Expand Up @@ -172,9 +176,13 @@ public static boolean uploadToUrl(URL url, InputStream inputStream, String proxy

if (responseCode == 200) {
LOGGER.info("Successfully uploaded.");
} else {
LOGGER.error("***************************S3UploadUtil: Error uploading asset: " + responseCode);
}
} catch (IOException e) {
LOGGER.error("IOException",e);
} catch (Exception e) {
LOGGER.error("***************************S3UploadUtil: Error uploading asset");
}
return responseCode == 200;

Expand Down Expand Up @@ -224,9 +232,13 @@ public static boolean uploadToUrl(URL url, InputStream inputStream, Proxy proxy)

if (responseCode == 200) {
LOGGER.info("Successfully uploaded.");
} else {
LOGGER.error("***************************S3UploadUtil: Error uploading asset: " + responseCode);
}
} catch (IOException e) {
LOGGER.error("IOException",e);
} catch (Exception e) {
LOGGER.error("***************************S3UploadUtil: Error uploading asset");
}
return responseCode == 200;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@
import java.util.concurrent.TimeUnit;

import javax.jcr.AccessDeniedException;
import javax.jcr.InvalidItemStateException;
import javax.jcr.ItemExistsException;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.ReferentialIntegrityException;
import javax.jcr.RepositoryException;
import javax.jcr.ValueFormatException;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.version.VersionException;

import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
Expand All @@ -60,7 +67,7 @@ public class BrightcoveSyncAssetWorkflowStep implements WorkflowProcess{

private static final Logger LOG = LoggerFactory.getLogger(BrightcoveSyncAssetWorkflowStep.class);
private static final String SERVICE_ACCOUNT_IDENTIFIER = "brightcoveWrite";
private String brightcoveAssetId;

private Map<String, String> paths = null;

@Override
Expand All @@ -70,6 +77,7 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData
LOG.info("********************* payloadpath:" + payloadPath);

ResourceResolver rr = null;
String brightcoveAssetId = null;

try {

Expand Down Expand Up @@ -119,10 +127,10 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData
Asset _asset = assetResource.adaptTo(Asset.class);
ServiceUtil serviceUtil = new ServiceUtil(brightcoveAccountId);
// upload or modify the asset
activateAsset(rr, _asset, serviceUtil);
brightcoveAssetId = activateAsset(rr, _asset, serviceUtil);
TimeUnit.SECONDS.sleep(15);
if (brightcoveAssetId != null && !brightcoveAssetId.isEmpty()) {
syncBrightcoveData(serviceUtil, _asset, rr, brightcoveAccountId);
syncBrightcoveData(brightcoveAssetId, serviceUtil, _asset, rr, brightcoveAccountId);
}
rr.commit();

Expand All @@ -135,12 +143,12 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData
} catch (LoginException e) {

// there is some issue with the system user used
LOG.error("There was an error using the Brightcove system user.");
LOG.error("*************************** There was an error using the Brightcove system user.");

} catch (Exception e) {

// a general error
LOG.error("Error when handling the Brightcove video sync: {}", e.getMessage());
LOG.error("*************************** Error when handling the Brightcove video sync: {}", e.getMessage());

} finally {
if (rr != null && rr.isLive()) {
Expand All @@ -149,7 +157,7 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData
}
}

private void syncBrightcoveData(ServiceUtil serviceUtil, Asset _asset, ResourceResolver rr, String brightcoveAccountId) {
private void syncBrightcoveData(String brightcoveAssetId, ServiceUtil serviceUtil, Asset _asset, ResourceResolver rr, String brightcoveAccountId) {

try {
JSONObject result = serviceUtil.getSelectedVideo(brightcoveAssetId);
Expand All @@ -161,10 +169,12 @@ private void syncBrightcoveData(ServiceUtil serviceUtil, Asset _asset, ResourceR
}
}

private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) {
private String activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) {

LOG.trace("brc_lastsync was null or zero : asset should be initialized");
LOG.info("activate new asset");
String brightcoveAssetId = null;

try {

// get the binary
Expand Down Expand Up @@ -195,21 +205,25 @@ private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, Mod

// log the error
LOG.error(Constants.REP_ACTIVATION_SUCCESS_TMPL, _asset.getName());

LOG.error("*************************** Error sending data to Brightcove: {}", _asset.getName());

}

} catch (Exception e) {

LOG.error("Error: {}", e.getMessage());

}

return brightcoveAssetId;

}

private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video,
private String activateModified(Asset _asset, ServiceUtil serviceUtil, Video video,
ModifiableValueMap brc_lastsync_map) {

LOG.info("Entering activateModified()");
String brightcoveAssetId = null;

try {

Expand All @@ -220,7 +234,7 @@ private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video

boolean sent = api_resp.getBoolean(Constants.SENT);
if (sent) {

brightcoveAssetId = api_resp.getString(Constants.VIDEOID);
LOG.info("Brightcove video updated successfully: {}", _asset.getPath());
serviceUtil.updateRenditions(_asset, video);
LOG.info("Updated renditions for Brightcove video: {}", _asset.getPath());
Expand All @@ -235,36 +249,54 @@ private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video
} else {

// log the error
LOG.error("Error sending data to Brightcove: {}", _asset.getName());

LOG.error("*************************** Error sending data to Brightcove: {}", _asset.getName());
}
} catch (Exception e) {

// log the error
LOG.error("General Error: {}", _asset.getName());
LOG.error("*************************** Error sending data to Brightcove: {}", _asset.getName());

}

return brightcoveAssetId;

}

private void syncFolder(ServiceUtil serviceUtil, JSONObject api_resp, Node assetNode) {
try {
LOG.trace("CHECKING PARENT FOR BRC_FOLDER_ID: " + assetNode.getParent().getPath());
Node parentNode = assetNode.getParent();
String videoId = api_resp.getString(Constants.VIDEOID);

if (!parentNode.hasProperty("brc_folder_id")) {
String folderId = serviceUtil.createFolder(assetNode.getParent().getName());
if (folderId != null && !folderId.isEmpty()) {

parentNode.setProperty("brc_folder_id", folderId);
parentNode.getSession().save();
LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + folderId + "'");
serviceUtil.moveVideoToFolder(folderId, api_resp.getString(Constants.VIDEOID));
setFolderIdMoveAssetInBC(serviceUtil, parentNode, videoId, folderId);
} else {
LOG.error("*************************** No folder created ***************************");
TimeUnit.SECONDS.sleep(15);
parentNode.refresh(false);
if (!parentNode.hasProperty("brc_folder_id")) {
folderId = serviceUtil.createFolder(assetNode.getParent().getName());
if (folderId != null && !folderId.isEmpty()) {
setFolderIdMoveAssetInBC(serviceUtil, parentNode, videoId, folderId);
} else {
LOG.error("*************************** No folder created attempt 2 ***************************");
}
} else {
// this is in a subfolder so we need to formally move the asset to this folder
String brc_folder_id = parentNode.getProperty("brc_folder_id").getString();
LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + brc_folder_id + "'");
serviceUtil.moveVideoToFolder(brc_folder_id, videoId);
}
}
} else {
// this is in a subfolder so we need to formally move the asset to this folder
String brc_folder_id = assetNode.getParent().getProperty("brc_folder_id").getString();
String brc_folder_id = parentNode.getProperty("brc_folder_id").getString();
LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + brc_folder_id + "'");
serviceUtil.moveVideoToFolder(brc_folder_id, api_resp.getString(Constants.VIDEOID));
serviceUtil.moveVideoToFolder(brc_folder_id, videoId);
}


Expand All @@ -276,27 +308,35 @@ private void syncFolder(ServiceUtil serviceUtil, JSONObject api_resp, Node asset
}
}

private void activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil serviceUtil) {
private void setFolderIdMoveAssetInBC(ServiceUtil serviceUtil, Node parentNode, String videoId, String folderId) throws Exception {
parentNode.setProperty("brc_folder_id", folderId);
parentNode.getSession().save();

LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + folderId + "'");
serviceUtil.moveVideoToFolder(folderId, videoId);
}

private String activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil serviceUtil) {

// need to either activate a new asset or an updated existing
// ServiceUtil serviceUtil = new ServiceUtil(accountId);
String path = _asset.getPath();

String brightcoveAssetId = null;
Video video = serviceUtil.createVideo(path, _asset, "ACTIVE");
Resource assetRes = _asset.adaptTo(Resource.class);

if (assetRes == null) {
return;
return "";
}

Resource metadataRes = assetRes.getChild(Constants.ASSET_METADATA_PATH);
if (metadataRes == null) {
return;
return "";
}

ModifiableValueMap brc_lastsync_map = metadataRes.adaptTo(ModifiableValueMap.class);
if (brc_lastsync_map == null) {
return;
return "";
}

Long jcr_lastmod = _asset.getLastModified();
Expand All @@ -308,13 +348,15 @@ private void activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil servic

// we need to activate a new asset here
LOG.info("Activating New Brightcove Asset: {}", _asset.getPath());
activateNew(_asset, serviceUtil, video, brc_lastsync_map);
brightcoveAssetId = activateNew(_asset, serviceUtil, video, brc_lastsync_map);

} else {
// we need to modify an existing asset here
LOG.info("Activating Modified Brightcove Asset: {}", _asset.getPath());
activateModified(_asset, serviceUtil, video, brc_lastsync_map);
brightcoveAssetId = activateModified(_asset, serviceUtil, video, brc_lastsync_map);
}

return brightcoveAssetId;

}
}

0 comments on commit 182c855

Please sign in to comment.