<workflow-app name="CROSSREF DUMP - DOWNLOAD" xmlns="uri:oozie:workflow:0.5">
    <parameters>
        <property>
            <name>sparkDriverMemory</name>
            <description>memory for driver process</description>
        </property>
        <property>
            <name>sparkExecutorMemory</name>
            <description>memory for individual executor</description>
        </property>
        <property>
            <name>sparkExecutorCores</name>
            <description>number of cores used by single executor</description>
        </property>

        <!-- Crossref Parameters -->
        <property>
            <name>crossrefdumpfilename</name>
            <description>the Crossref input path</description>
        </property>
        <property>
            <name>crossrefDumpPath</name>
            <description>the Crossref dump path</description>
        </property>
        <property>
            <name>crossrefdumptoken</name>
            <description>the token for the API dump path</description>
        </property>


    </parameters>

    <global>
        <job-tracker>${jobTracker}</job-tracker>
        <name-node>${nameNode}</name-node>
        <configuration>
            <property>
                <name>oozie.action.sharelib.for.spark</name>
                <value>${oozieActionShareLibForSpark2}</value>
            </property>
        </configuration>
    </global>

    <start to="resume_from"/>

    <decision name="resume_from">
        <switch>
            <case to="ImportCrossRef">${wf:conf('resumeFrom') eq 'ImportCrossRef'}</case>
            <case to="UnpackCrossrefEntries">${wf:conf('resumeFrom') eq 'Unpack'}</case>
            <default to="removeFiles"/> <!-- first action to be done when downloadDump is to be performed -->
        </switch>
    </decision>

    <kill name="Kill">
        <message>Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>

    <action name="removeFiles">
        <fs>
            <delete path="${crossrefDumpPath}/${crossrefdumpfilename}"/>
            <delete path="${crossrefDumpPath}/files"/>
            <delete path="${crossrefDumpPath}/crossref_unpack"/>
        </fs>
        <ok to="DownloadDump"/>
        <error to="Kill"/>
    </action>


    <action name="DownloadDump">
        <shell xmlns="uri:oozie:shell-action:0.2">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <configuration>
                <property>
                    <name>mapred.job.queue.name</name>
                    <value>${queueName}</value>
                </property>
            </configuration>
            <exec>download.sh</exec>
            <argument>${url}</argument>
            <argument>${crossrefDumpPath}</argument>
            <argument>${crossrefdumpfilename}</argument>
            <argument>${crossrefdumptoken}</argument>
            <env-var>HADOOP_USER_NAME=${wf:user()}</env-var>
            <file>download.sh</file>
            <capture-output/>
        </shell>
        <ok to="ImportCrossRef"/>
        <error to="Kill"/>
    </action>

    <action name="ImportCrossRef">
        <java>
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <main-class>eu.dnetlib.doiboost.crossref.ExtractCrossrefRecords</main-class>
            <arg>--hdfsServerUri</arg><arg>${nameNode}</arg>
            <arg>--crossrefFileNameTarGz</arg><arg>${crossrefdumpfilename}</arg>
            <arg>--workingPath</arg><arg>${crossrefDumpPath}</arg>
            <arg>--outputPath</arg><arg>${crossrefDumpPath}/files/</arg>
        </java>
        <ok to="UnpackCrossrefEntries"/>
        <error to="Kill"/>
    </action>

    <action name="UnpackCrossrefEntries">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>SparkUnpackCrossrefEntries</name>
            <class>eu.dnetlib.doiboost.crossref.UnpackCrtossrefEntries</class>
            <jar>dhp-doiboost-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.sql.shuffle.partitions=3840
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
            </spark-opts>
            <arg>--master</arg><arg>yarn-cluster</arg>
            <arg>--sourcePath</arg><arg>${crossrefDumpPath}/files</arg>
            <arg>--targetPath</arg><arg>${crossrefDumpPath}/crossref_unpack/</arg>

        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>



    <end name="End"/>
</workflow-app>