FedStoa / moa

A Mastodon, Twitter, and Instagram Cross-poster
https://moa.party
MIT License
355 stars 19 forks source link

Worker crashes due to missing function in SQLite #105

Closed muellermartin closed 5 years ago

muellermartin commented 5 years ago

When I run the moa worker, I get following error:

2019-01-08 14:35:12,403 [21279] [worker.py:63 : <module>()] Starting up…
2019-01-08 14:35:12,442 [21279] [worker.py:95 : <module>()] Worker lock found
2019-01-08 14:35:12,446 [21279] [worker.py:111 : <module>()] Stale Worker found
Traceback (most recent call last):
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: no such function: rand

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/moa/moa/moa/worker.py", line 126, in <module>
    for bridge in bridges:
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2995, in __iter__
    return self._execute_and_instances(context)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3018, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
    context)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 248, in reraise
    raise value.with_traceback(tb)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/home/moa/moa/.venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such function: rand [SQL: 'SELECT bridge.id AS bridge_id, bridge.twitter_oauth_token AS bridge_twitter_oauth_token, bridge.twitter_oauth_secret AS bridge_twitter_oauth_secret, bridge.twitter_last_id AS bridge_twitter_last_id, bridge.twitter_handle AS bridge_twitter_handle, bridge.mastodon_access_code AS bridge_mastodon_access_code, bridge.mastodon_last_id AS bridge_mastodon_last_id, bridge.mastodon_account_id AS bridge_mastodon_account_id, bridge.mastodon_user AS bridge_mastodon_user, bridge.mastodon_host_id AS bridge_mastodon_host_id, bridge.enabled AS bridge_enabled, bridge.instagram_access_code AS bridge_instagram_access_code, bridge.instagram_last_id AS bridge_instagram_last_id, bridge.instagram_account_id AS bridge_instagram_account_id, bridge.instagram_handle AS bridge_instagram_handle, bridge.t_settings_id AS bridge_t_settings_id, bridge.created AS bridge_created, bridge.updated AS bridge_updated \nFROM bridge \nWHERE bridge.enabled = 1 ORDER BY rand()'] (Background on this error at: http://sqlalche.me/e/e3q8)

This is likely caused by line 122 in worker.py. Thanks to Stack Overflow I found out, that the func.rand() function is specific to MySQL while PostgreSQL and SQLite need to use func.random() instead. Unfortunately I never used SQLAlchemy before, therefore I'm not able to quickly create a PR with a fix, but if you need help say so :)

foozmeat commented 5 years ago

I didn't design Moa to run in production mode against sqlite that's why the rand() is disabled when DEVELOPMENT is True. I just pushed a change that also checks for sqlite in the connection URL so this will be disabled properly.