How to let user set time to run a task in google app engine - google-app-engine

My customer wants to set time (ex: Dec 13, 16:00 pm) to run a certain task.
I dont think cron job fits for it because customer dont know how to use google app engine SDK.
Is there any other way to do it?
Thanks

You can create a task and set the time when you want this task to be executed. From the documentation:
X-AppEngine-TaskETA, the target execution time of the task, specified
in milliseconds since January 1st 1970.

You can use the task queue API: https://cloud.google.com/appengine/docs/python/taskqueue/ or even the defer API: https://cloud.google.com/appengine/articles/deferred

If your customer is a user of your application, and there may be several users requesting tasks to be executed at different times, then you can save these requests to the datastore. Create a cron job to run every hour (or however precise you need the timeframe) which checks the datastore to see if there are any tasks to run at that time - if so, run the proper script.
If this is just a one-time, or small number of tasks, you can do as Andrei suggested.

Related

VOLTTRON Csv Driver Agent scheduled task

Is there a way I could call an agent to start a certain time every day?
For example I would like to create an agent that creates an API call at 4AM everyday (to openweathermap) to check the hourly forecast for the day. Basically if the hourly forecast yield's ideal conditions the agent could do building pre-cooling... Else if the hourly weather conditions starting at 4AM through the afternoon hours are not ideal the agent would not do anything (pass). Hopefully that makes sense! just one API call at 4AM to make some decisions, else pass.
Also having some extra logic if current time equals a winter month to pass as well. datetime.datetime.now().month?
##Core.receiver("onstart")
def onstart(self, sender, **kwargs):
"""
This is method is called once the Agent has successfully connected to the platform.
This is a good place to setup subscriptions if they are not dynamic or
do any other startup activities that require a connection to the message bus.
Called after any configurations methods that are called at startup.
Usually not needed if using the configuration store.
"""
# Every 120 seconds, ask the core agent loop to run the actuate point method with no parameters
self.core.periodic(600, self.actuate_point)
def actuate_point(self):
"""
Request that the Actuator set a point on the CSV device
"""
You could use the built in cron scheduler (https://volttron.readthedocs.io/en/main/developing-volttron/developing-agents/agent-development.html?highlight=cron#periodics-and-scheduling). This uses the same style of cron that your linux system uses. Put the following decorator on your function and the specified cron will just work.
from volttron.platform.scheduling import cron
#Core.schedule(cron('0 1 * * *'))
def cron_function(self):
print("this is a cron-scheduled function")
Some useful crons are located https://crontab.guru/examples.html

NDB query().iter() of 1000<n<1500 entities is wigging out

I have a script that, using Remote API, iterates through all entities for a few models. Let's say two models, called FooModel with about 200 entities, and BarModel with about 1200 entities. Each has 15 StringPropertys.
for model in [FooModel, BarModel]:
print 'Downloading {}'.format(model.__name__)
new_items_iter = model.query().iter()
new_items = [i.to_dict() for i in new_items_iter]
print new_items
When I run this in my console, it hangs for a while after printing 'Downloading BarModel'. It hangs until I hit ctrl+C, at which point it prints the downloaded list of items.
When this is run in a Jenkins job, there's no one to press ctrl+C, so it just runs continuously (last night it ran for 6 hours before something, presumably Jenkins, killed it). Datastore activity logs reveal that the datastore was taking 5.5 API calls per second for the entire 6 hours, racking up a few dollars in GAE usage charges in the meantime.
Why is this happening? What's with the weird behavior of ctrl+C? Why is the iterator not finishing?
This is a known issue currently being tracked on the Google App Engine public issue tracker under Issue 12908. The issue was forwarded to the engineering team and progress on this issue will be discussed on said thread. Should this be affecting you, please star the issue to receive updates.
In short, the issue appears to be with the remote_api script. When querying entities of a given kind, it will hang when fetching 1001 + batch_size entities when the batch_size is specified. This does not happen in production outside of the remote_api.
Possible workarounds
Using the remote_api
One could limit the number of entities fetched per script execution using the limit argument for queries. This may be somewhat tedious but the script could simply be executed repeatedly from another script to essentially have the same effect.
Using admin URLs
For repeated operations, it may be worthwhile to build a web UI accessible only to admins. This can be done with the help of the users module as shown here. This is not really practical for a one-time task but far more robust for regular maintenance tasks. As this does not use the remote_api at all, one would not encounter this bug.

How to auto start an activiti workflow periodically in a certain schedule in Alfresco 5.0.c?

I created a custom review and approve activiti workflow. I need to start this workflow automatically in every certain period , say every 30 minutes. For this i used timer start event as below:
<startEvent id="timerStart" name="Timer start" activiti:formKey="scheduledtask:submitParallelReviewTask">
<timerEventDefinition>
<timeCycle>R5/PT30M</timeCycle>
</timerEventDefinition>
</startEvent>
This created new process instance in every 30 minutes and repetition occured 5 times as required. But in the new timer started processes' tasks , initiator and other process variables were null. Also if i set the process variables as mandatory, timer executor job failed.
How can i set the initiator and other mandatory process variables in the newly created i.e. timer auto started process instances and its respective tasks?
Please suggest how to fix these bugs.
Thank you in advance !
Well I guess for the solution you should use cron job of alfresco.
For using cron job you can find below link usefull.
https://wiki.alfresco.com/wiki/Scheduled_Actions
Use workflowService for setting parameters.

How do I execute code on App Engine without using servlets?

My goal is to receive updates for some service (using http request-response) all the time, and when I get a specific information, I want to push it to the users. This code must run all the time (let's say every 5 seconds).
How can I run some code doing this when the server is up (means, not by an http request that initiates the execution of this code) ?
I'm using Java.
Thanks
You need to use
Scheduled Tasks With Cron for Java
You can set your own schedule (e.g. every minute), and it will call a specified handler for you.
You may also want to look at
App Engine Modules in Java
before you implement your code. You may separate your user-facing and backend code into different modules with different scaling options.
UPDATE:
A great comment from #tx802:
I think 1 minute is the highest frequency you can achieve on App Engine, but you can use cron to put 12 tasks on a Push Queue, either with delays of 5s, 10s, ... 55s using TaskOptions.countdownMillis() or with a processing rate of 1/5 sec.

How can I do the same thing over and over every 1-4 seconds in google app engine?

I want to run a script every few seconds (4 or less) in google app engine to process user input and generate output. What is the best way to do this?
Run a cron job.
http://code.google.com/appengine/docs/python/config/cron.html
http://code.google.com/appengine/docs/java/config/cron.html
A cron job will invoke a URL at a
given time of day. A URL invoked by
cron is subject to the same limits and
quotas as a normal HTTP request,
including the request time limit.
.
Also consider the Task Queue - http://code.google.com/appengine/docs/python/taskqueue/overview.html
Reconsider what you're doing. As Ash Kim says, you can do it with the task queue, but first take a close look if you really need to run a process like this. Is it possible to rewrite things so the task runs only when needed, or immediately, or lazily (that is, only when the results are needed)?

Resources