Rolling back service invokation when i get bad response

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Rolling back service invokation when i get bad response

sally
I want to make remote service invokation in nifi and i want to make one flowfile for each startDate reponse here is my ecexutescript code for it, but i want to konw how can i prevent service invokation as soon as i receve bad request for example if service doesn't have data for startDate=2017-07-07, I want to log this flowfile and then roll back service invokation for date=2017-07-08 , how can i do it?


var flowFile = session.get();
function addDays(date, days) {
    var result =new Date(date);
    result.setDate(result.getDate() + days);
    var dateFormated = result.toISOString().substr(0,10);
    return formatDate(dateFormated);
}
function removeDays(date, days) {
    var result =new Date(date);
    result.setDate(result.getDate() - days);
    var dateFormated = result.toISOString().substr(0,10);
    return formatDate(dateFormated);
}
 
 
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();
 
    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
 
    return [year, month, day].join('-');
}
 
if (flowFile != null) {
    var startDate= flowFile.getAttribute('startDate')
    var endDate= flowFile.getAttribute('endDate')
    var parameter=flowFile.getAttribute("parameter")
    var dateToStart=new Date(startDate);
    var dateToEnd=new Date(endDate);
   
 
 
 
    var difference = dateToEnd- dateToStart;
 
    for(var i=0;i<difference;i++) {
        var flowFile1 = session.create();
        dateToEnd = addDays(dateToStart, 1);
 
        var flowFile1 = session.putAttribute(flowFile1, 'filename', formatDate(dateToStart));
 
        var command = '<soapenv:Envelope xmlns:soapenv="url" xmlns:tem="name">' +
            '<soapenv:Header/>' +
            '<soapenv:Body>' +
            '<tem:Data>' +
            '<tem:startDate>' + formatDate(new Date(dateToStart)) + '</tem:startDate>' +
            '<tem:endDate>' + formatDate(new Date(dateToStart)) + '</tem:endDate>' +
            '</tem:Data>' +
            '</soapenv:Body>' +
            '</soapenv:Envelope>';
 
        flowFile1 = session.write(flowFile1,
            new OutputStreamCallback(function (outputStream) {
                outputStream.write(command.getBytes(StandardCharsets.UTF_8))
            }));
 
        session.transfer(flowFile1, REL_SUCCESS)
 
        dateToStart = dateToEnd;
    }
    session.remove(flowFile)
 
 
}