我怎么能转换后台作业,以类似的功能呢? [英] How can I convert background jobs to something like functions?

查看:137
本文介绍了我怎么能转换后台作业,以类似的功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个新闻阅读器应用程序,并使用Parse.com后台作业,以收集报纸的RSS源的链接。我已经使用xmlreader.js和sax.js解析HTT presponse并使用白水回收和beforeSave,在浏览器中的数据定期更新的类。

我有多个类别共有超过30双进行多重报纸,(我会在以后加入更多对作为,我想包括地方报纸)。到现在我与一家报纸和一类工作 - 印度教,体育类;而现在是工作的罚款。使得这两种功能的副本,并创造就业机会不会是有效的,我认为。

所以,我想问如果我能这两个工作,beforeSave转换成某种函数,这样我就可以通过在任何报纸类对类名称或网址自动完成的东西。

全部code - main.js

工作 -

  Parse.Cloud.job(job_hindu_sports功能(请求,响应){
返回Parse.Cloud.htt prequest({
    网址:'http://www.thehindu.com/sport/?service=rss
}),然后(功能(HTT presponse){
    VAR someXml = HTT presponse.text;
    xmlreader.read(someXml,函数(ERR,RES){
        如果(ERR){
            response.error(错误+ ERR);
            返回的console.log(ERR);
        }        变种listArray = [];
        res.rss.channel.item.each(函数(一,项目){
            VAR hinduSports =新HinduSports(); // @ startswithaj - 这部分
            hinduSports.set(链接,item.link.text());
            hinduSports.set(称号,item.title.text());
            hinduSports.set(pubdate的,item.pubDate.text());
            //console.log(\"pubDate - + item.pubDate.text());
            listArray.push(hinduSports);
        });        VAR承诺= [];
        Parse.Object.saveAll(listArray,{
                成功:函数(OBJ文件){
                    promises.push(OBJ文件);
                    的console.log(拯救了!);
                },
                错误:功能(错误){
                    的console.log(错误,同时节省 - +误差);
                }
            });
        返回Parse.Promise.when(承诺);    });
})。然后(函数(){
        response.success(保存成功完成。);
        },功能(错误){
        response.error(嗯哦,出事了。);
});
});

beforeSave -

  Parse.Cloud.beforeSave(HinduSports功能(请求,响应){
//console.log(\"in beforeSave);
VAR的查询=新Parse.Query(HinduSports);
VAR LINKTEXT = request.object.get(链接)
VAR titleText = request.object.get(标题);
query.equalTo(链接,LINKTEXT);
query.first({
  成功:函数(对象){
    //console.log(\"in查询);
    如果(对象){
        //console.log(\"found);
        如果(object.get('标题')!== titleText){
            的console.log(称号不一样);
            object.set(称号,titleText);
            response.success();
        }
        其他{
            的console.log(称号一样);
            response.error();
        }
    }其他{
        的console.log(找不到);
        response.success();
    }
  },
  错误:功能(错误){
    response.error();
  }
});
});


解决方案

在你的工作code可以查询数据存储为所有要处理的URL,然后遍历这些结果,要求每个URL和传球在HTT presponse到完成所有的工作。

函数

所以,你会(伪code)

 函数getDataForNewspaper(ID,网址){
    回报(函数(ID){
        Parse.Cloud.htt prequest({
            URL:
        }),然后(功能(HTT presponse){
           processDataForNewspaper(ID,HTT presponse)
        })
    })(ID)//你需要有这样一个封闭,所以你可以通过ID来processDataFor ...功能processDataforNewpaper(ID,HTT presponse){
  someXml = HTT presponse.text
  //过程你这里的xml
}Parse.Cloud.job(get_data_for_all_newspapers功能(请求,响应){
    VAR的查询=新Parse.Query(让所有的报纸)。找到{
      成功:函数(名单){
         在列表中的每个报纸则
              getDataForNewspaper(newspaper.id,newspaper.url)
      }
    }
}

这是不是最好的解释,但我希望这有助于

I am making a newsreader app and using Parse.com background jobs to collect links from RSS feed of the newspaper. I have used xmlreader.js and sax.js to parse the httpResponse and using saveAll and beforeSave, periodically update the classes in data browser.

I have multiple newspapers with multiple categories making a total of more than 30 pairs, (I would have to later include more pair as I would like to include regional newspapers). Till now I was working with one newspaper and one category - The Hindu, sports category; and it is now working fine. Making copies of these two function and create jobs wont be efficient I think.

Therefore, I wanted to ask if I can convert both these jobs and beforeSave into some kind of function so that I can just pass in either newspaper-category pair class name or its url to do the stuff automatically.

Full Code - main.js

job -

Parse.Cloud.job("job_hindu_sports", function (request, response) {
return Parse.Cloud.httpRequest({
    url: 'http://www.thehindu.com/sport/?service=rss'
}).then(function(httpResponse) {
    var someXml = httpResponse.text;
    xmlreader.read(someXml, function (err, res){
        if(err) {
            response.error("Error " +err);
            return console.log(err);
        }   

        var listArray = [];
        res.rss.channel.item.each(function (i, item){
            var hinduSports = new HinduSports(); //@startswithaj - this part
            hinduSports.set("link", item.link.text());
            hinduSports.set("title", item.title.text());
            hinduSports.set("pubDate", item.pubDate.text());
            //console.log("pubDate - "+ item.pubDate.text());
            listArray.push(hinduSports);
        });

        var promises = [];
        Parse.Object.saveAll(listArray, {
                success: function(objs) {
                    promises.push(objs);
                    console.log("SAVED ALL!");
                },
                error: function(error) { 
                    console.log("ERROR WHILE SAVING - "+error);
                }   
            });
        return Parse.Promise.when(promises);        

    });
}).then(function() {
        response.success("Saving completed successfully.");
        },function(error) {
        response.error("Uh oh, something went wrong.");
});
});

beforeSave -

Parse.Cloud.beforeSave("HinduSports", function(request, response) {
//console.log("in beforeSave");
var query = new Parse.Query(HinduSports);
var linkText = request.object.get("link")
var titleText = request.object.get("title");
query.equalTo("link", linkText);
query.first({
  success: function(object) {
    //console.log("in query");
    if (object) {
        //console.log("found");
        if(object.get('title')!==titleText){
            console.log("title not same");
            object.set("title", titleText);
            response.success();
        }
        else{
            console.log("title same");
            response.error();
        }
    } else {
        console.log("not found");
        response.success();
    }
  },
  error: function(error) {
    response.error();
  }
});
});

解决方案

In your job code you could query your datastore for all of the URLS you want to process, and then iterate through the results requesting each url and passing the httpresponse to a function that does all the work

So you would have (pseudo code)

function getDataForNewspaper(id, url){
    return (function(id) {
        Parse.Cloud.httpRequest({
            url: url
        }).then(function(httpResponse){
           processDataForNewspaper(id, httpResponse)
        })
    })(id) //you need to have this in a closure so you can pass id to processDataFor...

function processDataforNewpaper(id, httpResponse){
  someXml = httpResponse.text
  //process your xml here 
}

Parse.Cloud.job("get_data_for_all_newspapers", function (request, response) {
    var query = new Parse.Query("Get all the newspapers").find{
      success: function(list){ 
         for each newspaper in list then 
              getDataForNewspaper(newspaper.id, newspaper.url)  
      }
    }
}

It's not the best explanation but I hope this helps

这篇关于我怎么能转换后台作业,以类似的功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆